编程语言
首页 > 编程语言> > C# SOAP请求

C# SOAP请求

作者:互联网

 

原文可见:

https://bl(删除)og.csdn.net/(删除)sinat_29009169/article/details(删除)/103917166

 

public static string GetWebSock()
{
#region SOAP请求内容
//int BADSTRALL = int.Parse(StrOpen) + int.Parse(StrShort) + int.Parse(StrLowResistance);
StringBuilder soap = new StringBuilder(); //构造请求报文
soap.Append("<soapenv:Envelope xmlns:soapenv=\"这里填对方的地址\" ");
soap.Append("xmlns:ser=\"一样填对应的地址\">");
soap.Append("<soapenv:Header/>");
soap.Append("<ser:根据对方内容填写1>");
soap.Append("<ser:根据对方内容填写2>");
soap.Append("<字段名>" + 填写字段内容 + "</字段名>"); 
soap.Append("<字段名>" + 填写字段内容 + "</字段名>"); 
soap.Append("<字段名>" + 填写字段内容 + "</字段名>"); 
soap.Append("</ser:根据对方内容填写2>");
soap.Append("</ser:根据对方内容填写1>");
soap.Append("</soapenv:Body>");
soap.Append("</soapenv:Envelope>");
#endregion

//发起请求
Uri uri = new Uri(URL);
string a = ""; ;
try
{
WebRequest webRequest = WebRequest.Create(uri);
webRequest.ContentType = "text/xml; charset=UTF-8";
webRequest.Method = "POST";
//webRequest.Headers.Add("SOAPAction", SOAPACTION);  // SOAPACTION 的值如果对方不为空的话,需要填写对方的,这个由对方提供,如果对方有SOAPUI,直接看其中RAW构成即可
webRequest.Headers.Add("SOAPAction", "");
using (Stream requestStream = webRequest.GetRequestStream())
{
byte[] paramBytes = Encoding.UTF8.GetBytes(soap.ToString());
requestStream.Write(paramBytes, 0, paramBytes.Length);
}

System.Threading.Thread.Sleep(2000);

//响应
WebResponse webResponse = webRequest.GetResponse();
using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
{
//Console.WriteLine(myStreamReader.ReadToEnd());
a += myStreamReader.ReadToEnd();
}
XmlDocument doc = new XmlDocument();
doc.LoadXml(a);

#region 解析方法
//XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
////根据返回内容解析SOAP:
//mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
//mgr.AddNamespace("ns2", "http://service.webservice.services.intmes.com/");
////mgr.AddNamespace("i", "http://jjjj/2001/XMLSchema-instance");
////mgr.AddNamespace("z", "http://jjjj/2003/10/Serialization/");

//var rootNode = doc.SelectNodes("//a:QueryEncounter_Result/*", mgr);
//string name = "";
//foreach (XmlNode xxNode in rootNode)
//{
// string dsf = xxNode.InnerText;
// string sdf = xxNode.Name;
// //name += "|节点名字:" + sdf + "|节点文本:" + dsf + "|节点本地名字" + xxNode.Value;
// name += sdf + ":" + dsf;
//}

//a = name + "---------------------------" + a;
////解析XML
#endregion
}
catch (Exception err)
{
return err.ToString();
}
return a;
}

 

想用的时候,直接调用就可以了

标签:请求,C#,SOAP,mgr,webRequest,new,soap,Append,string
来源: https://www.cnblogs.com/Lee597/p/15679538.html