本文主要參考例如以下两个链接。并整理:

Java使用SOAP: http://www.cnblogs.com/linjiqin/archive/2012/05/07/2488880.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();
}
}
}

例如以下图即能够得到执行结果:

最新文章

  1. POJ1236Network of Schools(强连通分量 + 缩点)
  2. 各大互联网公司前端面试题(js)
  3. [Bootstrap]7天深入Bootstrap(3)CSS布局
  4. UVA 12382 Grid of Lamps --贪心+优先队列
  5. SPOJ-7001 VLATTICE 莫比乌斯反演定理
  6. Java SAX Parser
  7. isset(), empty()
  8. virtualbox共享文件夹无访问权限问题解决方法
  9. nginx添加缓存
  10. JS比较思维模型
  11. Python标准库--Scope
  12. Java中集合List,Map和Set的区别
  13. Codeforces Round #549 (Div. 2) 训练实录 (5/6)
  14. linxu下redis安装实战
  15. 获取标准shell 命令的输出内容
  16. PHP实用工具类
  17. linux windows 格式化一块大于2 TiB硬盘
  18. BZOJ3512 DZY Loves Math IV
  19. JAVA知识笔记
  20. iOS 线程安全--锁

热门文章

  1. Permutations java实现
  2. codeforces 700C Break Up 暴力枚举边+边双缩点(有重边)
  3. codeforces 678D Iterated Linear Function 矩阵快速幂
  4. js控制不同的时间段显示不同的css样式
  5. bzoj 3207 花神的嘲讽计划Ⅰ(哈希法+主席树)
  6. [转] Web前端优化之 内容篇
  7. 优雅地对泛型List 进行深拷贝
  8. nodejs API笔记
  9. nodejs+express +jade模板引擎 新建项目
  10. java出现no XXX in java.library.path的解决办法及eclipse配置