/// <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请求的更多相关文章

  1. Java发布一个简单 webservice应用 并发送SOAP请求

    一.创建并发布一个简单的webservice应用 1.webservice 代码: package com.ls.demo; import javax.jws.WebMethod; import ja ...

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

    webservice框架有很多,比如axis.axis2.cxf.xFire等等,做服务端和做客户端都可行,个人感觉使用这些框架的好处是减少了对于接口信息的解析,最主要的是减少了对于传递于网络中XML ...

  3. Jmeter发送SOAP请求对WebService接口测试

    Jmeter发送SOAP请求对WebService接口测试 1.测试计划中添加一个用户自定义变量 2.HTTP信息头管理器,添加Content-Tpe,  application/soap+xml;c ...

  4. webService 发送soap请求,并解析返回的soap报文

    本例应用场景:要做一个webService测试功能,不局限于任何一种固定格式的webService,所以像axis,cxf等框架就不好用了.只有深入到webService的原理,通过发收soap报文, ...

  5. Jmeter发送soap请求

    1.新建线程组-添加SOAP/XML-RPC Request 2.我们以天气预报接口为例,http://ws.webxml.com.cn/WebServices/WeatherWS.asmx,选择最后 ...

  6. Java 发送SOAP请求调用WebService,解析SOAP报文

    https://blog.csdn.net/Peng_Hong_fu/article/details/80113196 记录测试代码 SoapUI调用路径 http://localhost:8082/ ...

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

    本文使用JAX-WS2.2编译webservice,并使用HttpUrlConnection的POST方式对wsdl发送soap报文进行请求返回数据, 对错误Server returned HTTP ...

  8. C# httpRequest Soap请求

    一般添加web服务引用是.NET用代理类模式 创建SOAP请求代理类,代理类是.NET开发工具VS自动给你生成. 下面用一般HTTP的模式有时候可能更合适,原理是构造SOAP请求的XML后POST过去 ...

  9. SoapUI使用方法-01发送http请求

    soap ui http://blog.csdn.net/russ44/article/details/51680083 一.发送HTTP请求消息 1.打开soapUI. 2.新建一个项目,实例如下: ...

随机推荐

  1. html中切记ID不能重复

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  2. ss 命令学习

    1.统计服务器并发连接数(ss性能 > netstat) time netstat -ant |grep EST|wc -l time ss -o state established | wc ...

  3. jquery动态修改div高度

    <!DOCTYPE html> <html> <head> <script src="jquery-1.4.2.min.js">&l ...

  4. git查看各个branch之间的关系图

    两种方法: 一.  使用Git log命令 git log --graph --decorate --oneline --simplify-by-decoration --all 说明: --deco ...

  5. eclipse Failed to load the JNIshared library

      eclipse Failed to load the JNIshared library CreateTime--2018年4月22日22:08:35 Author:Marydon 1.情景再现 ...

  6. 【Oracle】迁移含有BLOG类型的字段的表

    日常数据库迁移过程中,发现dmp文件中的表,如果表含有blog类型的字段,无法进行迁移. 以下是我迁移含有blog类型字段表的经验分享' 使用PLSQL Developer进行导出 接着选择需要导出的 ...

  7. SpringMVC之ModelAndView的用法(转)

    原文地址:https://blog.csdn.net/qq30211478/article/details/78016155 (一)使用ModelAndView类用来存储处理完后的结果数据,以及显示该 ...

  8. IT忍者神龟之 oracle行转列、列转行

    一.行转列 须要将例如以下格式 转换为: 这就是最常见的行转列,主要原理是利用decode函数.聚集函数(sum).结合group by分组实现的 create table test( id varc ...

  9. 将Gradle项目公布到maven仓库

    将Gradle项目公布到maven仓库 1 Gradle简单介绍 1.1 Ant.Maven还是Gradle? 1.1.1 Ant和Maven介绍 全称为Apache Maven,是一个软件(特别是J ...

  10. maven仓库介绍 牛人博客

    http://juvenshun.iteye.com/blog/359256 查找jar包方法 http://juvenshun.iteye.com/blog/269094