1. 程式人生 > 實用技巧 >.Net Core 呼叫WebService

.Net Core 呼叫WebService

業務描述:

  SAP系統的開發人員提供了一個根據訂單號獲取SAP訂單的WebService。此WebService與之前的WebService有點不同,需要輸入使用者名稱及密碼。

  WSDL地址:
    http://gwsapdev03.greatwall.com.cn:8000/sap/bc/srt/wsdl/bndg_EB1D857C0E8C9DF18A1F00155DF0731F/wsdl11/allinone/ws_policy/document?sap-client=830
  SOAP地址:

    http://gwsapdev03.greatwall.com.cn:8000/sap/bc/srt/rfc/sap/zws_wms_get_prdorder_data/830/zws_get_prdorder_data/zws_get_prdorder_data

  注:直接在瀏覽器裡輸入地址後會彈出輸入使用者名稱及密碼的對話方塊。

解決思路:

  1:由於.Net Core 無法直接新增服務引用(報錯),於是準備用Vs2012 ASP.Net 專案先新增服務應用,生成Reference.cs後再拷貝到.Net Core專案中來

  2:在VS2012先呼叫通過

  3:由於新增服務引用時,Web.config檔案中會生成一些配置檔案(<system.serviceModel>),需刪除這部分配置程式碼再除錯通過。

  4:最後在.Net Core 專案中除錯通過。

開始:

步驟一:

  用WSDL地址新增時,提示了3次輸入使用者名稱和密碼,輸入後新增服務引用成功。

  用SOAP地址新增服務引用,提示錯誤(驗證不通過)。忽略之

步驟二:開始在VS2012種寫測試程式碼(寫完感覺挺簡單的,麻煩的在後面)

 protected void btnOk_Click(object sender, EventArgs e)
        {
            GreatwallSapWebService.ZWS_WMS_GET_PRDORDER_DATAClient client = new GreatwallSapWebService.ZWS_WMS_GET_PRDORDER_DATAClient();
            GreatwallSapWebService.ZwmsGetPrdorderDataRequest request 
= new GreatwallSapWebService.ZwmsGetPrdorderDataRequest(); request.IPrdorder = "1179209"; client.ClientCredentials.UserName.UserName = "WMSRFC"; client.ClientCredentials.UserName.Password = "Wmsrfc*2020"; Task<GreatwallSapWebService.ZwmsGetPrdorderDataResponse> responseTask = client.ZwmsGetPrdorderDataAsync(request); GreatwallSapWebService.ZwmsGetPrdorderDataResponse response = responseTask.Result; }
View Code

步驟三: 刪除掉Web.config <system.serviceModel> 這個節點,繼續寫測試程式碼

配置檔案:httpsTransport 刪除掉S,生成出來時,執行會報錯。

 <system.serviceModel>
    <bindings>
      <customBinding>
        <binding name="ZWS_GET_PRDORDER_DATA" >
          <textMessageEncoding messageVersion="Soap11" />
          <httpTransport authenticationScheme="Basic" />
        </binding>
      </customBinding>
    </bindings>
    <client>
      <endpoint address="http://gwsapdev03.greatwall.com.cn:8000/sap/bc/srt/rfc/sap/zws_wms_get_prdorder_data/830/zws_get_prdorder_data/zws_get_prdorder_data"
        binding="customBinding" bindingConfiguration="ZWS_GET_PRDORDER_DATA"
        contract="GreatwallSapWebService.ZWS_WMS_GET_PRDORDER_DATA"
        name="ZWS_GET_PRDORDER_DATA" />
    </client>
  </system.serviceModel>

可以看出這個配置資訊下有個customBinding 節點,後面的程式碼思路就是用程式碼的方式替代配置項的方式。

這塊走了點彎路,一開始時通過錯誤提示在百度上查,然後不斷地嘗試。浪費了一些時間。

且由於多次連線不成功後,導致SAP提供的那個賬號被鎖住了(一開始還沒發現),於是就有個OK和error按鈕。

沒點一次Error按鈕就再點一次OK按鈕,來保證賬號不被鎖住。(這些都是題外話)

注:可以跟蹤已除錯通過(第一個方法),看看Client 這個變數裡。見下圖:

  protected void btnError_Click(object sender, EventArgs e)
        {
            HttpTransportBindingElement httpTransport = new HttpTransportBindingElement();
            httpTransport.AuthenticationScheme = System.Net.AuthenticationSchemes.Basic;
            TextMessageEncodingBindingElement textMessageEncoding = new TextMessageEncodingBindingElement();
            textMessageEncoding.MessageVersion = MessageVersion.Soap11;
            
            SynchronizedCollection<BindingElement> coll = new SynchronizedCollection<BindingElement>();
            coll.Add(textMessageEncoding); 
            coll.Add(httpTransport);

            System.ServiceModel.Channels.CustomBinding myBinding = new System.ServiceModel.Channels.CustomBinding(coll);
            EndpointAddress address = new EndpointAddress("http://gwsapdev03.greatwall.com.cn:8000/sap/bc/srt/rfc/sap/zws_wms_get_prdorder_data/830/zws_get_prdorder_data/zws_get_prdorder_data");

            GreatwallSapWebService.ZWS_WMS_GET_PRDORDER_DATAClient client = new GreatwallSapWebService.ZWS_WMS_GET_PRDORDER_DATAClient(myBinding, address);
          
            GreatwallSapWebService.ZwmsGetPrdorderDataRequest request = new GreatwallSapWebService.ZwmsGetPrdorderDataRequest();
            request.IPrdorder = "1179209";
            client.ClientCredentials.UserName.UserName = "WMSRFC";
            client.ClientCredentials.UserName.Password = "Wmsrfc*2020";

            Task<GreatwallSapWebService.ZwmsGetPrdorderDataResponse> responseTask = client.ZwmsGetPrdorderDataAsync(request);
            GreatwallSapWebService.ZwmsGetPrdorderDataResponse response = responseTask.Result;
          
        }
View Code

步驟三:拷貝到.Net Core 專案中