C#調用Java的WebService添加SOAPHeader驗證(2)html
1.問題描述web
調用的Java的webservice函數
string Invoke(string func, string reqXml)post
使用C#直接調用一直報錯。ui
webservice提供方有說明以下:編碼
身份驗證採用對SOAP身份認證(用戶名/密碼驗證/序列號)的方式部署,設定用戶名和密碼由系統配置,全部文本內容編碼選擇UTF-8編碼規範 <?xml version='1.0' encoding='utf-8'?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header> <vc:Authentication xmlns: vc ="http://ant.com"> <vc:Username>[系統配置] </vc:Username> <vc:Password>[系統配置]</vc:Password> <vc:SerialNo>[系統配置]</vc:SerialNo > </vc:Authentication> </soapenv:Header> <soapenv:Body> </soapenv:Body> </soapenv:Envelope>
相信就是soapenv:Header這裏的問題了,C# 沒soapenv:Header這些東西的url
網上查了好多相似下面的spa
https://www.cnblogs.com/o2ds/p/4093413.htmlcode
都沒有用xml
後來嘗試sopui及xmlspy建立soap,直接發送xml,終於試出來了,而後C#使用http post調用webservice,成功了。
2.問題解決1
C#拼接的須要http post的soap字符串以下
</SOAP-ENV:Header> 照搬給的文檔裏的字符串
<SOAP-ENV:Body> 爲調用函數,string Invoke(string func, string reqXml) 函數名Invoke,兩個參數,自行理解吧
注意:裏面的\r\n換行標誌都要保留,否則都是報錯
string soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + "<SOAP-ENV:Envelope\r\n" + "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"\r\n" + "xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"\r\n" + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n" + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n" + "<SOAP-ENV:Header>\r\n" + "<vc:Authentication\r\n" + "xmlns:vc=\"http://ant.com\">\r\n" + "<vc:Username>xx</vc:Username>\r\n" + "<vc:Password>xxx</vc:Password>\r\n" + "<vc:SerialNo>xxxx</vc:SerialNo>\r\n" + "</vc:Authentication>\r\n" + "</SOAP-ENV:Header>\r\n" + "<SOAP-ENV:Body>\r\n" + "<m:Invoke\r\n" + "xmlns:m=\"http://tempuri.org/\">\r\n" + "<m:func>" + jkid + "</m:func>\r\n" + "<m:reqXml>" + HttpUtility.HtmlEncode(xml) + "</m:reqXml>\r\n" + "</m:Invoke>\r\n" + "</SOAP-ENV:Body>\r\n" + "</SOAP-ENV:Envelope>";
而後發送,隨便找個http post的代碼就好了
public static string GetSOAPReSource(string url, string datastr) { try { //request Uri uri = new Uri(url); 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(datastr.ToString()); requestStream.Write(paramBytes, 0, paramBytes.Length); } //response WebResponse webResponse = webRequest.GetResponse(); using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8)) { string result = ""; return result = myStreamReader.ReadToEnd(); } } catch (Exception ex) { throw ex; } }
3.問題解決2
發現仍是報錯,http 500錯誤,和以前不同,但依然不對
研究webservice的wsdl發現了問題
調用時加上
webRequest.Headers.Add("SOAPAction", "http://tempuri.org/IAjsjService/Invoke");
終於成功了
public static string GetSOAPReSource(string url, string datastr)
{
try
{
//request
Uri uri = new Uri(url);
WebRequest webRequest = WebRequest.Create(uri);
webRequest.ContentType = "text/xml; charset=utf-8";
webRequest.Headers.Add("SOAPAction", "http://tempuri.org/IAjsjService/Invoke");
webRequest.Method = "POST";
using (Stream requestStream = webRequest.GetRequestStream())
{
byte[] paramBytes = Encoding.UTF8.GetBytes(datastr.ToString());
requestStream.Write(paramBytes, 0, paramBytes.Length);
}
//response
WebResponse webResponse = webRequest.GetResponse();
using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
{
string result = "";
return result = myStreamReader.ReadToEnd();
}
}
catch (Exception ex)
{
throw ex;
}
}
其餘調用webservice的方式: