DELPHI 6 呼叫MicroSoft 寫的WebService How To. |
|
HikaruGo
中階會員 發表:22 回覆:69 積分:88 註冊:2007-12-09 發送簡訊給我 |
文章 DELPHI 6 呼叫MicroSoft 寫的WebService How To.
C#的WebService [code c#] using System; using System.Collections.Generic; using System.Web; using System.Web.Services; namespace AccountService { /// <summary> ///Service1 的摘要描述 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] public class AccountService : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public string[] GetAccNumList(string CIF) { try { this.Context.Request.SaveAs(@"c:\log\" DateTime.Now.ToString("yyyymmddhhnnss") "req.txt", true); } catch (Exception ex) { // } List<string> myList = new List<string>(); myList.Add("1111111111"); myList.Add("2222222222"); myList.Add("3333333333"); myList.Add(CIF); return myList.ToArray(); } [WebMethod] public string[] GetAccNumListFrCrd(string CardNumber) { try { this.Context.Request.SaveAs(@"c:\log\" DateTime.Now.ToString("yyyymmddhhnnss") "req.txt", true); } catch (Exception ex) { // } List<string> myList = new List<string>(); myList.Add("4444444444"); myList.Add("5555555555"); myList.Add("6666666666"); myList.Add(CardNumber); return myList.ToArray(); } [/code] 案例一: 1. 使用 Delphi6 THTTPRIO 呼叫 WebService STEP1 :Import WSDL STEP 2: 呼叫 WebService GetAccNumList 回傳 ArrayOfString = array of String; [code delphi] var intfEV: AccountServiceSoap; ss:integer; v:Variant ; CIF:String; gg:string; begin CIF := 'CIF'; intfEV := HTTPRIO1 as AccountServiceSoap; v := intfEV.GetAccNumList( CIF); //問題只能宣告Variant, //無法宣告ArrayOfString [/code] Step3: DEBUG C# Webservice 發現 public string[] GetAccNumList(string CIF) CIF 為 null 表示收不到Delphi 發的CIF 參數 STEP4: 發送的Soap封包格式如下 [code c#] <?xml version="1.0"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"> <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <NS1:GetAccNumList xmlns:NS1="http://tempuri.org/"> <CIF xsi:type="xsd:string">CIFddd</CIF> </NS1:GetAccNumList> </SOAP-ENV:Body> </SOAP-ENV:Envelope> [/code] 發現是多一個NS1:導致C#無法收到LOG 改用C# Client發送 SOAP SOAP LOG [code c#] <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body><GetAccNumList xmlns="http://tempuri.org/"> <CIF>ddd</CIF> </GetAccNumList> </soap:Body> </soap:Envelope> [/code] WebService可以收到 C# Client 發的CIF 參數 改寫DELPHI CODE 改用 SOAP Tool Kit [code delphi] type TStringDynArray = Array of String; var vWSObj:Variant; ret:Variant; i:integer; ds : TStringDynArray; ds2 : TStringDynArray; begin vWSObj := CreateOleObject('MSSOAP.SoapClient'); vWSObj.mssoapinit('http://localhost:1673/AccountService.asmx?WSDL'); caption := vWSObj.HelloWorld; // ds := vWSObj.GetAccNumList('123'); for i := Low(ds) to high(ds) do begin memo1.Lines.Add(ds[i]); end; ds2 := vWSObj.GetAccNumListFrCrd('456'); for i := Low(ds2) to high(ds2) do begin memo1.Lines.Add(ds2[i]); end; vWSObj := unassigned; end; [/code] Array of String; 可以使用了 WebService可以收到 ('MSSOAP.SoapClient' 發的CIF 參數 SOAP 封包 [code delphi] POST /AccountService.asmx HTTP/1.1 Cache-Control: no-cache Connection: Keep-Alive Content-Length: 317 Content-Type: text/xml Host: localhost:1672 User-Agent: SOAP Sdk SOAPAction: "http://tempuri.org/GetAccNumListFrCrd" <?xml version="1.0" encoding="UTF-8" standalone="no"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Body><GetAccNumListFrCrd xmlns="http://tempuri.org/"> <CardNumber xmlns:SOAPSDK1="http://tempuri.org/">456</CardNumber> </GetAccNumListFrCrd> </SOAP-ENV:Body> </SOAP-ENV:Envelope> [/code] 編輯記錄
HikaruGo 重新編輯於 2009-04-20 15:05:18, 註解 無‧
|
salo0610
高階會員 發表:42 回覆:120 積分:107 註冊:2003-02-18 發送簡訊給我 |
|
wayne_cheng
一般會員 發表:1 回覆:19 積分:14 註冊:2002-07-10 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |