1         #region Tip:使用说明
  2         //webServices 应该支持Get和Post调用,在web.config应该增加以下代码
  3         //<webServices>
  4         //  <protocols>
  5         //    <add name="HttpGet"/>
  6         //    <add name="HttpPost"/>
  7         //  </protocols>
  8         //</webServices>
  9
 10         //调用示例:
 11         //Hashtable ht = new Hashtable();  //Hashtable 为webservice所需要的参数集
 12         //ht.Add("str", "test");
 13         //ht.Add("b", "true");
 14         //XmlDocument xx = WebSvcCaller.QuerySoapWebService("http://localhost:81/service.asmx", "HelloWorld", ht);
 15         //MessageBox.Show(xx.OuterXml);
 16         #endregion
 17
 18         /// <summary>
 19         /// 需要WebService支持Post调用
 20         /// </summary>
 21         public static XmlDocument QueryPostWebService(string URL, string MethodName, Hashtable Pars)
 22         {
 23             HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName);
 24             request.Method = "POST";
 25             request.ContentType = "application/x-www-form-urlencoded";
 26             SetWebRequest(request);
 27             byte[] data = EncodePars(Pars);
 28             WriteRequestData(request, data);
 29             return ReadXmlResponse(request.GetResponse());
 30         }
 31
 32         /// <summary>
 33         /// 需要WebService支持Get调用
 34         /// </summary>
 35         public static XmlDocument QueryGetWebService(String URL, String MethodName, Hashtable Pars)
 36         {
 37             HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName + "?" + ParsToString(Pars));
 38             request.Method = "GET";
 39             request.ContentType = "application/x-www-form-urlencoded";
 40             SetWebRequest(request);
 41             return ReadXmlResponse(request.GetResponse());
 42         }
 43
 44         /// <summary>
 45         /// 通用WebService调用(Soap),参数Pars为String类型的参数名、参数值
 46         /// </summary>
 47         public static XmlDocument QuerySoapWebService(String URL, String MethodName, Hashtable Pars)
 48         {
 49             if (_xmlNamespaces.ContainsKey(URL))
 50             {
 51                 return QuerySoapWebService(URL, MethodName, Pars, _xmlNamespaces[URL].ToString());
 52             }
 53             else
 54             {
 55                 return QuerySoapWebService(URL, MethodName, Pars, GetNamespace(URL));
 56             }
 57         }
 58
 59         private static XmlDocument QuerySoapWebService(String URL, String MethodName, Hashtable Pars, string XmlNs)
 60         {
 61             _xmlNamespaces[URL] = XmlNs;//加入缓存,提高效率
 62             HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
 63             request.Method = "POST";
 64             request.ContentType = "text/xml; charset=utf-8";
 65             request.Headers.Add("SOAPAction", "\"" + XmlNs + (XmlNs.EndsWith("/") ? "" : "/") + MethodName + "\"");
 66             SetWebRequest(request);
 67             byte[] data = EncodeParsToSoap(Pars, XmlNs, MethodName);
 68             WriteRequestData(request, data);
 69             XmlDocument doc = new XmlDocument(), doc2 = new XmlDocument();
 70             doc = ReadXmlResponse(request.GetResponse());
 71
 72             XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
 73             mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
 74             String RetXml = doc.SelectSingleNode("//soap:Body/*/*", mgr).InnerXml;
 75             doc2.LoadXml("<root>" + RetXml + "</root>");
 76             AddDelaration(doc2);
 77             return doc2;
 78         }
 79         private static string GetNamespace(String URL)
 80         {
 81             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL + "?WSDL");
 82             SetWebRequest(request);
 83             WebResponse response = request.GetResponse();
 84             StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
 85             XmlDocument doc = new XmlDocument();
 86             doc.LoadXml(sr.ReadToEnd());
 87             sr.Close();
 88             return doc.SelectSingleNode("//@targetNamespace").Value;
 89         }
 90
 91         private static byte[] EncodeParsToSoap(Hashtable Pars, String XmlNs, String MethodName)
 92         {
 93             XmlDocument doc = new XmlDocument();
 94             doc.LoadXml("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"></soap:Envelope>");
 95             AddDelaration(doc);
 96             //XmlElement soapBody = doc.createElement_x_x("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/");
 97             XmlElement soapBody = doc.CreateElement("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/");
 98             //XmlElement soapMethod = doc.createElement_x_x(MethodName);
 99             XmlElement soapMethod = doc.CreateElement(MethodName);
100             soapMethod.SetAttribute("xmlns", XmlNs);
101             foreach (string k in Pars.Keys)
102             {
103                 //XmlElement soapPar = doc.createElement_x_x(k);
104                 XmlElement soapPar = doc.CreateElement(k);
105                 soapPar.InnerXml = ObjectToSoapXml(Pars[k]);
106                 soapMethod.AppendChild(soapPar);
107             }
108             soapBody.AppendChild(soapMethod);
109             doc.DocumentElement.AppendChild(soapBody);
110             return Encoding.UTF8.GetBytes(doc.OuterXml);
111         }
112         private static string ObjectToSoapXml(object o)
113         {
114             XmlSerializer mySerializer = new XmlSerializer(o.GetType());
115             MemoryStream ms = new MemoryStream();
116             mySerializer.Serialize(ms, o);
117             XmlDocument doc = new XmlDocument();
118             doc.LoadXml(Encoding.UTF8.GetString(ms.ToArray()));
119             if (doc.DocumentElement != null)
120             {
121                 return doc.DocumentElement.InnerXml;
122             }
123             else
124             {
125                 return o.ToString();
126             }
127         }
128
129         /// <summary>
130         /// 设置凭证与超时时间
131         /// </summary>
132         /// <param name="request"></param>
133         private static void SetWebRequest(HttpWebRequest request)
134         {
135             request.Credentials = CredentialCache.DefaultCredentials;
136             request.Timeout = 10000;
137         }
138
139         private static void WriteRequestData(HttpWebRequest request, byte[] data)
140         {
141             request.ContentLength = data.Length;
142             Stream writer = request.GetRequestStream();
143             writer.Write(data, 0, data.Length);
144             writer.Close();
145         }
146
147         private static byte[] EncodePars(Hashtable Pars)
148         {
149             return Encoding.UTF8.GetBytes(ParsToString(Pars));
150         }
151
152         private static String ParsToString(Hashtable Pars)
153         {
154             StringBuilder sb = new StringBuilder();
155             foreach (string k in Pars.Keys)
156             {
157                 if (sb.Length > 0)
158                 {
159                     sb.Append("&");
160                 }
161                 //sb.Append(HttpUtility.UrlEncode(k) + "=" + HttpUtility.UrlEncode(Pars[k].ToString()));
162             }
163             return sb.ToString();
164         }
165
166         private static XmlDocument ReadXmlResponse(WebResponse response)
167         {
168             StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
169             String retXml = sr.ReadToEnd();
170             sr.Close();
171             XmlDocument doc = new XmlDocument();
172             doc.LoadXml(retXml);
173             return doc;
174         }
175
176         private static void AddDelaration(XmlDocument doc)
177         {
178             XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "utf-8", null);
179             doc.InsertBefore(decl, doc.DocumentElement);
180         }
181
182         private static Hashtable _xmlNamespaces = new Hashtable();//缓存xmlNamespace,避免重复调用GetNamespace

