C#使用SOAP调用Web Service
using System.IO;
using System.Net;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//构造soap请求信息
StringBuilder soap = new StringBuilder();
soap.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
soap.Append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
soap.Append("<soap:Body>");
soap.Append("<getWeather xmlns=\"http://WebXml.com.cn/\">");
soap.Append("<theCityCode>2210</theCityCode>");
soap.Append("<theUserID></theUserID>");
soap.Append("</getWeather>");
soap.Append("</soap:Body>");
soap.Append("</soap:Envelope>");
//发起请求
Uri uri = new Uri("http://www.webxml.com.cn/WebServices/WeatherWS.asmx");
WebRequest webRequest = WebRequest.Create(uri);
webRequest.ContentType = "text/xml; charset=utf-8";
webRequest.Method = "POST";
using (Stream requestStream = webRequest.GetRequestStream())
{
byte[] paramBytes = Encoding.UTF8.GetBytes(soap.ToString());
requestStream.Write(paramBytes, 0, paramBytes.Length);
}
//响应
WebResponse webResponse = webRequest.GetResponse();
using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
{
Console.WriteLine(myStreamReader.ReadToEnd());
}
Console.ReadKey();
}
}
}
返回结果:
感冒指数:将有一次强降温过程,且空气湿度较大,极易发生感冒,请特别注意增加衣服保暖防寒。
晨练指数:有阵雨,较不宜晨练,若坚持室外锻炼,请携带雨具。建议年老体弱人群适当减少晨练时间。
洗车指数:不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。
晾晒指数:偶尔的降雨可能会淋湿晾晒的衣物,不太适宜晾晒。请随时注意天气变化。
旅游指数:有阵雨,温度适宜,在细雨中游玩别有一番情调,可不要错过机会呦!但记得出门要携带雨具。
路况指数:有小雨,路面潮湿,车辆易打滑,请小心驾驶。
舒适度指数:白天不太热也不太冷,风力不大,相信您在这样的天气条件下,应会感到比较清爽和舒适。</string><string>1月21日 阵雨转小雨</string><string>12℃/19℃</string><string>无持续风向微风</string><string>3.gif</string><string>7.gif</string><string>1月22日 小雨</string><string>9℃/14℃</string><string>无持续风向微风</string><string>7.gif</string><string>7.gif</string><string>1月23日 小雨</string><string>8℃/12℃</string><string>无持续风向微风</string><string>7.gif</string><string>7.gif</string><string>1月24日 阴</string><string>12℃/17℃</string><string>无持续风向微风</string><string>2.gif</string><string>2.gif</string><string>1月25日 多云</string><string>13℃/18℃</string><string>无持续风向微风</string><string>1.gif</string><string>1.gif</string></getWeatherResult></getWeatherResponse></soap:Body></soap:Envelope>
ASP版
Dim xmlHttp,soap
soap = soap & "<?xml version=""1.0"" encoding=""utf-8""?>"
soap = soap & "<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">"
soap = soap & "<soap:Body>"
soap = soap & "<getWeather xmlns=""http://WebXml.com.cn/"">"
soap = soap & "<theCityCode>2210</theCityCode>"
soap = soap & "<theUserID></theUserID>"
soap = soap & "</getWeather>"
soap = soap & "</soap:Body>"
soap = soap & "</soap:Envelope>"
Set xmlHttp = CreateObject("MSXML2.XMLHTTP")
xmlHttp.Open "post","http://www.webxml.com.cn/WebServices/WeatherWS.asmx",false
xmlHttp.SetRequestHeader "Content-Type","text/xml; charset=utf-8"
'曾见过Java的ws要求一定要有SOAPAction,否则报错no SOAPAction header!
xmlHttp.SetRequestHeader "SOAPAction","http://WebXml.com.cn/getWeather"
xmlHttp.Send(soap)
If xmlHttp.Status = 200 Then
Response.Write(xmlHttp.responseXML.xml)
End If
Set xmlHttp = Nothing
%>
带SoapHeader的Web Service调用
using System.IO;
using System.Net;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//构造soap请求信息
StringBuilder soap = new StringBuilder();
soap.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
soap.Append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
soap.Append("<soap:Header>");
soap.Append("<MySoapHeader xmlns=\"http://www.mzwu.com/\" soap:mustUnderstand=\"1\">");//mustUnderstand=1,接收者必须能处理此SoapHeader信息,否则返回错误
soap.Append("<UserName>admin</UserName>");
soap.Append("<UserPass>admin888</UserPass>");
soap.Append("</MySoapHeader>");
soap.Append("</soap:Header>");
soap.Append("<soap:Body>");
soap.Append("<Hello xmlns=\"http://www.mzwu.com/\">");
soap.Append("<name>dnawo</name>");
soap.Append("</Hello>");
soap.Append("</soap:Body>");
soap.Append("</soap:Envelope>");
//发起请求
Uri uri = new Uri("http://localhost:39674/Service1.asmx");
WebRequest webRequest = WebRequest.Create(uri);
webRequest.ContentType = "text/xml; charset=utf-8";
webRequest.Method = "POST";
using (Stream requestStream = webRequest.GetRequestStream())
{
byte[] paramBytes = Encoding.UTF8.GetBytes(soap.ToString());
requestStream.Write(paramBytes, 0, paramBytes.Length);
}
//响应
WebResponse webResponse = webRequest.GetResponse();
using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
{
Console.WriteLine(myStreamReader.ReadToEnd());
}
Console.ReadKey();
}
}
}
小技巧
在浏览器中查看Web Service时,有详细的SOAP请求和响应示例:
参考资料
·SOAP教程:http://www.w3school.com.cn/soap/index.asp
·简单对象访问协议:http://zh.wikipedia.org/zh-cn/SOAP
·SOAP调用Web Service:http://book.51cto.com/art/200906/129733.htm
·关于朗新WEBSERVER接口问题:http://fpcfjf.blog.163.com/blog/static/5546979320081069223827/
C#使用SOAP调用Web Service的更多相关文章
- SOAP调用Web Service
SOAP调用Web Service (示例位置:光盘\code\ch07\ WebAppClient\ JsService4.htm) <html xmlns="http://www. ...
- ORACLE存储过程调用Web Service
1. 概述 最近在ESB项目中,客户在各个系统之间的服务调用大多都是在oracle存储过程中进行的,本文就oracle存储过程调用web service来进行说明.其他主流数据库,比如mysql和sq ...
- C#开发和调用Web Service
http://blog.csdn.net/h0322/article/details/4776819 1.1.Web Service基本概念 Web Service也叫XML Web Service ...
- php5调用web service
工作中需要用php调用web service接口,对php不熟,上网搜搜,发现关于用php调用web service的文章也不多,不少还是php4里用nusoap这个模块调用的方法,其实php5里已经 ...
- 通过ksoap2-android来调用Web Service操作的实例
import java.io.IOException; import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.SoapObjec ...
- 使用Android应用调用Web Service
Java本身提供了丰富的Web Service支持,比如Sun公司指定的JAX-WS 2规范,还有Apache开源组织所提供的Axis1.Axis2.CXF等,这些技术不仅可以用于非常方便地对外提 ...
- 【转】基于CXF Java 搭建Web Service (Restful Web Service与基于SOAP的Web Service混合方案)
转载:http://www.cnblogs.com/windwithlife/archive/2013/03/03/2942157.html 一,选择一个合适的,Web开发环境: 我选择的是Eclip ...
- ASP.NET调用Web Service
1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求, ...
- php5调用web service (笔者测试成功)
转自:http://www.cnblogs.com/smallmuda/archive/2010/10/12/1848700.html 感谢作者分享 工作中需要用php调用web service接口, ...
随机推荐
- 【HeadFirst设计模式】7.适配器模式与外观模式
今晚学习完第七章,顺便做一下知识备忘. 适配器模模式: 定义:将一个类的接口,转换成客户期望的另一个接口.适配器让原本接口不兼容的类可以合作无间. 对象适配器: 类适配器: 外观模式: 提供了一个统一 ...
- 2D游戏模型中动态分层的处理 及解决方案 (适用于 webgame 手游等资源控制较严格类型)
文章若非特别注明转载,皆是原创,转载请注明出处. 本文地址:http://www.cnblogs.com/bobolive/p/3537215.html 2D游戏中模型一般都有换装逻辑,特别是联网游戏 ...
- PH获取当前url路径及服务器路径汇总 (url 获取当前路径 服务器路径)
以下是整理的一些, php中获取路径的小知识, 希望对你有所帮助! 1,$_SERVER["QUERY_STRING"] 说明:查询(query)的字符串 2,$_SERVER[& ...
- Google设计理念
Google的十大信条 我们首次拟就这“十大信条”还是在Google刚刚成立没几年的时候.此后,我们时常重新审视这份清单,看看它是否依然适用.我们希望这些信条永不过时,而您也可以监督我们是否遵守了这些 ...
- MSChart实例
MSChart是VS中自带的图表控件,功能比较强大,效果也比较丰富.下面只提供一个例子,以供新接触的朋友参考. 先看下效果图: 看完效果图上代码啦. 使用这个控件需要先在页面注册一下. <%@ ...
- Html5页面返回机制解决方案
需要处理的返回场景: 1.正常的a->b->c 2.页面上的按钮触发需要登陆 3.页面跳转需要登陆 4.页面上的可修改的部分(如选择地址,地址页面本身也是可以增删改查的) 整体的原则是原路 ...
- java 中的 & | ~ ^ 运算符分析
1.与运算符与运算符用符号“&”表示,其使用规律如下:两个操作数中位都为1,结果才为1,否则结果为0,例如下面的程序段.public class data13{public static vo ...
- 在jsp中运用ajax实现同一界面不跳转处理事件
目前,编写应用程序时有两种基本的选择: 桌面应用程序 Web应用程序 它们有什么区别呢?桌面应用程序一般很快(就在您的计算机上运行,不用等待互联网连接),具有漂亮的用户界面(通常和操作系统有关)和非凡 ...
- Oracle索引扫描
Oracle索引扫描:先通过index查找到索引的值,并根据索引的值对应的rowid值(对于非唯一索引可能返回多个rowid值)直接从表中得到具体的数据.一个rowid唯一的表示一行数据,该行对应的数 ...
- Spring核心框架 - AOP的起源及介绍
一.AOP技术起源 AOP技术的诞生并不算晚,早在1990年开始,来自Xerox Palo Alto Research Lab(即PARC)的研究人员就对面向对象思想的局限性进行了分析.他们研究出了一 ...