• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

【转载】C#HttpWebRequest发送SOAPXML

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

调用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;
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#执行sql存储过程发布时间:2022-07-10
下一篇:
C#常用API函数大全发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap