[转]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>

时间: 2024-08-27 09:52:49

[转]C#通过Http发送Soap请求的相关文章

Java发布webservice应用并发送SOAP请求调用

webservice框架有很多,比如axis.axis2.cxf.xFire等等,做服务端和做客户端都可行,个人感觉使用这些框架的好处是减少了对于接口信息的解析,最主要的是减少了对于传递于网络中XML的解析,代价是你不得不在你的框架中添加对于这些框架的依赖.个人观点是:服务端使用这些框架还行,如果做客户端,没必要使用这些框架,只需使用httpclient即可. 一.创建并发布一个简单的webservice应用 1.webservice 代码: import javax.jws.WebMethod

perl发送SOAP请求

项目中的需要发送SOAP消息来进行一些操作.由于SOAP协议是构建在HTTP协议之上的,因此通过发送HTTP请求也可以解决此问题. 此外,项目中还需要考虑对SSL协议的支持. 方法一:利用SOAP::Lite(perl的第三方库)来实现 use SOAP::Lite; my $proxy='http://host:port/.../...?wsdl'; my $soap=SOAP::Lite->proxy($proxy)->ns('...'); my $response=$soap->c

Jmete发送soap请求

1.新建线程组-添加SOAP/XML-RPC Request 2.我们以天气预报接口为例,http://ws.webxml.com.cn/WebServices/WeatherWS.asmx,选择最后一个:获取天气预报数据 3.SOAP/XML-RPC Request输入url和请求数据,注意Send SOAPAction如果不填,必须去掉勾选框 4.如果勾选需要输入SOAPAction内容 5.theCityCode输入城市名称或城市ID,theUserID默认参数"string"去

jmeter发送soap请求

以天气预报接口为例: http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl 1.通过SoapUI软件等软件将WSDL文档生成其相应Soap消息来设置 File---->new soapUI Project---->输入Project Name---->Initial WSDL/WADL:输入http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsd

java使用POST发送soap报文请求webservice返回500错误解析

本文使用JAX-WS2.2编译webservice,并使用HttpUrlConnection的POST方式对wsdl发送soap报文进行请求返回数据, 对错误Server returned HTTP response code: 500 的解决方法进行简单分析. 问题描述: 由于课程需要博主需要自己写一个webservice并且通过soap进行请求, 于是使用JAX-WS编译了下面java代码生成webservice服务 生成webservice的java代码: [java] view plai

Web Service之Soap请求响应内容中文编码解密

java模拟Soap请求测试Web Service接口,发现Web Service响应内容中的中文竟然是编码格式.比如: 中文:退保成功 Soap中文编码:退保成功 我仔细分析后发现,退编码实际上就是Unicode编码的Soap版,正规的Unicode编码是\u9000,Soap改成自己的格式&#x[4位内容];格式. 还有其他的比如: 换行,Soap编码: 单引号,Soap为转换为html编码:&apos; 与号,Soap为转换为html编码:& 小于号,Soap为转换为html

java soap api操作和发送soap消息

Java代码   package gov.hn12396.appintegration.mule.client; import gov.hn12396.appintegration.mule.util.EncoderUtil; import java.net.URL; import java.util.Calendar; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPBody; import javax.xml.s

Ajax发送post请求

//创建Ajax对象(兼容处理) function createXHR() { var xhr = null; if(window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else if(window.ActiveXObject) { xhr = new ActiveXObject('Microsoft.XMLHTTP'); } return xhr; } //发送post请求 function request(){ xhr.open('P

每天一个linux命令13之curl发送http请求

一.get请求 curl "http://www.baidu.com"  如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地 curl -i "http://www.baidu.com"  显示全部信息 curl -l "http://www.baidu.com" 只显示头部信息 curl -v "http://www.baidu.com" 显示get请求全过程解析 wget "http://www.ba