mysql触发器调用webservice_WebService调用

本文档展示了如何使用C#的HttpWebRequest类通过Post、Get和Soap方式调用WebService。详细介绍了设置请求、编码参数、读取响应数据的步骤,并提供了缓存命名空间以提高效率的实现。

usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Net;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Xml;usingSystem.Xml.Serialization;namespaceNetWebServiceInvoke

{///

///通过HttpWebRequest调用WebService///

public classInvokeByHttpWebRequest

{#region 定义

///

///缓存xmlNamespace,避免重复调用GetNamespace///

private static Hashtable _xmlNamespaces = newHashtable();#endregion

#region 调用方式

///

///Post方式///

/// 统一资源定位符

/// 方法名

/// 参数

/// xml文档

public staticXmlDocument QueryPostWebService(String URL, String MethodName, Hashtable Pars)

{

HttpWebRequest request= (HttpWebRequest)HttpWebRequest.Create(URL + "/" +MethodName);

request.Method= "POST";

request.ContentType= "application/x-www-form-urlencoded";

SetWebRequest(request);byte[] data =EncodePars(Pars);

WriteRequestData(request, data);returnReadXmlResponse(request.GetResponse());

}///

///Get方式///

/// 统一资源定位符

/// 方法名

/// 参数

/// xml文档

public staticXmlDocument QueryGetWebService(String URL, String MethodName, Hashtable Pars)

{

HttpWebRequest request= (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName + "?" +ParsToString(Pars));

request.Method= "GET";

request.ContentType= "application/x-www-form-urlencoded";

SetWebRequest(request);returnReadXmlResponse(request.GetResponse());

}///

///Soap方式///

/// 统一资源定位符

/// 方法名

/// 参数

/// xml文档

public staticXmlDocument QuerySoapWebService(String URL, String MethodName, Hashtable Pars)

{if(_xmlNamespaces.ContainsKey(URL))

{returnQuerySoapWebService(URL, MethodName, Pars, _xmlNamespaces[URL].ToString());

}else{returnQuerySoapWebService(URL, MethodName, Pars, GetNamespace(URL));

}

}///

///Soap方式///

/// 统一资源定位符

/// 方法名

/// 参数

/// xml命名空间

/// xml文档

private static XmlDocument QuerySoapWebService(String URL, String MethodName, Hashtable Pars, stringXmlNs)

{

_xmlNamespaces[URL]= XmlNs;//加入缓存,提高效率

HttpWebRequest request =(HttpWebRequest)HttpWebRequest.Create(URL);

request.Method= "POST";

request.ContentType= "text/xml; charset=utf-8";

request.Headers.Add("SOAPAction", "\"" + XmlNs + (XmlNs.EndsWith("/") ? "" : "/") + MethodName + "\"");

SetWebRequest(request);byte[] data =EncodeParsToSoap(Pars, XmlNs, MethodName);

WriteRequestData(request, data);

XmlDocument doc= new XmlDocument(), doc2 = newXmlDocument();

doc=ReadXmlResponse(request.GetResponse());

XmlNamespaceManager mgr= newXmlNamespaceManager(doc.NameTable);

mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");

String RetXml= doc.SelectSingleNode("//soap:Body/*/*", mgr).InnerXml;

doc2.LoadXml("" + RetXml + "");

AddDelaration(doc2);returndoc2;

}#endregion

#region 设置

///

///获得命名空间///

/// 统一资源定位符

///

private static stringGetNamespace(String URL)

{

HttpWebRequest request= (HttpWebRequest)WebRequest.Create(URL + "?WSDL");

SetWebRequest(request);

WebResponse response=request.GetResponse();

StreamReader sr= newStreamReader(response.GetResponseStream(), Encoding.UTF8);

XmlDocument doc= newXmlDocument();

doc.LoadXml(sr.ReadToEnd());

sr.Close();return doc.SelectSingleNode("//@targetNamespace").Value;

}///

///设置凭证与超时时间///

/// 请求

private static voidSetWebRequest(HttpWebRequest request)

{

request.Credentials=CredentialCache.DefaultCredentials;

request.Timeout= 10000;

}///

///写入请求数据///

/// 请求

/// 数据

private static void WriteRequestData(HttpWebRequest request, byte[] data)

{

request.ContentLength=data.Length;

Stream writer=request.GetRequestStream();

writer.Write(data,0, data.Length);

writer.Close();

}#endregion

#region 参数

///

///参数编码///

/// 参数

/// 字节数组

private static byte[] EncodePars(Hashtable Pars)

{returnEncoding.UTF8.GetBytes(ParsToString(Pars));

}///

///参数编码///

/// 参数

///

private staticString ParsToString(Hashtable Pars)

{

StringBuilder sb= newStringBuilder();foreach (string k inPars.Keys)

{if (sb.Length > 0)

{

sb.Append("&");

}//sb.Append(HttpUtility.UrlEncode(k) + "=" + HttpUtility.UrlEncode(Pars[k].ToString()));

}returnsb.ToString();

}///

///Soap参数编码///

/// 参数

/// xml命名空间

/// 方法

/// 字节数组

private static byte[] EncodeParsToSoap(Hashtable Pars, String XmlNs, String MethodName)

{

XmlDocument doc= newXmlDocument();

doc.LoadXml("");

AddDelaration(doc);//XmlElement soapBody = doc.createElement_x_x("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/");

XmlElement soapBody = doc.CreateElement("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/");//XmlElement soapMethod = doc.createElement_x_x(MethodName);

XmlElement soapMethod =doc.CreateElement(MethodName);

soapMethod.SetAttribute("xmlns", XmlNs);foreach (string k inPars.Keys)

{//XmlElement soapPar = doc.createElement_x_x(k);

XmlElement soapPar =doc.CreateElement(k);

soapPar.InnerXml=ObjectToSoapXml(Pars[k]);

soapMethod.AppendChild(soapPar);

}

soapBody.AppendChild(soapMethod);

doc.DocumentElement.AppendChild(soapBody);returnEncoding.UTF8.GetBytes(doc.OuterXml);

}///

///obj序列化xml///

///

/// 结果

private static string ObjectToSoapXml(objectobj)

{

XmlSerializer mySerializer= newXmlSerializer(obj.GetType());

MemoryStream ms= newMemoryStream();

mySerializer.Serialize(ms, obj);

XmlDocument doc= newXmlDocument();

doc.LoadXml(Encoding.UTF8.GetString(ms.ToArray()));if (doc.DocumentElement != null)

{returndoc.DocumentElement.InnerXml;

}else{returnobj.ToString();

}

}#endregion

#region 响应数据

///

///响应数据读取///

/// 响应

/// xml文档

private staticXmlDocument ReadXmlResponse(WebResponse response)

{

StreamReader sr= newStreamReader(response.GetResponseStream(), Encoding.UTF8);

String retXml=sr.ReadToEnd();

sr.Close();

XmlDocument doc= newXmlDocument();

doc.LoadXml(retXml);returndoc;

}///

///xml文档添加声明///

/// xml文档

private static voidAddDelaration(XmlDocument doc)

{

XmlDeclaration decl= doc.CreateXmlDeclaration("1.0", "utf-8", null);

doc.InsertBefore(decl, doc.DocumentElement);

}#endregion }

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值