c#動(dòng)態(tài)調(diào)用Webservice的兩種方法實(shí)例
方法一:
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;
/// <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));
}
}
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;
}
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
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));
}
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);
}
}
- C# 動(dòng)態(tài)調(diào)用WebService的示例
- C# 調(diào)用WebService的方法
- c# 三種方法調(diào)用WebService接口
- c#中WebService的介紹及調(diào)用方式小結(jié)
- C#調(diào)用WebService實(shí)例與開發(fā)教程(推薦)
- C#創(chuàng)建、部署、調(diào)用WebService圖文實(shí)例詳解
- C#調(diào)用webservice接口的最新方法教程
- C# 創(chuàng)建、部署和調(diào)用WebService簡(jiǎn)單示例
- C#調(diào)用WebService實(shí)例開發(fā)
- C#動(dòng)態(tài)webservice調(diào)用接口
- C#調(diào)用WebService的方法介紹
相關(guān)文章
C#先判斷是否存在再創(chuàng)建文件夾或文件與遞歸計(jì)算文件夾大小
這篇文章介紹了C#先判斷是否存在再創(chuàng)建文件夾或文件與遞歸計(jì)算文件夾大小的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07C# Winform按鈕中圖片實(shí)現(xiàn)左圖右字的效果實(shí)例
這篇文章主要給大家介紹了關(guān)于C# Winform按鈕中圖片實(shí)現(xiàn)左圖右字效果的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11C#使用分部類設(shè)計(jì)實(shí)現(xiàn)一個(gè)計(jì)算器
分部類是C#4.5中的一個(gè)新特性,它的出現(xiàn)使得程序的結(jié)構(gòu)更加合理,代碼組織更加緊密,本文將使用分部類設(shè)計(jì)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的計(jì)算器,感興趣的小伙伴可以了解下2024-02-02C#中數(shù)組、ArrayList和List三者的區(qū)別詳解
這篇文章主要介紹了C#中數(shù)組、ArrayList和List三者的區(qū)別詳解,對(duì)于三者之間的區(qū)別想要了解的可以進(jìn)來了解一下。2016-12-12C#運(yùn)行程序時(shí)阻止關(guān)閉顯示器和系統(tǒng)待機(jī)
這篇文章介紹了C#運(yùn)行程序時(shí)阻止關(guān)閉顯示器和系統(tǒng)待機(jī)的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06如何在UpdatePanel中調(diào)用JS客戶端腳本
本文將介紹如何在UpdatePanel中調(diào)用JS客戶端腳本,需要了解的朋友可以參考下2012-12-12