C#使用SOAP获取webservice实例解析
2015-07-23 15:27
666 查看
本文主要参考如下两个链接,并整理:
Java使用SOAP: /article/4621095.html
C# send soap and get response: http://stackoverflow.com/questions/4791794/client-to-send-soap-request-and-received-response
1.webservice提供方:http://www.webxml.com.cn/zh_cn/index.aspx
2.下面我们以“获得腾讯QQ在线状态”为例。网页介绍参考:http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?op=qqCheckOnline
代码如下:
如下图即可以得到运行结果:
Java使用SOAP: /article/4621095.html
C# send soap and get response: http://stackoverflow.com/questions/4791794/client-to-send-soap-request-and-received-response
1.webservice提供方:http://www.webxml.com.cn/zh_cn/index.aspx
2.下面我们以“获得腾讯QQ在线状态”为例。网页介绍参考:http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?op=qqCheckOnline
代码如下:
using System.IO; using System.Xml; using System.Net; namespace ConsoleApplicationTest2 { class SOAPTest { public static void CallWebService(string qq) { var _url = "http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx"; var _action = "http://WebXml.com.cn/qqCheckOnline"; XmlDocument soapEnvelopeXml = CreateSoapEnvelope(qq); HttpWebRequest webRequest = CreateWebRequest(_url, _action); InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest); // begin async call to web request. IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null); // suspend this thread until call is complete. You might want to // do something usefull here like update your UI. asyncResult.AsyncWaitHandle.WaitOne(); // get the response from the completed web request. string soapResult; using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult)) { using (StreamReader rd = new StreamReader(webResponse.GetResponseStream())) { soapResult = rd.ReadToEnd(); } Console.WriteLine(soapResult); } } private static HttpWebRequest CreateWebRequest(string url, string action) { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); webRequest.Headers.Add("SOAPAction", action); webRequest.ContentType = "text/xml;charset=\"utf-8\""; webRequest.Accept = "text/xml"; webRequest.Method = "POST"; return webRequest; } private static XmlDocument CreateSoapEnvelope(string qq) { XmlDocument soapEnvelop = new XmlDocument(); string soapXml = @"<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/1999/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/1999/XMLSchema""><SOAP-ENV:Body><qqCheckOnline xmlns=""http://WebXml.com.cn/""><qqCode>qq_Code</qqCode></qqCheckOnline></SOAP-ENV:Body></SOAP-ENV:Envelope>"; soapEnvelop.LoadXml(soapXml.Replace("qq_Code",qq)); return soapEnvelop; } private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest) { using (Stream stream = webRequest.GetRequestStream()) { soapEnvelopeXml.Save(stream); } } static void test() { string[] qq = { "49", "4941", "4949252", "494925223", "4949252242", "48492522502", "49492522" }; foreach (var qc in qq) SOAPTest.CallWebService(qc); Console.ReadKey(); } } }
如下图即可以得到运行结果:
相关文章推荐
- c#延迟加载
- c#的中英文混合字符串截取 public static string SubString(string inputString, int byteLength)
- c#的中英文混合字符串截取指定长度,startidx从0开始
- 浅谈C#中Control的Invoke与BeginInvoke在主副线程中的执行顺序和区别
- C# 加密解密(DES,3DES,MD5,Base64) 类
- C#文件的处理
- c# 时间戳转换
- Sublime Text 3 (含:配置 C# 编译环境)
- C#高级编程五十天----栈
- C#集合总结
- C# 截取中英文混合字符串分行显示宽度相同
- 在用c#进行c语言的编程题时,保持运行的窗口能停留的方法
- C# 创建快捷方式
- C#截取中英文混合字符串分行显示
- C#截取指定长度中英文字符串方法 (修改)
- C#Socket多线程通信(聊天室)
- .Net学习笔记----2015-07-23(C#基础复习11,字符串复习)
- c#使用正则表达式
- C#之IO读写文件方法封装代码
- C# WinForm动态调用远程Web服务