//WebServices请求
#region RequestSoap
private Return RequestSoap(string tranData,string url )
{
StringBuilder soap = new StringBuilder();
soap.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
soap.Append(
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
soap.Append("<soap:Body>");
soap.Append("<SendRequestPack xmlns=\"http://tempuri.org/\">");//接口名称
soap.AppendFormat("<tranData>{0}</tranData>", tranData);//参数
soap.Append("</SendRequestPack >");
soap.Append("</soap:Body>");
soap.Append("</soap:Envelope>");
try
{
//发起请求
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(soap.ToString());
requestStream.Write(paramBytes, 0, paramBytes.Length);
requestStream.Close();
}
var result = string.Empty;
//响应
using (WebResponse webResponse = webRequest.GetResponse())
{
using (Stream rsStream = webResponse.GetResponseStream())
{
if (rsStream != null)
{
using (var streamReader = new StreamReader(rsStream, Encoding.UTF8))
{
result = streamReader.ReadToEnd();
streamReader.Close();
}
rsStream.Close();
}
}
webResponse.Close();
}
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(result);
//var root = xmlDoc.DocumentElement;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
var selectSingleNode = xmlDoc.SelectSingleNode("//soap:Envelope", nsmgr);
if (selectSingleNode != null)
{
var value = selectSingleNode.InnerText;
return JsonConvert.DeserializeObject<Return>(value);
}
return new Return();
}
catch (Exception ex)
{
return new Return();
}
}
#endregion
版权声明:本文为u013314974原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。