C# http webservice wsdl soap xml 通过C#调用webservice基于sop协议的接口
一:注意事项
接口地址示例:https://********/DataExchangeForCBS?wsdl
http:///soap/JHIPLIB.SoAP.BS.StreamService.cls?CfgItem=JH2006绩效系统信息日
1:基于soap协议的接口,传输的数据为xml格式,在对接其他系统接口的时候,接口文档可能会给出的参数格式为json,这时候需要注意,可能说的是xml包json的格式 例如 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:quer="http://query.powerchinasupplier.epoint.com/"> <soapenv:Header/> <soapenv:Body> <quer:SendCbsPjInfo> <!--Optional:--> <quer:sendInfo>{ "PingshenDate": "2022-04-17 00:00:00", "PingshenDateTo": "2022-04-18 15:11:00", "ValidateData": "djsz" }</quer:sendInfo> </quer:SendCbsPjInfo> </soapenv:Body> </soapenv:Envelope> <quer:sendInfo>内部包的是实际的参数,但是必须有外部的xml包起来 在使用C#调用用http请求方式调用接口的时候,需要注意 contenttype = "text/xml;charset=utf-8";
还有就是header头中,需要加入 header.Add("SOAPAction", "http://*********/SendCbsPjInfo");
这里的SOAPAction代表的应该是对面接口的具体地址 SendCbsPjInfo代表的是提供的方法名
如果不知道SOAPAction地址是什么的话,可以通过SoapUI软件查看
SoapUI使用步骤:
1.

2.

3.

4.

这里就可以看到SOAPAction的地址了,拿过来用就行了
附上C#代码:
string url = "接口地址";//如果是?wsdl结尾的 需要把?wsdl删除掉 string contenttype = "text/xml;charset=utf-8"; Hashtable header = new Hashtable(); header.Add("SOAPAction", "上面获取到的SOAPAction地址"); string data = LoadXmls(@"{'PingshenDate': '2022-04-17 00:00:00','PingshenDateTo': '2022-04-18 15:11:00','ValidateData': 'djsz'}");//这里是传递参数,拼接正规的完整xml入参 string filexml = Http(url, "post", contenttype, header, data); XmlDocument doc1 = new XmlDocument(); doc1.LoadXml(filexml); //将获取到的结果加载为xml对象 string model = doc1.InnerText;//直接读取返回值 也就是cdata内部的内容 /// <summary> /// 接口入参拼接 /// </summary> /// <param name="data"></param> /// <returns></returns> public string LoadXmls(string data) { XmlDocument doc1 = new XmlDocument(); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(@"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:quer='http://query.powerchinasupplier.epoint.com/'> <soapenv:Header/> <soapenv:Body> <quer:SendCbsPjInfo> <!--Optional:--> <quer:sendInfo>"); stringBuilder.Append(data); stringBuilder.Append(@"</quer:sendInfo> </quer:SendCbsPjInfo> </soapenv:Body> </soapenv:Envelope> "); return stringBuilder.ToString(); } /// <summary> /// 调用soap协议接口 webservice wsdl /// </summary> /// <param name="url"></param> /// <param name="method"></param> /// <param name="contenttype"></param> /// <param name="header"></param> /// <param name="data"></param> /// <returns></returns> public static string Http(string url, string method = "post", string contenttype = "text/xml;charset=utf-8", Hashtable header = null, string data = null) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = string.IsNullOrEmpty(method) ? "post" : method; request.ContentType = string.IsNullOrEmpty(contenttype) ? "text/xml;charset=utf-8" : contenttype; if (header != null) { foreach (var i in header.Keys) { request.Headers.Add(i.ToString(), header[i].ToString()); } } if (!string.IsNullOrEmpty(data)) { Stream RequestStream = request.GetRequestStream(); byte[] bytes = Encoding.UTF8.GetBytes(data); RequestStream.Write(bytes, 0, bytes.Length); RequestStream.Close(); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream ResponseStream = response.GetResponseStream(); StreamReader StreamReader = new StreamReader(ResponseStream, Encoding.GetEncoding("utf-8")); string re = StreamReader.ReadToEnd(); StreamReader.Close(); ResponseStream.Close(); return re; }