欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#動(dòng)態(tài)調(diào)用Webservice的兩種方法實(shí)例

 更新時(shí)間:2013年08月26日 15:57:34   作者:  
這篇文章介紹了c#動(dòng)態(tài)調(diào)用Webservice的兩種方法實(shí)例,有需要的朋友可以參考一下

方法一:

復(fù)制代碼 代碼如下:

Hashtable ht = new Hashtable();
            ht.Add("a", "testhelloworld");
            XmlDocument xx = WebServicesHelper.QuerySoapWebService("http://www.dbjr.com.cn/elab_mgmt/WorkflowSchemeTaskSerivce.asmx", "ATesting", ht);
            string ss = xx.OuterXml;

復(fù)制代碼 代碼如下:

/// <summary>
        /// 通用WebService調(diào)用(Soap),參數(shù)Pars為String類型的參數(shù)名、參數(shù)值
        /// </summary>
        public static XmlDocument QuerySoapWebService(String URL, String MethodName, Hashtable Pars)
        {
            if (_xmlNamespaces.ContainsKey(URL))
            {
                return QuerySoapWebService(URL, MethodName, Pars, _xmlNamespaces[URL].ToString());
            }
            else
            {
                return QuerySoapWebService(URL, MethodName, Pars, GetNamespace(URL));
            }
        }

復(fù)制代碼 代碼如下:

private static XmlDocument QuerySoapWebService(String URL, String MethodName, Hashtable Pars, string XmlNs)
        {
            _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 = 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("http://soap:Body/*/*", mgr).InnerXml;
            doc2.LoadXml("<root>" + RetXml + "</root>");
            AddDelaration(doc2);
            return doc2;
        }

復(fù)制代碼 代碼如下:

private static string GetNamespace(String URL)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL + "?WSDL");
            SetWebRequest(request);
            WebResponse response = request.GetResponse();
            StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(sr.ReadToEnd());
            sr.Close();
            return doc.SelectSingleNode("http://@targetNamespace").Value;
        }

方法二:

通過SOAPUI直接取URL

復(fù)制代碼 代碼如下:

string postData2="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\"><soapenv:Header/><soapenv:Body><tem:ATesting><!--Optional:--><tem:a>?</tem:a></tem:ATesting></soapenv:Body></soapenv:Envelope>";
            HttpHelper.GetResponseFormUrlAsync("http://www.xxx.com/testingservices.asmx?wsdl", postData2, "text/xml", true, new AsyncCallback(responseCallback));

復(fù)制代碼 代碼如下:

}
        static void responseCallback(IAsyncResult ar)
        {
            HttpWebRequest req = ar.AsyncState as HttpWebRequest;
            if (req == null)
                return;
            try
            {
                HttpWebResponse response = req.EndGetResponse(ar) as HttpWebResponse;
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    response.Close();
                    LogHelper.Error("定時(shí)任務(wù)", "異步執(zhí)行失敗," + req.RequestUri.ToString() + "\r\nResponse狀態(tài)代碼為\r\n" + response.StatusCode.ToString());
                    return;
                }
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                string ResponseStr = reader.ReadToEnd();
                responseStream.Close();
                LogHelper.Warn("定時(shí)任務(wù)", req.RequestUri.ToString() + "\r\n" + ResponseStr);
            }
            catch (Exception e)
            {
                LogHelper.Fatal("定時(shí)任務(wù)", req.RequestUri.ToString() + "\r\n執(zhí)行失敗", e);
            }
        }

相關(guān)文章

最新評(píng)論