delphi 调用 C#写的webservice

看以下的一个例子(转) 
先用vs.net创建一个简单的webservices; 
1.打开vs.net2003   新建一个webservises   命名为WebService1             
2.添加两个WebMethod 

[WebMethod] 
public string HelloWorld()
{
return "Hello World 用.NET写的web服务!!^_^ ";
}

[WebMethod]
public string datetostr(DateTime dt)//将时间转换为字符
{
return dt.ToUniversalTime().ToString();
}

  完整代码

using   System; 

using System.Collections;

using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace WebService1
{
public class Service1 : System.Web.Services.WebService
{
public Service1()
{
//CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
InitializeComponent();

#region 组件设计器生成的代码
//Web 服务设计器所必需的
private IContainer components = null;

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{

components.Dispose();
}
base.Dispose(disposing);
}
#endregion

[WebMethod]
public string HelloWorld()
{
return "Hello World 用.NET写的web服务!!^_^ ";
}
[WebMethod]
public string datetostr(DateTime dt)//将时间转换为字符
{
return dt.ToUniversalTime().ToString();
}
}
}

  

1下面在打开delphi   7 
2新建一个应用程序: 
拖两个button两个edit   和一个HTTPRIO(在webservices页上)到窗体上; 
3.接下来: 
File-> new-> other-> webservices-> WSDL   importer 
点击next   导入 
保存unit   Service1 
4.在unit1(form1   的单元文件)中引用Service1,   XSBuiltIns 
5.   HTTPRIO1的 (特别注意)

   。 delphi 7里的设置
    WSDLLocation     属性设置为 'http://localhost/WebService1/Service1.asmx?WSDL ' 

    选择Port和server ,一定要先不然会出错

   。 delphi 2010里的设置 

   URL 属性设置“ttp://localhost/WebService1/Service1.asmx?WSDL ”   只设置这个就行了,不能按delphi7里的设置不然会报错


6.添加form1   的代码如下: 

unit   Unit1;   
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, InvokeRegistry, Rio, SOAPHTTPClient;
type

TForm1
= class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Edit2: TEdit;
HTTPRIO1: THTTPRIO;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
XSBuiltIns,
Service1;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
dtm:TXSDateTime;
begin
dtm:
=TXSDateTime.Create;
dtm:
=DateTimeToXSDateTime(now);
Edit1.Text:
=(HTTPRIO1 as Service1Soap).datetostr(dtm);
dtm.free;
end;  
procedure   TForm1.Button2Click(Sender:   TObject); 
begin
Edit2.Text:
=(HTTPRIO1 as Service1Soap).HelloWorld;
end;

end.

vs.net2005开发的话 

要在delphi中加一句InvRegistry.RegisterInvokeOptions(TypeInfo(ServiceSoap),   ioDocument);   //测式了返回值还是空,不知为什么。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值