看了网上的几个文章,SOAP的示例布局都不清晰,不能马上入手,特意写个例子与大家分享,同时记录备用。

当前环境:VS2013 + WPF

private void Button_Click(object sender, RoutedEventArgs e)
{
string url = "http://www.webxml.com.cn/WebServices/WeatherWS.asmx";
string soap = SetSoapMessage();// 构造soap请求信息 string result = GetSOAPReSource(url, soap); txtShow.Text = result.Replace(">", ">\n").Replace("<string>\n", "<string>");
} #region 发起SOAP请求
/// <summary>
/// 发起SOAP请求
/// </summary>
/// <param name="url">URL</param>
/// <param name="datastr">数据</param>
/// <returns></returns>
public static string GetSOAPReSource(string url, string datastr)
{
//发起请求
Uri uri = new Uri(url);
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(datastr.ToString());
requestStream.Write(paramBytes, , paramBytes.Length);
} //响应
WebResponse webResponse = webRequest.GetResponse();
using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
{
string result = "";
return result = myStreamReader.ReadToEnd();
}
}
#endregion #region 构造soap请求信息
string SetSoapMessage()
{
string header = "";
string body = "";
string fault = ""; body = "<getRegionCountry xmlns=\"http://WebXml.com.cn/\" />"; return GetSoapMessageByBase(header, body, fault);
}
#endregion #region SOAP消息基本结构
/// <summary>
/// SOAP消息基本结构
/// </summary>
/// <param name="header">头部(包含Header)</param>
/// <param name="body">内容主体(包含Body)</param>
/// <param name="fault">错误提示(包含Fault)</param>
/// <returns></returns>
string GetSoapMessageByBase(string header, string body, string fault)
{
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\">");
//添加头部
if (!string.IsNullOrWhiteSpace(header))
{
soap.Append("<soap:Header>");
soap.Append(header);
soap.Append("</soap:Header>");
}
//添加内容
if (!string.IsNullOrWhiteSpace(body))
{
soap.Append("<soap:Body>");
soap.Append(body); //添加错误
if (!string.IsNullOrWhiteSpace(fault))
{
soap.Append("<soap:Fault>");
soap.Append(fault);
soap.Append("</soap:Fault>");
} soap.Append("</soap:Body>");
} soap.Append("</soap:Envelope>"); return soap.ToString();
}
#endregion

SOAP简单示例的更多相关文章

  1. Web API 简单示例

    一.RESTful和Web API Representational State Transfer (REST) is a software architecture style consisting ...

  2. 基于.NET CORE微服务框架 -surging的介绍和简单示例 (开源)

    一.前言 至今为止编程开发已经11个年头,从 VB6.0,ASP时代到ASP.NET再到MVC, 从中见证了.NET技术发展,从无畏无知的懵懂少年,到现在的中年大叔,从中的酸甜苦辣也只有本人自知.随着 ...

  3. Linux下的C Socket编程 -- server端的简单示例

    Linux下的C Socket编程(三) server端的简单示例 经过前面的client端的学习,我们已经知道了如何创建socket,所以接下来就是去绑定他到具体的一个端口上面去. 绑定socket ...

  4. C# 构建XML(简单示例)

    C# 构建XML的简单示例: var pars = new Dictionary<string, string> { {"url","https://www. ...

  5. 根据juery CSS点击一个标签弹出一个遮罩层的简单示例

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  6. ACEXML解析XML文件——简单示例程序

    掌握了ACMXML库解析XML文件的方法后,下面来实现一个比较完整的程序. 定义基本结构 xml文件格式如下 <?xml version="1.0"?> <roo ...

  7. demo工程的清单文件及activity中api代码简单示例

    第一步注册一个账户,并创建一个应用.获取app ID与 app Key. 第二步下载sdk 第三步新建工程,修改清单文件,导入相关的sdk文件及调用相应的api搞定. 3.1 修改清单文件,主要是加入 ...

  8. spring-servlet.xml简单示例

    spring-servlet.xml简单示例 某个项目中的spring-servlet.xml 记下来以后研究用 <!-- springMVC简单配置 --> <?xml versi ...

  9. SignalR 简单示例

    一.什么是 SignalR ASP.NET SignalR is a library for ASP.NET developers that simplifies the process of add ...

随机推荐

  1. bootstrap响应式布局列子

    <!DOCTYPE html><html lang="zh-CN"> <head> <meta charset="utf-8&q ...

  2. 2017全球GDP总量达74万亿美元 各国占比排行榜

    全球GDP总量达74万亿美元 各国占比排行榜     2017年公布的2015年全球各国GDP占比,数据图片来源:世界银行报告 2月24日,来自世界银行的最新GDP数字已于2月早些时候公布,现由How ...

  3. Android 四大组件和Intent

    一.Android有四大组件(component):Activity.Service.BroadcastReceiver.ContentProvider. 1.Activity 通过startActi ...

  4. ESLint的使用

    ESLint是在ECMAScript/JavaScript代码中识别和报告模式匹配的工具,它的目标是保证代码的一致性和避免错误.在许多方面,它和JSLint.JSHint相似,除了少数的例外: ESL ...

  5. CF10D LCIS

    题意翻译 求两个串的最长公共上升子序列. 题目描述 This problem differs from one which was on the online contest. The sequenc ...

  6. native2ascii -reverse -encoding UTF-8 validation_msg.properties > validation_msg_src.properties

    native2ascii -reverse -encoding UTF-8 validation_msg.properties > validation_msg_src.properties

  7. (NOI2014)(bzoj3669)魔法森林

    LCT裸题,不会的可以来这里看看. 步入正题,现将边按a排序,依次加入每一条边,同时维护路径上的最小生成树上的最大边权,如果两点不连通,就直接连通. 如果两点已经连通,就将该边与路径上较小的一条比较, ...

  8. 【CF888E】Maximum Subsequence(meet in the middle)

    [CF888E]Maximum Subsequence(meet in the middle) 题面 CF 洛谷 题解 把所有数分一下,然后\(meet\ in\ the\ middle\)做就好了. ...

  9. MySQL的replace方法

    mysql中replace函数直接替换mysql数据库中某字段中的特定字符串,不再需要自己写函数去替换,用起来非常的方便,mysql 替换函数replace()Update `table_name` ...

  10. SQL2005分页存储过程(支持多表联接)

    Code /*********************************************************   * 作    用:数据分页(完整SQL分页存储过程(支持多表联接)) ...