【转载】C# HttpWebRequest 发送SOAP XML

调用webservice的几种方法:

方法一:添加web引用(简单/方便 局限客户端是.net)

方法二:Post xml(本文重点讲述)

方法三:使用微软MSXML2组件(好像在window server2008上就不支持)

方法四:在页面上添加form post到服务端

//创建HttpWebRequest 实例,使用WebRequest.Create
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("服务地址");
//发送请求
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 += " <SoapHeader xmlns=\"http://www.52taiqiu.com/\">";
soap += " <Login>" + txtUser.Text + "</Login>";
soap += " <Password>" + txtPwd.Text + "</Password>";
soap += " </SoapHeader>";
soap += " </soap:Header>";

soap += "<soap:Body>";

soap += " <Request xmlns=\"http://www.52taiqiu.com/\">";
soap += " <requestXml><![CDATA[" + txtRequest.Text + "]]></requestXml>";
soap += " </Request>";
soap += "</soap:Body>";
soap += "</soap:Envelope>";

webRequest.Headers["SoapAction"] = "http://www.52taiqiu.com/Request";

//字符转字节
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;
}
时间: 2024-10-24 18:35:41

【转载】C# HttpWebRequest 发送SOAP XML的相关文章

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

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

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

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

/// <summary>        /// 发送SOAP请求,并返回响应xml        /// </summary>        /// <param name="url">请求地址</param>        /// <param name="datastr">SOAP请求信息</param>        /// <returns>返回响应信息</retur

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

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

用JAVA发送一个XML格式的HTTP请求

1 import java.io.BufferedInputStream; 2 import java.io.BufferedReader; 3 import java.io.ByteArrayOutputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.InputStreamReader; 7 import java.io.OutputStreamWriter; 8 impo

[转载]使用HttpWebRequest进行请求时发生错误:基础连接已关闭,发送时发生错误处理

转载,原文来自 http://blog.csdn.net/hawksoft/article/details/21776009 最近调试原来的微信模拟登陆时发生了“基础连接已关闭,发送时发生错误”的错误提示,原来都是好好的,只是很久没用了. 出错代码如下: HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&la

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"去

Python发送Soap报文

原文地址: http://users.skynet.be/pascalbotte/rcx-ws-doc/postxmlpython.htm # post xml soap message import sys, httplib # a "as lighter as possible" soap message: SM_TEMPLATE = """<?xml version="1.0" encoding="UTF-8&qu