一般添加web服务引用是.NET用代理类模式 创建SOAP请求代理类,代理类是.NET开发工具VS自动给你生成。

下面用一般HTTP的模式有时候可能更合适,原理是构造SOAP请求的XML后POST过去:

下面是HelloWorld的例子

private void button1_Click(object sender, EventArgs e)
{ //创建HttpWebRequest 实例,使用WebRequest.Create
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://localhost:1198/WebSite1/Service.asmx");
//发送请求
webRequest.Method = "POST";
//编码
webRequest.ContentType = "text/xml";
string soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
soap += "<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 += " <soap:Header>";
soap += " </soap:Header>";
soap += "<soap:Body>";
soap += " <HelloWorld xmlns=\"http://tempuri.org/\" />";
soap += "</soap:Body>";
soap += "</soap:Envelope>";
// webRequest.Headers["SoapAction"] = "http://localhost:1198/WebSite1/Service.asmx"; //字符转字节
byte[] bytes = Encoding.UTF8.GetBytes(soap);
Stream writer = webRequest.GetRequestStream();
writer.Write(bytes, 0, bytes.Length);
writer.Flush();
writer.Close();
string result = "";
//返回 HttpWebResponse
try
{
HttpWebResponse hwRes = webRequest.GetResponse() as HttpWebResponse;
if (hwRes.StatusCode == System.Net.HttpStatusCode.OK)
{//是否返回成功
Stream rStream = hwRes.GetResponseStream();
//流读取
StreamReader sr = new StreamReader(rStream, Encoding.UTF8);
result = sr.ReadToEnd();
sr.Close();
rStream.Close();
}
else
{
result = "连接错误";
}
//关闭
hwRes.Close();
txtResponse.Text = result;
}
catch (System.Net.WebException ex)
{
String responseFromServer = ex.Message.ToString() + " ";
if (ex.Response != null)
{
using (WebResponse response = ex.Response)
{
Stream data = response.GetResponseStream();
using (StreamReader reader = new StreamReader(data))
{
responseFromServer += reader.ReadToEnd();
}
}
}
txtResponse.Text = responseFromServer;
} }

  

不错意外会返回如下:

<?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><HelloWorldResponse xmlns="http://tempuri.org/"><HelloWorldResult>Hello World</HelloWorldResult></HelloWorldResponse></soap:Body></soap:Envelope>

  

不构造XML结构请求也是可以的:

/*
/*POST /WebSite1/Service.asmx/HelloWorld HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length
*/
//创建HttpWebRequest 实例,使用WebRequest.Create
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://localhost:1198/WebSite1/Service.asmx/HelloWorld");
//发送请求
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength=0;
string soap = "";//请求参数如 soap = "str=wgscd&num=123";
//字符转字节
byte[] bytes = Encoding.UTF8.GetBytes(soap);
Stream writer = webRequest.GetRequestStream();
writer.Write(bytes, 0, bytes.Length);
writer.Flush();
writer.Close();
string result = "";
//返回 HttpWebResponse
try
{
HttpWebResponse hwRes = webRequest.GetResponse() as HttpWebResponse;
if (hwRes.StatusCode == System.Net.HttpStatusCode.OK)
{//是否返回成功
Stream rStream = hwRes.GetResponseStream();
//流读取
StreamReader sr = new StreamReader(rStream, Encoding.UTF8);
result = sr.ReadToEnd();
sr.Close();
rStream.Close();
}
else
{
result = "连接错误";
}
//关闭
hwRes.Close();
txtResponse.Text = result;
}
catch (Exception ex)
{
txtResponse.Text = ex.Message ; }

  

会返回:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>

  

不构造XML请求结构的一个死穴是,那种传人参数是某个对象/类的情况就GG了。

如:

[WebMethod]
public DataRet HelloWorld(DataRequest dt)
{

DataRet d =new DataRet();
return d;
}

