C#调用Java的WebService添加SOAPHeader验证
C#调用Java的WebService添加SOAPHeader验证(2)
1.问题描述
调用的Java的webservice
string Invoke(string func, string reqXml)
使用C#直接调用一直报错。
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这些东西的
网上查了好多类似下面的
https://www.cnblogs.com/o2ds/p/4093413.html
C#访问Java的WebService添加SOAPHeader验证的问题
都没有用
后来尝试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的方式: