標籤:
經常發現這類問題,主要是返回的資料轉換問題造成,一般情況下不要直接引用wsdl的方式(如果的調用.Net開發的webservice可以),用HttpWebRequest方式擷取返回的資料,然後再解析,這種方式比引用wsdl成功率高,至於如何傳參數,可以用
SoapUI工具進行分析,能看到該怎樣傳參數
還有用HTTPAnalyzerFull工具可以進行抓包,看看提交和返回的是什麼內容
不管是.net 調用java或者Java調用.net,一般用以上兩個工具都能解決問題
public string GetGsReturn(string url, StringBuilder param)//提交到webservice並返回結果
{
string responseString = string.Empty;
try
{
byte[] bs = Encoding.UTF8.GetBytes(param.ToString());
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "post";
myRequest.Headers.Add("SOAPAction", "\"\"");
myRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
myRequest.ContentType = "text/xml; charset=utf-8";
myRequest.KeepAlive = true;
myRequest.ContentLength = bs.Length;
myRequest.UserAgent = "Apache-HttpClient/4.1.1 (java 1.5)";
Stream reqStream = myRequest.GetRequestStream();
reqStream.Write(bs, 0, bs.Length);
HttpWebResponse myResponse;
try
{ myResponse = (HttpWebResponse)myRequest.GetResponse(); }
catch (WebException ex)
{ myResponse = (HttpWebResponse)ex.Response; }
if (myRequest != null)
{
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
responseString = sr.ReadToEnd();//返回的資料
}
}
catch (Exception ex)
{
responseString = ex.Message;
}
return responseString;
}
一個參數用例,具體看你的實際需求
StringBuilder param = new StringBuilder();
param.AppendLine("<?xml version=‘1.0‘ encoding=‘utf-8‘?>");
param.AppendLine("<soapenv:Envelope xmlns:xsi=‘http://www.w3.org/2001/XMLSchema-instance‘ xmlns:xsd=‘http://www.w3.org/2001/XMLSchema‘ xmlns:soapenv=‘http://schemas.xmlsoap.org/soap/envelope/‘ xmlns:web=‘http://webservice.protal.ems.com‘>");
param.AppendLine("<soapenv:Header/>");
param.AppendLine("<soapenv:Body>");
param.AppendLine("<web:sendSMS soapenv:encodingStyle=‘http://schemas.xmlsoap.org/soap/encoding/‘>");
param.AppendLine("<enterpriseID xsi:type=‘xsd:string‘>15644</enterpriseID>");
param.AppendLine("<loginName xsi:type=‘xsd:string‘>admin</loginName>");
param.AppendLine("<password xsi:type=‘xsd:string‘>511c6269b5eb69538c71ab372529fb1d</password>"); //741123
param.AppendLine("<smsId xsi:type=‘xsd:string‘></smsId>");
param.AppendLine("<subPort xsi:type=‘xsd:string‘></subPort>");
param.AppendLine("<content xsi:type=‘xsd:string‘>" + smstxt + "</content>");
param.AppendLine("<mobiles xsi:type=‘xsd:string‘>" + tels + "</mobiles>");
param.AppendLine("<sendTime xsi:type=‘xsd:string‘></sendTime>");
param.AppendLine("</web:sendSMS>");
param.AppendLine("</soapenv:Body>");
param.AppendLine("</soapenv:Envelope>");
string rtnstr = GetGsReturn(SmsUrl, param);//傳送參數並返回結果
.net 調用java或者Java調用.net返回的資料轉換問題