业务描述:
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 项目中