[转]C#通过Http发送Soap请求
/// <summary>
/// 发送SOAP请求,并返回响应xml
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="datastr">SOAP请求信息</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, 0, paramBytes.Length);
}
//响应
WebResponse webResponse = webRequest.GetResponse();
using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
{
string result = "";
return result=myStreamReader.ReadToEnd();
}
}
示例:调用webservice查询IP地址信息
webservice地址:http://www.wjg121.cn/Service/IPAddress.asmx?op=GetIPCountryAndLocal
static void Main(string[] args)
{
//构造soap请求信息
StringBuilder soap = new StringBuilder();
soap.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
soap.Append("<soap:Envelope
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
soap.Append("<soap:Body>");
soap.Append("<GetIPCountryAndLocal xmlns=\"http://tempuri.org/\">");
soap.Append("<RequestIP>183.39.119.90</RequestIP>");
soap.Append("</GetIPCountryAndLocal>");
soap.Append("</soap:Body>");
soap.Append("</soap:Envelope>");
string url = "http://www.wjg121.cn/Service/IPAddress.asmx";
Console.WriteLine(WebServiceUtility.GetSOAPReSource(url,soap.ToString()));
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><GetIPCountryAndLocalR
esponse xmlns="http://tempuri.org/"><GetIPCountryAndLocalResult>广东省电信</GetI
PCountryAndLocalResult></GetIPCountryAndLocalResponse></soap:Body></soap:Envelop
e>
[转]C#通过Http发送Soap请求的更多相关文章
- Java发布一个简单 webservice应用 并发送SOAP请求
一.创建并发布一个简单的webservice应用 1.webservice 代码: package com.ls.demo; import javax.jws.WebMethod; import ja ...
- Java发布webservice应用并发送SOAP请求调用
webservice框架有很多,比如axis.axis2.cxf.xFire等等,做服务端和做客户端都可行,个人感觉使用这些框架的好处是减少了对于接口信息的解析,最主要的是减少了对于传递于网络中XML ...
- Jmeter发送SOAP请求对WebService接口测试
Jmeter发送SOAP请求对WebService接口测试 1.测试计划中添加一个用户自定义变量 2.HTTP信息头管理器,添加Content-Tpe, application/soap+xml;c ...
- webService 发送soap请求,并解析返回的soap报文
本例应用场景:要做一个webService测试功能,不局限于任何一种固定格式的webService,所以像axis,cxf等框架就不好用了.只有深入到webService的原理,通过发收soap报文, ...
- Jmeter发送soap请求
1.新建线程组-添加SOAP/XML-RPC Request 2.我们以天气预报接口为例,http://ws.webxml.com.cn/WebServices/WeatherWS.asmx,选择最后 ...
- Java 发送SOAP请求调用WebService,解析SOAP报文
https://blog.csdn.net/Peng_Hong_fu/article/details/80113196 记录测试代码 SoapUI调用路径 http://localhost:8082/ ...
- java使用POST发送soap报文请求webservice返回500错误解析
本文使用JAX-WS2.2编译webservice,并使用HttpUrlConnection的POST方式对wsdl发送soap报文进行请求返回数据, 对错误Server returned HTTP ...
- C# httpRequest Soap请求
一般添加web服务引用是.NET用代理类模式 创建SOAP请求代理类,代理类是.NET开发工具VS自动给你生成. 下面用一般HTTP的模式有时候可能更合适,原理是构造SOAP请求的XML后POST过去 ...
- SoapUI使用方法-01发送http请求
soap ui http://blog.csdn.net/russ44/article/details/51680083 一.发送HTTP请求消息 1.打开soapUI. 2.新建一个项目,实例如下: ...
随机推荐
- 启动mysql出现1067错误
0. 打开mysql\bin\my.ini,查找[mysqld],在[mysqld]下面添加一行文字,skip-grant-tables 即组成 [mysqld] skip-grant-tables[ ...
- java发送email(含代理方式,ssl方式,传统方式)
package spring.vhostall.com; import java.security.Security; import java.util.Date; import java.util. ...
- Openerp 7 view画面编辑/创建/删除/复制功能控制
OpenERP 7.0中,Tree或Form画面上可以添加属性create edit delete,用于控制Create.Delete.Edit.Duplicate功能是否显现.如下例:<for ...
- uploadify上传之前判断一个input输入框是否为空
onUploadStart:function(file){ if ($("#ContractCode").val() == "") { alert(" ...
- tarfile模块可以方操作tar归档文件
# -*- coding: utf-8 -*- #python 27 #xiaodeng #Python自带的tarfile模块可以方便读取tar归档文件 #http://www.open-open. ...
- 整理两个JVM博客集合,空闲时候可以看
纯洁的微笑写的:https://www.cnblogs.com/ityouknow/p/5614961.html 集合:http://www.cnblogs.com/ityouknow/categor ...
- 用css3做一个求婚小动画
概述 本案例主要是运用到了css3的animation.keyframes.transform等属性,熟悉了,就可以做更多的其他动画效果,这几个属性功能非常强大. 详细 代码下载:http://www ...
- PHP:微信小程序调用【统一下单】【微信支付】【支付回调】API;XML转Array,Array转XML方法(通用)
1.微信公众号.微信小程序开发过程中,第三方服务器与微信服务器数据交互,需要进行数据转换,必须用到这两个函数: 分别是xml_to_array.array_to_xml ; /** * 输出xml字符 ...
- 【mysql】Innodb三大特性之adaptive hash index
1.Adaptive Hash Indexes 定义 If a table fits almost entirely in main memory, the fastest way to perfor ...
- PHP扩展的基本结构
1.下载php源码 git clone https://github.com/php/php-src.git 2,创建扩展 cd php-src/ext/ ./ext_skel --extname= ...