手动调用web服务
Posted xuexiaodong2009
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了手动调用web服务相关的知识,希望对你有一定的参考价值。
/// <summary>
/// 查询M得到数据
/// </summary>
/// <param name="ClassName">类名</param>
/// <param name="FuncName">方法名</param>
/// <param name="Param">参数</param>
/// <returns></returns>
private string getDataComm(string ClassName, string FuncName, params string[] parameters)
Dictionary<string, string> parameterDic = new Dictionary<string, string>();
for (int i = 0; i < parameters.Length; i++)
parameterDic["parameter" + (i + 1)] = parameters[i];
string Param = JsonConvert.SerializeObject(parameterDic);
StringBuilder xmlsb = new StringBuilder();
xmlsb.Append("<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>");
xmlsb.Append("<soap:Envelope xmlns:soap=\\"http://schemas.xmlsoap.org/soap/envelope/\\" xmlns:xsi=\\"http://www.w3.org/2001/XMLSchema-instance\\" xmlns:xsd=\\"http://www.w3.org/2001/XMLSchema\\" xmlns:wsa=\\"http://schemas.xmlsoap.org/ws/2004/08/addressing\\" xmlns:wsse=\\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\\" xmlns:wsu=\\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\\">");
xmlsb.Append("<soap:Header>");
xmlsb.Append("<wsa:Action>http://tempuri.org/NurMp.Service.WebService.GetData.Excute</wsa:Action>");//需要和服务端对应的NAMESPACE
xmlsb.Append("<wsa:ReplyTo>");
xmlsb.Append("<wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address>");
xmlsb.Append("</wsa:ReplyTo> ");
xmlsb.Append("<wsa:To>" + WebServiceURI + "</wsa:To>");//web服务的地址
xmlsb.Append("<wsse:Security soap:mustUnderstand=\\"1\\">");
xmlsb.Append("<wsse:UsernameToken>");
xmlsb.Append("<wsse:Username>" + XMLSpecialOP(username) + "</wsse:Username>");//web服务的用户名
xmlsb.Append("<wsse:Password>" + XMLSpecialOP(password) + "</wsse:Password>");//web服务的密码
xmlsb.Append("</wsse:UsernameToken>");
xmlsb.Append("</wsse:Security>");
xmlsb.Append("</soap:Header>");
xmlsb.Append("<soap:Body>");
xmlsb.Append(" <Excute xmlns=\\"http://tempuri.org\\">");//web服务的参数
xmlsb.Append(" <clsName>" + ClassName + "</clsName>");
xmlsb.Append(" <methodName>" + FuncName + "</methodName>");
xmlsb.Append("<parameters><![CDATA[" + Param + "]]></parameters>");
xmlsb.Append(" </Excute>");
xmlsb.Append("</soap:Body>");
xmlsb.Append("</soap:Envelope>");
HttpContent content = new StringContent(xmlsb.ToString(), Encoding.UTF8, "text/xml");
content.Headers.Add("SOAPAction", "http://tempuri.org/NurMp.Service.WebService.GetData.Excute");//需要和服务端对应的NAMESPACE
return PostData(content);
private string PostData(HttpContent content)
string result = string.Empty;
try
var handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback =
(
HttpRequestMessage message,
X509Certificate2 cert,
X509Chain chain,
SslPolicyErrors errors
) =>
return true; ;//remove if this makes it to production
using (HttpClient client = ISHTTPS? new HttpClient(handler):new HttpClient ())
client.Timeout = TimeSpan.FromSeconds(WebserviceRequestTimeOut);
using (var response = client.PostAsync(WebServiceURI, content))
result = response.Result.Content.ReadAsStringAsync().Result;
//创建一个xml文档
XmlDocument xmlDoc = new XmlDocument();
//为文档导入数据
xmlDoc.LoadXml(result);
result = xmlDoc.InnerText;
return result;
catch (Exception ex)
throw new Exception("调用服务异常:" + ex.Message);
return result;
/// <summary>
/// xml中特殊字符处理
/// </summary>
/// <param name="Input"></param>
/// <returns></returns>
private string XMLSpecialOP(string Input)
return Input.Replace("&", "&")
.Replace("'", "'")
.Replace("\\"", """)
.Replace(">", ">")
.Replace("<", "<");
private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
return true;
以上是关于手动调用web服务的主要内容,如果未能解决你的问题,请参考以下文章