C#使用SOAP获取webservice实例解析
本文主要參考例如以下两个链接。并整理:
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();
}
}
}
例如以下图即能够得到执行结果:
C#使用SOAP获取webservice实例解析的更多相关文章
- html+javascript+soap获取webservice免费天气预报信息
转自:http://blog.163.com/hubeimeiyu@126/blog/static/8004881020118303318687/ 首先,也是最重要的是互联网上免费的天气预报源:htt ...
- Datatables插件1.10.15版本服务器处理模式ajax获取分页数据实例解析
一.问题描述 前端需要使用表格来展示数据,找了一些插件,最后确定使用dataTables组件来做. 后端的分页接口已经写好了,不能修改.接口需要传入页码(pageNumber)和页面显示数据条数(pa ...
- SAP和Java系统的Webservice实例
简介: 关于Webservice的概念和原理,简单来讲,Webservice是一种基于SOAP传输协议,用WSDL描述,用XML封装数据的接口技术.由于其跨平台.跨防火墙.开发成本低.开发周期短等优势 ...
- THINKPHP3.2 中使用 soap 连接webservice 解决方案
今天使用THINKPHP3.2 框架中开发时使用soap连接webservice 一些浅见现在分享一下, 1.首先我们要在php.ini 中开启一下 php_openssl.dll php_soap. ...
- SoapUI简介和入门实例解析
SoapUI简介 SoapUI是一个开源测试工具,通过soap/http来检查.调用.实现Web Service的功能/负载/符合性测试.该工具既可作为一个单独的测试软件使用,也可利用插件集成到Ecl ...
- C# 通过Get、Post、Soap调用WebService的方法
实现代码来源于网络,我只是作了一些修改! using System; using System.Web; using System.Xml; using System.Collections; usi ...
- [Reprint]C++普通函数指针与成员函数指针实例解析
这篇文章主要介绍了C++普通函数指针与成员函数指针,很重要的知识点,需要的朋友可以参考下 C++的函数指针(function pointer)是通过指向函数的指针间接调用函数.相信很多人对指向一般 ...
- Ksoap2 获取webservice返回值的getResponse() 出现的问题
今天写了一个判断记录重复的webservcie 返回布尔类型 // 判断序列号在数据库是否重复 public static boolean isSerialNumExist(String serial ...
- JavaWeb实现文件上传下载功能实例解析
转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web应用系统开发中,文件上传和下载功能是非常常用的功能 ...
随机推荐
- Mybatis拦截器介绍
拦截器的一个作用就是我们可以拦截某些方法的调用,我们可以选择在这些被拦截的方法执行前后加上某些逻辑,也可以在执行这些被拦截的方法时执行自己的逻辑而不再执行被拦截的方法.Mybatis拦截器设计的一个初 ...
- NiuTrans 日记 1
这些天把东北大学自然语言实验室的NiuTrans 系统搭建并按照例子将短语系统运行了一遍,写这个日记主要是为了以后能提醒自己在这其中遇到的问题. 环境:短语系统我是windows和linux都运行了, ...
- private
成员变量私有化的好处在于可以强制加强面向对象和封装的概念,一个面向对象的系统更加关注行为,而不是数据,所以应该通过发送消息来获得数据,也应该实习细节的封装
- Python中的__init__,__call__
__init__函数 当一个类实例被创建时, __init__() 方法会自动执行,在类实例创建完毕后执行,类似构建函数.__init__() 可以被当成构建函数,不过不象其它语言中的构建函数,它并不 ...
- 自己实现Single LinkedList
My_Single_LinkedList 分4个部分实现(CRUD - 增删改查). 首先要有一个Node(节点类) class Node { public int val; public Node ...
- es 的集群状态
es的集群状态一共有三种 : green yellow red 状态是基于 碎片的 等级进行划分的 .
- workstack windows to openstack
https://www.mirantis.com/openstack-portal/express-openstack-portal/migrating-from-vmware-for-windows ...
- Create a commit using pygit2
Create a commit using pygit2 Create a commit using pygit2 2015-04-06 10:41 user1479699 imported from ...
- 第二百六十天 how can I 坚持
晚上去看了个电影,<万万没想到>,挺好看的,豆瓣评分不高.网络啊. 互联网会让聪明的人越来越聪明. 明天去看寻龙诀了. 懒惰会生根发芽,哈哈. 睡觉了.
- 现在,UICollectionViews有了简单的重排功能
原文:UICollectionViews Now Have Easy Reordering 我是UICollectionView的忠实粉丝.这个类比起它的老哥UITableView类具有更高的可定制性 ...