C# httpRequest Soap请求的更多相关文章

  1. Axis2(10):使用soapmonitor模块监视soap请求与响应消息

    在Axis2中提供了一个Axis2模块(soapmonitor),该模块实现了与<WebService大讲堂之Axis2(9):编写Axis2模块(Module)>中实现的logging模 ...

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

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

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

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

  4. PHP用post来进行Soap请求

    最近调了一个Soap请求C# webservice的项目.网上坑不少. 使用原生的SoapClient库请求也是失败.只好用post来进行模拟了.代码贴出来,给大家参考一下. <?php nam ...

  5. [Postman]发出SOAP请求(18)

    使用Postman发出SOAP请求: 将SOAP端点作为URL.如果您使用的是WSDL,那么请将WSDL的路径作为URL. 将请求方法设置为POST. 打开原始编辑器,并将正文类型设置为“text / ...

  6. java 查看SOAP请求报文

    log.info("ESB 请求URL = " + cachedEndpoint.toString());//打印SOAP请求报文 add by LinJC on 20170120 ...

  7. iOS webservice SOAP 请求

    1. Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的.专门的第三方软件或硬件, 就可相互交换数据或集成.依据Web Service规范实施的应用之间, 无论它们所使用的语 ...

  8. [转]C#通过Http发送Soap请求

    /// <summary>        /// 发送SOAP请求,并返回响应xml        /// </summary>        /// <param na ...

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

    java模拟Soap请求测试Web Service接口,发现Web Service响应内容中的中文竟然是编码格式.比如: 中文:退保成功 Soap中文编码:退保成功   我仔细分析后发现,退编码实际上 ...

随机推荐

  1. django 关于render的返回数据

    1,问题探讨 : 通过ajax 发送请求,接受render返回的数据.到底是什么样的类型呢? def text(request): # v = reverse("test") # ...

  2. [Android] 实现简单的相机程序

    好久没写了,有些东西做过都快忘了,赶紧记一下. 现在来实现一个简单的相机程序. 原文地址http://www.cnblogs.com/rossoneri/p/4246134.html 当然需要的话可以 ...

  3. CentOS6.8系统安装Node

    环境:CentOS6.8_X64系统 一.到官方下载最新的编译好的安装文件,目前是6.9.4. $>cd /usr/local/src #定位到这个目录,下载的文件会在这个目录#使用wget下载 ...

  4. 蓝魔i7s刷机

    ,电脑管家,豌豆荚之类的PC工具 5. 安装MFT6.0.43.exe, 注意:MFT6.0.43需要对应的ISOC和USB驱动,不可使用其它版本 6. 将CUSTOM_CONFIG..INI文件复制 ...

  5. Oracle EBS INV 创建物料搬运单

    Create or Replace PROCEDURE ProcessMoveOrder AS -- Common Declarations l_api_version NUMBER := 1.0; ...

  6. 跨过Django的坑

    在最近的Django的学习中,慢慢的开始踩坑,开此栏,专为收纳Django的坑,在以后的学习中以便警示.(使用工具为pycharm专业版2018.2.4,python3.5.2,Django版本2.1 ...

  7. linux stat 简单介绍

    stat 命令查看文件或文件系统的状态时间等属性 用法:stat [参数]... 文件... 简单的介绍一下stat命令显示出来的文件其他信息: - File:显示文件名 - Size:显示文件大小 ...

  8. You are not late! You are not early!

    Do you think you are going No Where in Life? STOP! Take a deep breathe THINK! New York is three hour ...

  9. ArcGIS pro2.3中添加天地图底图

    应用背景: 很多时候,我们需要使用网络上的遥感影像或者百度地图.天地图等在线地图做一些矢量化工作或者其他. 笔者见过很多人都是把百度地图截图,然后把图片导如Arcmap或者Arcgis pro中,然后 ...

  10. 阿里八八Alpha阶段Scrum(2/12)

    今日进度 叶文滔: 11.1:搭建Andriod Studio开发环境 11.2:已经完成Alpha阶段的APP整体框架搭建. 11.3:根据会议讨论内容,增加了模块标题栏返回键. 王国超: 完成了多 ...