程序代码
using System;
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();
        }
    }

}

返回结果:

引用内容
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><getWeatherResponse xmlns="http://WebXml.com.cn/"><getWeatherResult><string>福建 福州</string><string>福州</string><string>2210</string><string>2010/01/21 15:42:15</string><string>今日天气实况:气温:12.5℃;风向/风力:东南风 4级;湿度:94%;气压:1011.9hPa</string><string>空气质量:良;紫外线强度:暂无</string><string>穿衣指数:建议着薄型套装或牛仔衫裤等春秋过渡装。年老体弱者宜着套装、夹克衫等。
感冒指数:将有一次强降温过程,且空气湿度较大,极易发生感冒,请特别注意增加衣服保暖防寒。
晨练指数:有阵雨,较不宜晨练,若坚持室外锻炼,请携带雨具。建议年老体弱人群适当减少晨练时间。
洗车指数:不宜洗车,未来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;
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的更多相关文章

  1. SOAP调用Web Service

    SOAP调用Web Service (示例位置:光盘\code\ch07\ WebAppClient\ JsService4.htm) <html xmlns="http://www. ...

  2. ORACLE存储过程调用Web Service

    1. 概述 最近在ESB项目中,客户在各个系统之间的服务调用大多都是在oracle存储过程中进行的,本文就oracle存储过程调用web service来进行说明.其他主流数据库,比如mysql和sq ...

  3. C#开发和调用Web Service

    http://blog.csdn.net/h0322/article/details/4776819 1.1.Web Service基本概念 Web Service也叫XML Web Service ...

  4. php5调用web service

    工作中需要用php调用web service接口,对php不熟,上网搜搜,发现关于用php调用web service的文章也不多,不少还是php4里用nusoap这个模块调用的方法,其实php5里已经 ...

  5. 通过ksoap2-android来调用Web Service操作的实例

    import java.io.IOException; import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.SoapObjec ...

  6. 使用Android应用调用Web Service

    Java本身提供了丰富的Web  Service支持,比如Sun公司指定的JAX-WS  2规范,还有Apache开源组织所提供的Axis1.Axis2.CXF等,这些技术不仅可以用于非常方便地对外提 ...

  7. 【转】基于CXF Java 搭建Web Service (Restful Web Service与基于SOAP的Web Service混合方案)

    转载:http://www.cnblogs.com/windwithlife/archive/2013/03/03/2942157.html 一,选择一个合适的,Web开发环境: 我选择的是Eclip ...

  8. ASP.NET调用Web Service

    1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求, ...

  9. php5调用web service (笔者测试成功)

    转自:http://www.cnblogs.com/smallmuda/archive/2010/10/12/1848700.html 感谢作者分享 工作中需要用php调用web service接口, ...

随机推荐

  1. 九度OJ 1408 吃豆机器人 -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1408 题目描述: 淘宝公司内部有许多新鲜的小玩具,例如淘宝智能机器人.小时候,大家都玩过那个吃豆子的游戏吧,这机器 ...

  2. Sharepoint 2010 Workflow 发布

    1.首先需要有一个已经创建好的WorkFlow 2.然后在Sharepoint中打开这个WorkFlow,点击Save as Template,系统会自动将这个Workflow保存在Site Asse ...

  3. 链表C++模板实现

    #include <iostream.h> #include <stdlib.h> //结点模板类 template <typename t1, typename t2& ...

  4. .NET研发人员面试题(一)

    1.简述javascript中的“=.==.===”的区别? =赋值 ==比较是否一般相等   "3"==3 //会做类型的隐式转换,true ===比较是否严格相等 " ...

  5. winform 通过 html 与swf 交互 简单案例

    在上一篇 winform 与 html 交互 简单案例 中讲了winform与html之间的简单交互,接下来的内容是在winform中以html为中转站,实现将swf嵌入winform中并实现交互. ...

  6. hggroup和adress

    hggroup通常放在标签内部,不过不做强制要求! ​adress 通常用于作者的联系信息.比如名字,Email,电话,地址.标签内字体显示斜体. ​ 与 标签的比较:比div简洁,更少的用到id或c ...

  7. 上传头像,界面无跳转,php+js

    上传头像,界面无跳转的方式很多,我用的是加个iframe那种.下面直接上代码. html: //route 为后端接口//upload/avatar 为上传的头像的保存地址//imgurl=/uplo ...

  8. C指针笔记

    指针的学习 两个数比较大小,通过传递内容进行比较 #include <stdio.h> void swap(int *p1, int *p2){ int temp; //注意指变量*的两个 ...

  9. 大数据时代的技术hive:hive的数据类型和数据模型

    在上篇文章里,我列举了一个简单的hive操作实例,创建了一张表test,并且向这张表加载了数据,这些操作和关系数据库操作类似,我们常把hive和关系数据库进行比较,也正是因为hive很多知识点和关系数 ...

  10. codeforces 615D - Multipliers

    Multipliers 题意:给定一个2e5范围内的整数m,之后输入m个2e5内的素数(当然可以重复了),问把这些输入的素数全部乘起来所得的数的约数的乘积mod(1e9+7)等于多少? 思路:对题目样 ...