C#调用WebService(Soap)接口方法
首先了解两点知识:
1.什么是WebService
2.什么是soap
WebService:是一个SOA(面向服务的编程)的架构,它是不依赖于语言,不依赖于平台,可以实现不同的语言间的相互调用,通过Internet进行基于Http协议的网络应用间的交互。
WebService实现不同语言间的调用,是依托于一个标准,webservice是需要遵守WSDL(web服务定义语言)/SOAP(简单请求协议)规范的。 WebService=WSDL+SOAP+UDDI(webservice的注册)。
WebServices 提供一个建立分布式应用的平台。
Soap :是基于 XML 的简易协议,可使应用程序在 HTTP 之上进行信息交换。或者更简单地说:SOAP 是用于访问网络服务的协议,基于 XML。SOAP 是一种用于发送消息的格式。
Soap协议的Post方法
/// <summary> /// Soap协议Post方法 /// </summary> /// <param name="uri">WebService地址</param> /// <param name="str">传入Soap协议格式数据</param> /// <returns></returns> public static string SendToReturn(Uri _uri, StringBuilder _str) { string _returnstr = ""; //发起请求 WebRequest webRequest = WebRequest.Create(_uri); webRequest.ContentType = "text/xml; charset=utf-8"; webRequest.Method = "POST"; using (Stream requestStream = webRequest.GetRequestStream()) { byte[] paramBytes = Encoding.UTF8.GetBytes(_str.ToString()); requestStream.Write(paramBytes, 0, paramBytes.Length); } //响应 try { WebResponse webResponse = webRequest.GetResponse(); using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8)) { _returnstr = myStreamReader.ReadToEnd(); } } catch (WebException ex) { _returnstr = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd(); } return _returnstr; }
Soap协议的Xml格式组装(根据每个人情况自行组装)
StringBuilder soap = new StringBuilder(); soap.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); soap.Append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soap=\"http://www.shaphar.com/SoapService\" >"); soap.Append("<soapenv:Header/>"); soap.Append("<soapenv:Body>"); soap.Append("<soap:REQUEST_DATA>"); soap.Append("<soap:commonHeader>"); soap.AppendFormat("<soap:BIZTRANSACTIONID>InsertOUTPT_CARRIER_TRACKING_PS_{0}</soap:BIZTRANSACTIONID><soap:COUNT>{1}</soap:COUNT><soap:CONSUMER>{2}</soap:CONSUMER>", DateTime.Now.ToString("yyyyMMddHHmmss"), 1, "SYGD"); soap.AppendFormat("<soap:SRVLEVEL>1</soap:SRVLEVEL><soap:ACCOUNT></soap:ACCOUNT> <soap:PASSWORD></soap:PASSWORD><soap:COMMENTS></soap:COMMENTS>"); soap.Append("</soap:commonHeader>"); soap.Append("<soap:LIST><![CDATA[<DATA>"); soap.Append("<ITEM>"); soap.AppendFormat("<BL_LOAD_NO>{0}</BL_LOAD_NO>", soapBill.BL_LOAD_NO == null ? "" : soapBill.BL_LOAD_NO); soap.AppendFormat("<BL_ORDER_NO>{0}</BL_ORDER_NO>", soapBill.BL_ORDER_NO == null ? "" : soapBill.BL_ORDER_NO); soap.AppendFormat("<CUSTOMER_ORDER_NO>{0}</CUSTOMER_ORDER_NO>", soapBill.CUSTOMER_ORDER_NO == null ? "" : soapBill.CUSTOMER_ORDER_NO); soap.AppendFormat("<STATUS>{0}</STATUS>", soapBill.STATUS == null ? "" : soapBill.STATUS); soap.AppendFormat("<TRACKING_TIME>{0}</TRACKING_TIME>", soapBill.TRACKING_TIME == null ? "" : soapBill.TRACKING_TIME); soap.AppendFormat("<STATUS_TIME>{0}</STATUS_TIME>", soapBill.STATUS_TIME == null ? "" : soapBill.STATUS_TIME); soap.AppendFormat("<POSITION>{0}</POSITION>", soapBill.POSITION == null ? "" : soapBill.POSITION); soap.AppendFormat("<VEHICLE_ID>{0}</VEHICLE_ID>", soapBill.VEHICLE_ID == null ? "" : soapBill.VEHICLE_ID); soap.AppendFormat("<DRIVER_NAME>{0}</DRIVER_NAME>", soapBill.DRIVER_NAME == null ? "" : soapBill.DRIVER_NAME); soap.AppendFormat("<SIGNER>{0}</SIGNER>", soapBill.SIGNER == null ? "" : soapBill.SIGNER); soap.AppendFormat("<VEHICLE_TYPE>{0}</VEHICLE_TYPE>", soapBill.VEHICLE_TYPE == null ? "" : soapBill.VEHICLE_TYPE); soap.AppendFormat("<NODE_DESCRIPTION>{0}</NODE_DESCRIPTION>", soapBill.NODE_DESCRIPTION == null ? "" : soapBill.NODE_DESCRIPTION); soap.AppendFormat("<NOTES>{0}</NOTES>", soapBill.NOTES == null ? "" : soapBill.NOTES); soap.AppendFormat("<QTY_EACH>{0}</QTY_EACH>", soapBill.QTY_EACH == null ? "" : soapBill.QTY_EACH); soap.AppendFormat("<GROSSWEIGHT>{0}</GROSSWEIGHT>", soapBill.GROSSWEIGHT == null ? "" : soapBill.GROSSWEIGHT); soap.AppendFormat("<CUBIC>{0}</CUBIC>", soapBill.CUBIC == null ? "" : soapBill.CUBIC); soap.AppendFormat("<UDF01>{0}</UDF01>", soapBill.UDF01 == null ? "" : soapBill.UDF01); soap.AppendFormat("<UDF02>{0}</UDF02>", soapBill.UDF02 == null ? "" : soapBill.UDF02); soap.AppendFormat("<UDF03>{0}</UDF03>", soapBill.UDF03 == null ? "" : soapBill.UDF03); soap.AppendFormat("<UDF04>{0}</UDF04>", soapBill.UDF04 == null ? "" : soapBill.UDF04); soap.AppendFormat("<UDF05>{0}</UDF05>", soapBill.UDF05 == null ? "" : soapBill.UDF05); soap.Append("</ITEM>"); soap.Append("</DATA>]]></soap:LIST>"); soap.Append("</soap:REQUEST_DATA>"); soap.Append("</soapenv:Body>"); soap.Append("</soapenv:Envelope>");
Soap协议的Post方法调用
//插入请求数据的日志 AddInterfaceLog("SendNodeReturnData", "请求数据:" + soap.ToString(), 1); //调用webService接口(soap) Uri uri = new Uri(reqUrl); string soapResult = SendToReturn(uri, soap); //插入响应结果的日志 AddInterfaceLog("SendNodeReturnData", "响应结果:" + soapResult, 1);
调用后结果
PS:
具体参考以下两篇博客:
https://www.jb51.net/article/130404.htm
https://blog.csdn.net/sinat_29009169/article/details/103917166
平时多记记,到用时才能看看,记录你的进步,分享你的成果