Delphi與WebService

大家都知道C# Webservice技術的出現將各種開發技術和語言完全的融合了,下面就這種融合在C#和delphi之間的交互做一次全面的體現,前者是目前最好的開發平臺,後者依然是小型c/s系統的最佳選擇.

1.使用C#創建一個Webservice服務。

使用vs2005的模板創建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. //如果使用設計的組件,請取消註釋以下行    
  11. InitializeComponent();   
  12. }   
  13.    
  14. #region  Component  Designer    
  15. generated  code   
  16. private void InitializeComponent()   
  17. {   
  18.     
  19. }   
  20. //Web  服務設計器所必需的    
  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. //這個是自動生成的一個webservice函數,  
  34. 可以不要。   
  35. [WebMethod]   
  36. public string HelloWorld() {   
  37. return \"Hello World\";   
  38. }   
  39. //這個纔是我們自己創建的,   
  40. [WebMethod]     
  41. public int addnumber(int a, int b)   
  42. {   
  43. return = a + b;   
  44. }   

2.使用delphi創建一個dll(非com的dll),該dll調用上面的webserivce服務。

使用delphi調用C#的webservice過程也很容易,但是對於新手可能比較麻煩(我就是這麼過來的)

第一步:創建一個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. //這個GetServiceSoap是什麼,看後面...   
  9. result:=ireturn;   
  10. end;  
  11. exports   
  12. GetNum name ’GetNum’;   
  13. begin   
  14. end.   
  15. //如果是delphi調用該dll必須使用下面的代碼。  
  16. C#調用則不需要了(C#還是要牛一些,呵呵)   
  17. initialization   
  18. coinitialize(nil);   
  19. finalization   
  20. counInitializ   
  21. 第二步:將webserivce的WSDL導入到該dll工程中,  
  22. 如何導,方法至少有兩種,我說簡單的一種:   
  23. file->new->other->WebService->WSDL Importer,  
  24. (將C#的WSDL輸入)然後delphi會自動給你生成了一個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. //對於無法識別傳入的參數的問題,需要手工加上這一句>......   
  76. InvRegistry.RegisterInvokeOptions(TypeInfo  
  77. (ServiceSoap), ioDocument);   
  78.    
  79. 這段代碼基本上你不用關心,但是,有兩個地方需要特別注意:   
  80. 1.RIO.HTTPWebNode.UseUTF8InHeader:=True;  
  81. 對於中文參數必須加上   
  82. 2.InvRegistry.RegisterInvokeOptions  
  83. (TypeInfo(ServiceSoap), ioDocument);  

如果傳入的參數不能被webservice識別時,多半是因爲你沒有加上這一句。

3.使用delphi調用上面的dll 就一個函數,沒有什麼好說的:

  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)); {調用GetNumTotal函數}   
  17. end   
  18. else   
  19. ShowMessage(’GetNumTotal函數沒有找到’);   
  20. finally   
  21. FreeLibrary(Th); {釋放DLL}   
  22. end   
  23. else   
  24. ShowMessage(’mywebservice.dll沒有找到’);   
  25. end;   

4.使用C#調用上面的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. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章