/// <summary> /// /// </summary> /// <param name="WebServiceInfos">web地址信息</param> /// <param name="MethodName"></param> /// <param name="Para">post 参数hashtable.add("","")</param> /// <param name="contentType">默认application/x-www-form-urlencoded</param> /// <returns>string 可以是json</returns> public static string QueryPostWebServiceReString(WebServiceInfo WebServiceInfos, string MethodName, Hashtable Para, string contentType = "application/x-www-form-urlencoded") { HttpWebRequest request; if (string.IsNullOrWhiteSpace(MethodName)) { request = (HttpWebRequest)HttpWebRequest.Create(WebServiceInfos.WebServiceUrl + "/" + WebServiceInfos.WebServiceName); } else { request = (HttpWebRequest)HttpWebRequest.Create(WebServiceInfos.WebServiceUrl + "/" + WebServiceInfos.WebServiceName + "/" + MethodName); } request.Method = "POST"; request.ContentType = contentType; SetWebRequest(request); byte[] data = EncodePars(Para); WriteRequestData(request, data); string res = string.Empty; using (var s = request.GetResponse().GetResponseStream()) { if (s != null) { using (StreamReader sRead = new StreamReader(s)) { res = sRead.ReadToEnd(); } } } return(res); }
/// <summary> /// 通过SOAP协议调用WebService /// </summary> /// <param name="WebServiceInfos"></param> /// <param name="MethodName"></param> /// <param name="Para"></param> /// <returns></returns> public static XmlDocument QuerySoapWebService(WebServiceInfo WebServiceInfos, string MethodName, Hashtable Para) { if (_xmlNamespaces.ContainsKey(WebServiceInfos.WebServiceUrl + "/" + WebServiceInfos.WebServiceName)) { return(QuerySoapWebService(WebServiceInfos, MethodName, Para, _xmlNamespaces[WebServiceInfos.WebServiceUrl + "/" + WebServiceInfos.WebServiceName].ToString())); } else { return(QuerySoapWebService(WebServiceInfos, MethodName, Para, GetNamespace(WebServiceInfos.WebServiceUrl + "/" + WebServiceInfos.WebServiceName))); } }
/// <summary> /// 采用Get方式调用WebService /// </summary> /// <param name="WebServiceInfos"></param> /// <param name="MethodName"></param> /// <param name="Para"></param> /// <returns></returns> public static XmlDocument QueryGetWebService(WebServiceInfo WebServiceInfos, string MethodName, Hashtable Para) { HttpWebRequest request; if (string.IsNullOrWhiteSpace(MethodName)) { request = (HttpWebRequest)HttpWebRequest.Create(WebServiceInfos.WebServiceUrl + "/" + WebServiceInfos.WebServiceName); } else { request = (HttpWebRequest)HttpWebRequest.Create(WebServiceInfos.WebServiceUrl + "/" + WebServiceInfos.WebServiceName + "/" + MethodName); } request.Method = "GET"; request.ContentType = "application/x-www-form-urlencoded"; SetWebRequest(request); return(ReadXmlResponse(request.GetResponse())); }
/// <summary> /// 采用Post方式调用WebService /// /// </summary> /// <param name="WebServiceInfos"></param> /// <param name="MethodName"></param> /// <param name="Para">string型的参数名和参数值组成的哈希表</param> /// <param name="contentType">application/x-www-form-urlencoded</param> /// <returns></returns> public static XmlDocument QueryPostWebService(WebServiceInfo WebServiceInfos, string MethodName, Hashtable Para, string contentType = "application/x-www-form-urlencoded") { HttpWebRequest request; if (string.IsNullOrWhiteSpace(MethodName)) { request = (HttpWebRequest)HttpWebRequest.Create(WebServiceInfos.WebServiceUrl + "/" + WebServiceInfos.WebServiceName); } else { request = (HttpWebRequest)HttpWebRequest.Create(WebServiceInfos.WebServiceUrl + "/" + WebServiceInfos.WebServiceName + "/" + MethodName); } request.Method = "POST"; request.ContentType = contentType; SetWebRequest(request); byte[] data = EncodePars(Para); WriteRequestData(request, data); return(ReadXmlResponse(request.GetResponse())); }
/// <summary> /// /// </summary> /// <param name="WebServiceInfos"></param> /// <param name="MethodName"></param> /// <param name="Para"></param> /// <param name="XmlNs"></param> /// <returns></returns> private static XmlDocument QuerySoapWebService(WebServiceInfo WebServiceInfos, String MethodName, Hashtable Para, string XmlNs) { _xmlNamespaces[WebServiceInfos.WebServiceUrl + "/" + WebServiceInfos.WebServiceName] = XmlNs;//加入缓存,提高效率 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(WebServiceInfos.WebServiceUrl + "/" + WebServiceInfos.WebServiceName + ".asmx"); request.Method = "POST"; request.ContentType = "text/xml; charset=utf-8"; request.Headers.Add("SOAPAction", "\"" + XmlNs + (XmlNs.EndsWith("/") ? "" : "/") + MethodName + "\""); SetWebRequest(request); byte[] data = EncodeParsToSoap(Para, XmlNs, MethodName); WriteRequestData(request, data); XmlDocument doc = new XmlDocument(), doc2 = new XmlDocument(); doc = ReadXmlResponse(request.GetResponse()); XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable); mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/"); String RetXml = doc.SelectSingleNode("//soap:Body/*/*", mgr).InnerXml; doc2.LoadXml("<root>" + RetXml + "</root>"); AddDelaration(doc2); return(doc2); }