实际写的一段:
1、工程内,File->new->Webservice->WSDLImporter,导入webservice地址,生成一个新的pas,得到输出函数,比如叫GetSvrForPosSoap
2、该pas内切记,在initialization后面加上
InvRegistry.RegisterInvokeOptions(TypeInfo(SvrForPosSoap), ioDocument);//这行要手工加,否则传入参数会为空
3、
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, SOAPHTTPClient, StdCtrls, InvokeRegistry, Rio, ActiveX;
uses
SvrForPos,XSBuiltIns;
procedure TForm1.Button1Click(Sender: TObject);
var
HelloWorld,sMsg:widestring;
HTTPRIO:THTTPRIO;
begin
try
begin
HTTPRIO := THTTPRIO.Create(Application);
HTTPRIO.HTTPWebNode.UseUTF8InHeader:=true;
HelloWorld := GetSvrForPosSoap(True,'', HTTPRIO).HelloWorld('Test My Brain,脑子!我要脑子!');
Edit1.Text:=HelloWorld;
end
Except
On E:Exception do
begin
sMsg:=E.Message;
sMsg:='意外失败:'+sMsg;
showmessage(sMsg);
end;
end;
end;
4、稍注意: coinitialize( nil )和CoUninitialize;
============================================================
例子1:
http://www.cnblogs.com/jxsoft/archive/2011/07/21/2113012.html
例子2:
Webservice技术的出现将各种开发技术和语言完全的融合了,下面就这种融合在C#和delphi之间的交互做一次全面的体现
1.使用C#创建一个Webservice服务。
使用vs2005的模板创建C#的webservice非常容易。原文件如下:
[WebService(Namespace = "http://localhost/webserver/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public>
> {
> }
}
2.使用delphi创建一个dll(非com的dll),该dll调用上面的webserivce服务。
使用delphi调用C#的webservice过程也很容易,但是对于新手可能比较麻烦(我就是这么过来的)
第一步:创建一个dll单元: //http://www.newasp.net/tech/57522.html
{$R *.res}
function GetNum(a,b:integer):integer> result:=ireturn;
end;
exports
GetNum> defURL = ’http://localhost/webserver/Service.asmx’;
> defPrt = ’ServiceSoap’;
var
RIO: THTTPRIO;
begin
Result :=> begin
> else
Addr :=> RIO := THTTPRIO.Create(nil)
> try
//RIO.HTTPWebNode.UseUTF8InHeader:=True; //在此添加一句,修改编码方案。
Result := (RIO> RIO.Service := defSvc;
RIO.Port := defPrt;
> finally
> RIO.Free;
> 2.InvRegistry.RegisterInvokeOptions(TypeInfo(ServiceSoap), ioDocument);
如果传入的参数不能被webservice识别时,多半是因为你没有加上这一句。
不加就报这个错误:
3.使用delphi调用上面的dll
就一个函数,没有什么好说的:
procedure TForm1.Button1Click(Sender: TObject);
type
GetNumTotal=function(a,b:integer):integer;stdcall;
var
Th:Thandle;
Tf:GetNumTotal;
Tp:TFarProc;
begin
Th:=LoadLibrary(’mywebservice.dll’); {装载DLL}
> if Tp<>nil
> Edit1.Text:=IntToStr(Tf(1,3)); {调用GetNumTotal函数}
>//如果是delphi调用该dll必须使用下面的代码。C#调用则不需要了(C#还是要牛一些,呵呵)
initialization
coinitialize(nil); //关于这儿查看http://www.cnblogs.com/IceKernel/articles/2563721.html
//关于这儿也查看http://blog.csdn.net/tercel99/article/details/2125164
finalization
counInitialize;
//uses ActiveX, // 确认加入这个单元 // 老版本的Delphi用 OLE2 代替 Windows;
use上面这个ActiveX,才能使用initialization 和finalization
4.使用C#调用上面的dll
[DllImport("mywebservice.dll", EntryPoint = "GetNum")]
public static extern int GetNum(int a, int b);
private void button1_Click(object sender, EventArgs e)
{
int a,b,i;
a=10;
b =20;
i=GetNum(a,b); //第一次比较慢(webserivce的唯一弊端!!!!)
textBox1.Text = i.ToString();
}
以下个人经验
在delphi调用 .net webservice时,中文传递过去就会变成????
问题原因:是因为传递时没用使用UTF8方式进行传递。
解决方式:在生成的pas文件中,找到Get*Soap的方法,在中间加入橙色字体那部分。就能够传递中文了
RIO := THTTPRIO.Create(nil);
try
> RIO.Service := defSvc;
RIO.Port :=> end else
> RIO.HTTPWebNode.UseUTF8InHeader := True;//中文
end;
Result := (RIO as VioAccessWebServiceSoap);
finally
> RIO.Free;
end;
一些其它注意事项:
http://icelander.iteye.com/blog/214586
部署webservice:
http://hi.baidu.com/bmlvy2006/item/d6541f8c34a7d956e73d195c