不通过“添加引用服务”调用WebService接口
来源:互联网 发布:由深入浅mysql中文网盘 编辑:程序博客网 时间:2024/06/05 08:14
有时候,在调用外网的服务时,例如Webservice服务,直接添加引用服务,有时候还要手动更新服务,比较不够智能,如果能只通过传入服务地址和参数就能获取数据,那就最好不过。
以下就是一个例子:
public static XElement WebCallRequestFunc(string paramXML) { try { System.Net.ServicePointManager.DefaultConnectionLimit = 200; System.Net.HttpWebRequest req = System.Net.HttpWebRequest.Create("*.asmx") as System.Net.HttpWebRequest;//回呼服务地址 System.Text.Encoding encoding = System.Text.Encoding.UTF8;//默认编码 string paras = string.Format("<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Header><h:SoapHeaderExt xmlns:h=\"http://tempuri.org/\" xmlns=\"http://tempuri.org/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><UserToken>{0}</UserToken></h:SoapHeaderExt></s:Header><s:Body xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><ArticleGeneration xmlns=\"http://tempuri.org/\"><xml>{1}</xml></ArticleGeneration></s:Body></s:Envelope>", "", paramXML);//xml格式参数 byte[] bs = System.Text.Encoding.UTF8.GetBytes(paras); req.Host = ""; req.Method = "POST"; req.UserAgent = "Mozilla-Firefox-Spider(Wenanry)"; req.Timeout = 30000; req.Headers.Add("SOAPAction", "http://tempuri.org/ArticleGeneration"); req.Headers.Add("VsDebuggerCausalityData", ""); //req.Headers.Add("Expect", "100-continue"); //req.CookieContainer = new System.Net.CookieContainer(); //System.Net.CookieContainer cookie = new System.Net.CookieContainer();//如果用不到Cookie,删去即可 req.KeepAlive = true; //上面的http头看情况而定,但是下面俩必须加 req.ContentType = "text/xml;charset=utf-8"; req.ContentLength = bs.Length; using (System.IO.Stream reqStream = req.GetRequestStream()) { reqStream.Write(bs, 0, bs.Length); reqStream.Close(); } string responseData = string.Empty;//返回数据 using (System.Net.HttpWebResponse response = (req.GetResponse() as System.Net.HttpWebResponse)) { //如果http头中接受gzip的话,这里就要判断是否为有压缩,有的话,直接解压缩即可 #region 读取内容 //req.CookieContainer.Add(response.Cookies); System.IO.Stream responseStream = response.GetResponseStream(); using (System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, encoding)) { responseData = reader.ReadToEnd(); } if (!string.IsNullOrEmpty(responseData)) { //string decsponseData = Server.HtmlDecode(responseData); Regex reg = new Regex("<ArticleGenerationResult>.*</ArticleGenerationResult>", RegexOptions.IgnoreCase);//返回 MatchCollection matches = reg.Matches(responseData); XElement xmlDoc = null; if (matches.Count > 0) { xmlDoc = XElement.Parse(matches[0].Value); //如果是xml字符串,则用以下形式 } return xmlDoc; } else { return null; } #endregion }//using } catch (Exception ex) { return null; } }
0 0
- 不通过“添加引用服务”调用WebService接口
- java 调用WebService服务接口
- C#不添加引用,动态调用webservice
- C#不添加引用,动态调用webservice .
- C#不添加引用,动态调用webservice
- C# 不用添加WebService引用,调用WebService方法
- 如何不通过反射来引用隐藏接口?
- 【webservice】调用webservice接口
- android 通过KSOAP调用.NET webservice中的服务接口
- Visual C++调用WebService服务接口常见数据类型转换
- WebService添加引用
- 调用webservice接口时出现401错误 添加身份验证
- WebService服务接口
- domino 调用 WebService 接口
- WebService 接口调用指南
- php 调用webservice接口!!
- Java WebService 接口调用
- java调用webservice 接口
- 回望过去三年(作为程序猿踏入社会的三年) 续篇
- jndi
- 构建并修复逻辑损坏块和物理损坏块
- catalina.out日志按天轮转
- Versions 崩溃(Mac升级OS X Yonsemite 10.10)
- 不通过“添加引用服务”调用WebService接口
- iOS开发笔记(IOS7回归)
- 括号匹配
- LinuxShell算术运算
- IO流基础特性 使用缓冲技术复制文本文件原理浅析
- libpcap使用
- 流氓的百度网盘
- linux进程控制函数--fork,exec,exit,wait,sleep
- 关于抽象类与接口的总结