定义 soap 1.1 post 调用webservice
public static XmlDocument Test3_QuerySoapWebService(String URL,
String MethodName,
Hashtable Pars)
{
#region 获取命名空间
HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(URL + "?WSDL");
request1.Credentials = CredentialCache.DefaultCredentials;
request1.Timeout = 10000;
WebResponse response = request1.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
XmlDocument doc1 = new XmlDocument();
doc1.LoadXml(sr.ReadToEnd());
sr.Close();
var XmlNs = doc1.SelectSingleNode("//@targetNamespace").Value;
#endregion
#region 请求信息
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
request.Method = "POST";
request.ContentType = "text/xml; charset=utf-8 ";
request.Headers.Add("SOAPAction", XmlNs + MethodName);
request.Credentials = CredentialCache.DefaultCredentials;
request.Timeout = 10000;
#endregion
#region 要传入的参数,是xml文档对象(http post,http get 是字符串)
XmlDocument doc = new XmlDocument();
doc.LoadXml(
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" >" +
"</soap:Envelope>"
);
XmlDeclaration dec2 = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.InsertBefore(dec2, doc.DocumentElement);
XmlElement soapBody = doc.CreateElement("soap",
"Body", "http://schemas.xmlsoap.org/soap/envelope/");
XmlElement soapMethod = doc.CreateElement(MethodName);
soapMethod.SetAttribute("xmlns", XmlNs);
foreach (string k in Pars.Keys)
{
XmlElement soapPar = doc.CreateElement(k);
soapPar.InnerXml = ObjectToSoapXml(Pars[k]);
soapMethod.AppendChild(soapPar);
}
soapBody.AppendChild(soapMethod);
doc.DocumentElement.AppendChild(soapBody);
byte[] data = Encoding.UTF8.GetBytes(doc.OuterXml);
#endregion
#region 把参数写入请求流中
request.ContentLength = data.Length;
Stream writer = request.GetRequestStream();
writer.Write(data, 0, data.Length);
writer.Close();
#endregion
#region 读取响应内容
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
response = (HttpWebResponse)ex.Response;
var sr1 = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
var strHtml = sr1.ReadToEnd();
}
XmlDocument doca = ReadXmlResponse(response);
return doca;
//XmlNamespaceManager mgr = new XmlNamespaceManager(doca.NameTable);
//mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
//String RetXml = doca.SelectSingleNode("//soap:Body/*/*", mgr).InnerXml;
//XmlDocument docb = new XmlDocument();
//docb.LoadXml("<root>" + RetXml + "</root>");
//XmlDeclaration decl = docb.CreateXmlDeclaration("1.0", "utf-8", null);
//docb.InsertBefore(decl, docb.DocumentElement);
//return docb;
#endregion
}
调用示例:
protected void Page_Load(object sender, EventArgs e)
{
Hashtable pars = new Hashtable();
String Url = "http://localhost:63596/WebService1.asmx";
pars["aa"] = "HelenZhou";
XmlDocument doc = WebSvcCaller.
Test3_QuerySoapWebService(Url,
"MyHelloWorld",
pars);
Response.Write(doc.OuterXml);
}