一般添加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. android 电话监听和拦截

    一.首先在manifest.xml文件中获取监听电话权限,注册监听电话的Activity <receiver android:name=".PhoneReceiver"> ...

  2. [JAVA] Android用到的一些文件操作

    // 获得某个文件夹folderPath下面某种文件后缀fileType的所有文件名 public static List<String> getFileNamesInFolder(Str ...

  3. .NET(C#)使用Serialize、Deserialize序列和反序列化XML文档

    本文给大家分享一下C#操作(读取.写入)XML文档的实用方法,即用.NET本身提供的Deserialize和Serialize进行反序列化和序列化XML文档.这种方法主要是对比较规范的XML文档进行操 ...

  4. Oracle EBS INV 查询物料无值 ECO

    查找物料的时候报错 没有输入值 解决方法: 针对FORM做trace 多查看几个生成的trace 搜索 MTL_SYSTEM_ITEMS_b 的信息 查看到最后面的语句(一般可直接查看) 看SQL 哪 ...

  5. Linux中 /proc/[pid] 目录各文件简析

    Linux 内核提供了一种通过 proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc 文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以文件系统的方式为访问系 ...

  6. idea 2017破解方法

    http://blog.csdn.net/zx110503/article/details/78734428

  7. Oracle 截取字符串(截取固定分隔符中间的字符

    #### Oracle 截取字符串(截取固定分隔符中间的字符) #### ####  oracle 取固定分隔符之间的字符--方法一 substr+ instrSELECT  substr('12JP ...

  8. MyBatis 中的级联

    MyBatis 的级联分为 3 种. 1.鉴别器(discriminator):它是根据某些条件决定采用具体实现类级联的方案,比如体检表要根据性别去区分. 2.一对一(association):比如学 ...

  9. Django商城项目笔记No.13用户部分-用户中心个人信息

    首先处理个人信息的显示 邮箱绑定: 首先给用户的模型类里添加一个字段来说明用户的邮箱是否激活 然后数据库迁移 python manage.py makemigrations python manage ...

  10. JQUERY方法给TABLE动态增加行

    比如设置table的id为tabvar trHTML = "<tr><td>...</td></tr>"$("#tab&q ...