日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
詳解C#Webservice與Delphi交互

大家都知道C# Webservice技術(shù)的出現(xiàn)將各種開(kāi)發(fā)技術(shù)和語(yǔ)言完全的融合了,下面就這種融合在C#和delphi之間的交互做一次全面的體現(xiàn),前者是目前***的開(kāi)發(fā)平臺(tái),后者依然是小型c/s系統(tǒng)的***選擇.

1.使用C#創(chuàng)建一個(gè)Webservice服務(wù)。

使用vs2005的模板創(chuàng)建C# Webservice非常容易。原文件如下:

 
 
 
  1. [WebService(Namespace = \"http:  
  2. //localhost/webserver/\")]   
  3. [WebServiceBinding(ConformsTo =   
  4. WsiProfiles.BasicProfile1_1)]   
  5.     
  6. public class Service :   
  7. System.Web.Services.WebService   
  8. {   
  9. public Service () {   
  10. //如果使用設(shè)計(jì)的組件,請(qǐng)取消注釋以下行    
  11. InitializeComponent();   
  12. }   
  13.    
  14. #region  Component  Designer    
  15. generated  code   
  16. private void InitializeComponent()   
  17. {   
  18.     
  19. }   
  20. //Web  服務(wù)設(shè)計(jì)器所必需的    
  21. private IContainer components = null;   
  22.  
  23. protected override void Dispose  
  24. (bool disposing)   
  25. {   
  26. if (disposing && components != null)   
  27. {   
  28. components.Dispose();   
  29. }   
  30. base.Dispose(disposing);   
  31. }   
  32. #endregion   
  33. //這個(gè)是自動(dòng)生成的一個(gè)webservice函數(shù),  
  34. 可以不要。   
  35. [WebMethod]   
  36. public string HelloWorld() {   
  37. return \"Hello World\";   
  38. }   
  39. //這個(gè)才是我們自己創(chuàng)建的,   
  40. [WebMethod]     
  41. public int addnumber(int a, int b)   
  42. {   
  43. return = a + b;   
  44. }   

2.使用delphi創(chuàng)建一個(gè)dll(非com的dll),該dll調(diào)用上面的webserivce服務(wù)。

 使用delphi調(diào)用C#的webservice過(guò)程也很容易,但是對(duì)于新手可能比較麻煩(我就是這么過(guò)來(lái)的)

***步:創(chuàng)建一個(gè)dll單元:

 
 
 
  1. {$R *.res}   
  2. function GetNum(a,b:integer):  
  3. integer stdcall; [Page]  
  4. var   
  5. ireturn :Integer;   
  6. begin   
  7. ireturn:=GetServiceSoap().addnumber(a,b);   
  8. //這個(gè)GetServiceSoap是什么,看后面...   
  9. result:=ireturn;   
  10. end;  
  11. exports   
  12. GetNum name ’GetNum’;   
  13. begin   
  14. end.   
  15. //如果是delphi調(diào)用該dll必須使用下面的代碼。  
  16. C#調(diào)用則不需要了(C#還是要牛一些,呵呵)   
  17. initialization   
  18. coinitialize(nil);   
  19. finalization   
  20. counInitializ   
  21. 第二步:將webserivce的WSDL導(dǎo)入到該dll工程中,  
  22. 如何導(dǎo),方法至少有兩種,我說(shuō)簡(jiǎn)單的一種:   
  23. file->new->other->WebService->WSDL Importer,  
  24. (將C#的WSDL輸入)然后delphi會(huì)自動(dòng)給你生成了一個(gè)pas文件,   
  25. function GetServiceSoap(UseWSDL: Boolean;   
  26. Addr: string; HTTPRIO: THTTPRIO): ServiceSoap;   
  27. const   
  28. defWSDL = ’http://localhost/webserver/  
  29. Service.asmx?WSDL’;   
  30. defURL= ’http://localhost/webserver/  
  31. Service.asmx’;   
  32. defSvc= ’Service’;   
  33. defPrt= ’ServiceSoap’;   
  34. var   
  35. RIO: THTTPRIO;   
  36. begin   
  37. Result := nil;   
  38. if (Addr = ’’) then   
  39. begin   
  40. if UseWSDL then   
  41. Addr := defWSDL   
  42. else   
  43. Addr := defURL;   
  44. end;   
  45. if HTTPRIO = nil then   
  46. RIO := THTTPRIO.Create(nil)   
  47. else   
  48. RIO := HTTPRIO;   
  49. try   
  50. //RIO.HTTPWebNode.UseUTF8InHeader:=  
  51. True; //在此添加一句,修改編碼方案。   
  52. Result := (RIO as ServiceSoap);   
  53. if UseWSDL then   
  54. begin   
  55. RIO.WSDLLocation := Addr;   
  56. RIO.Service := defSvc;  
  57. RIO.Port := defPrt;   
  58. end else   
  59. RIO.URL := Addr;   
  60. finally   
  61. if (Result = nil) and   
  62. (HTTPRIO = nil) then   
  63. RIO.Free;   
  64. end;   
  65. end;   
  66.  
  67. initialization   
  68. InvRegistry.RegisterInterface  
  69. (TypeInfo(ServiceSoap), ’  
  70. http://localhost/webserver/’, ’  
  71. utf-8’); [Page]  
  72. InvRegistry.RegisterDefaultSOAPAction  
  73. (TypeInfo(ServiceSoap), ’  
  74. http://localhost/webserver/%operationName%’);   
  75. //對(duì)于無(wú)法識(shí)別傳入的參數(shù)的問(wèn)題,需要手工加上這一句>......   
  76. InvRegistry.RegisterInvokeOptions(TypeInfo  
  77. (ServiceSoap), ioDocument);   
  78.    
  79. 這段代碼基本上你不用關(guān)心,但是,有兩個(gè)地方需要特別注意:   
  80. 1.RIO.HTTPWebNode.UseUTF8InHeader:=True;  
  81. 對(duì)于中文參數(shù)必須加上   
  82. 2.InvRegistry.RegisterInvokeOptions  
  83. (TypeInfo(ServiceSoap), ioDocument);  

如果傳入的參數(shù)不能被webservice識(shí)別時(shí),多半是因?yàn)槟銢](méi)有加上這一句。

3.使用delphi調(diào)用上面的dll 就一個(gè)函數(shù),沒(méi)有什么好說(shuō)的:

 
 
 
  1. procedure TForm1.Button1Click(Sender: TObject);   
  2. type   
  3. GetNumTotal=function(a,b:integer):integer;stdcall;   
  4. var   
  5. Th:Thandle;   
  6. Tf:GetNumTotal;   
  7. Tp:TFarProc;   
  8. begin   
  9.  Th:=LoadLibrary(’mywebservice.dll’); {裝載DLL}   
  10. if Th〉0 then   
  11. try   
  12. Tp:=GetProcAddress(Th,PChar(’GetNum’));   
  13. if Tp〈 〉nil   
  14. then begin   
  15. Tf:=GetNumTotal(Tp);   
  16. Edit1.Text:=IntToStr(Tf(1,3)); {調(diào)用GetNumTotal函數(shù)}   
  17. end   
  18. else   
  19. ShowMessage(’GetNumTotal函數(shù)沒(méi)有找到’);   
  20. finally   
  21. FreeLibrary(Th); {釋放DLL}   
  22. end   
  23. else   
  24. ShowMessage(’mywebservice.dll沒(méi)有找到’);   
  25. end;   

4.使用C#調(diào)用上面的dll

 
 
 
  1. [DllImport(\"mywebservice.dll\",  
  2. EntryPoint=\"GetNum\")]  
  3. publicstaticexternintGetNum  
  4. (inta,intb);  
  5. privatevoidbutton1_Click  
  6. (objectsender,EventArgse)  
  7. {  
  8. inta,b,i;  
  9. a=10;  
  10. b=20;  
  11. i=GetNum(a,b);//***次比較慢  
  12. (webserivce的唯一弊端!?。?!)  
  13. textBox1.Text=i.ToString();  
  14. }  

名稱欄目:詳解C#Webservice與Delphi交互
當(dāng)前URL:http://www.dlmjj.cn/article/dpcdpeo.html