View Code

转载于:https://www.cnblogs.com/LuckZ/p/4089947.html

webServices 应该支持Get和Post调用,在web.config应该增加以下代码相关推荐

  1. Coolite服务端方法调用与Web.Config配置

    1.AjaxEvet: //这是一个服务端方法 protected void UpdateTimeStamp(object sender, AjaxEventArgs e) { this.SetTim ...

  2. IDEA中报错:java: -source 1.5 中不支持静态接口方法调用

    用到java的一些新特性的时候,必须是在新的java版本中才能体现出来,否则会报错. 例如使用java8的Stream流或者lambadas特性,就会报如下错误: **Error:(11, 35) j ...

  3. JAVA与.NET的相互调用——通过Web服务实现相互调用

    JAVA与.NET是现今世界竞争激烈的两大开发媒体,两者语言有很多相似的地方.而在很多大型的开发项目里面,往往需要使用两种语言进行集成开发.而很多的开发人员都会偏向于其中一种语言,在使用集成开发的时候 ...

  4. 在ASP.NET Atlas中调用Web Service——创建Mashup调用远端Web Service(基础知识以及简单示例)...

    作者:Dflying Chen (http://dflying.cnblogs.com/) 注:Atlas中的Mashup极其复杂,其中涉及众多的对象与架构,为了写这篇文章,我花了不少时间学习研究.同 ...

  5. 使用 Ajax 调用 SOAP Web 服务,第 1 部分: 构建 Web 服务客户机

    James Snell (jasnell@us.ibm.com), 软件工程师,新兴技术, IBM James Snell 是 IBM 的 software group 中的 emerging Int ...

  6. C++ 调用 SOAP Web Service

    C++ 调用 SOAP Web Service 背景 首先,gSoap 肯定是个不错的选择,但是如果你的程序要调用多个 Web Services(即有多个 WSDL),gSoap 会比较麻烦.还有一个 ...

  7. CORBA 简单了解和JAVA与C++互操以及C++调用Java web service

    CORBA了解 CORBA(Common Object Request Broker Architecture, 公共对象请求代理体系结构)是由OMG(对象管理组织,Object Management ...

  8. ajax调用第三方web服务,js调用soapWebService服务

    js调用soapWebService服务 什么是 SOAP? SOAP 指简易对象访问协议 SOAP 是一种通信协议 SOAP 用于应用程序之间的通信 SOAP 是一种用于发送消息的格式 SOAP 被 ...

  9. 调用天气预报Web Service

    调用天气Web Service             i.创建项目                 项目名称:weatherclient             ii.创建本地的wsdl文件    ...

最新文章

  1. 抛弃ELK!Loki日志系统详解!
  2. 再议《反驳 吕震宇的“小议数据库主键选取策略(原创)” 》
  3. 【腾讯Bugly干货分享】Android Patch 方案与持续交付
  4. 让自己的user能够看到S4 product master这个tile
  5. 杀死初创科技公司的四大工程陷阱
  6. 创建项目提交至GitHub
  7. 自定义搜索框,带提示信息的搜索框
  8. jq使用教程09_ 教程集合帖-伙伴们贡献,不断更新(4.17)
  9. java如何统计txt的字数_Java HashSet对txt文本内容去重(统计小说用过的字或字数)...
  10. stm32F407 + FreeRTOS + FAT 文件系统移植
  11. python apkg_GitHub - cansou/pc_wxapkg_decrypt_python: PC微信小程序 wxapkg 解密
  12. 大量精品国学论文免费下载
  13. docker: Error response from daemon: driver failed programming external connectivity on endpoint mys
  14. SpringBoot+Vue打造资产出入库管理系统
  15. 第五人格深渊金币每周更新时间
  16. 带正电荷的脂质体-阳离子脂质体表面修饰
  17. 如何进入BIOS模式,BIOS进不去解决方案
  18. php?redis的scan用法实例分析
  19. 电竞英雄联盟数据API接口 - 【选手基本信息】API调用示例代码
  20. 【已解决】adb shell查看进程提示grep不是内部命令或外部命令

热门文章

  1. 03.结构化机器学习项目 W2.机器学习策略(2)
  2. LeetCode 16. 最接近的三数之和(固定左端+滑动窗口)
  3. php怎么获取分类数,php 两种获取分类树的方法
  4. jsonp跨域原理_Rust 搭建可跨域访问服务器JsonP(一)
  5. php.ini开启命名空间,Zend Framework教程之模型Model基本规则和使用方法
  6. Python自定义时间间隔访问网页
  7. Kaggle 房价预测竞赛优胜方案:用 Python 进行全面数据探索
  8. TCP的2MSL问题
  9. c语言数据类型_C语言基础数据类型
  10. 手机电脑的芯片主要是由_全体起立!苹果自研电脑芯片登场,iOS迎大更新…WWDC20精彩远不止这些...