博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c#动态调用Webservices
阅读量:5273 次
发布时间:2019-06-14

本文共 3814 字,大约阅读时间需要 12 分钟。

方式一:

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

 

///         /// 通用WebService调用(Soap),参数Pars为String类型的参数名、参数值        ///         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("//soap:Body/*/*", mgr).InnerXml;            doc2.LoadXml("
" + RetXml + "
"); 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("//@targetNamespace").Value;        }

 

 

方式二:

通过SOAPUI直接取URL

string postData2="
?
"; 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("定时任务", "异步执行失败," + req.RequestUri.ToString() + "\r\nResponse状态代码为\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("定时任务", req.RequestUri.ToString() + "\r\n" + ResponseStr);            }            catch (Exception e)            {                LogHelper.Fatal("定时任务", req.RequestUri.ToString() + "\r\n执行失败", e);            }        }
View Code

 

转载于:https://www.cnblogs.com/8090sns/p/3282562.html

你可能感兴趣的文章
Java 中 静态方法与非静态方法的区别
查看>>
echarts饼图显示百分比
查看>>
JMS消息
查看>>
Jenkins+ProGet+Windows Batch搭建全自动的内部包(NuGet)打包和推送及管理平台
查看>>
php上传文件及头像预览
查看>>
大四java实习生的一些经历
查看>>
线程池的概念
查看>>
Oracle_Statspack性能诊断工具
查看>>
转获取sql维护的表关系
查看>>
Java 序列化
查看>>
Java 时间处理实例
查看>>
Java 多线程编程
查看>>
Java 数组实例
查看>>
mysql启动过程
查看>>
2017前端面试题总结
查看>>
Http GetPost网络请求
查看>>
SWIFT国际资金清算系统
查看>>
Sping注解:注解和含义
查看>>
站立会议第四天
查看>>
如何快速掌握一门技术
查看>>