C#怎么发送soap报文的请求

Iovswety 2013-05-21 01:39:51
我这边需要做一个平台,调度WebService请求以及监控服务的流量跟使用次数等这些操作,现在碰到个问题,用http协议post请求webService,怎么构造post的soap的报文呢??

比如我某一个webService,soap 1.2请求格式是这样

POST /MyServices.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/HelloWorld"

<?xml version="1.0" encoding="utf-8"?>
<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:Body>
<HelloWorld xmlns="http://tempuri.org/">
<abc>
<a>string</a>
<b>int</b>
<c>
<string>string</string>
<string>string</string>
</c>
</abc>
</HelloWorld>
</soap:Body>
</soap:Envelope>


我是这样写的,之后报了400错误,麻烦各位牛人帮帮忙看看。


string data = string.Format(@"
<?xml version=""1.0"" encoding=""utf-8""?>
<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:Body>
<HelloWorld xmlns=""http://tempuri.org/"">
<abc>
<a>纳菲恩</a>
<b>123</b>
<c>
<string>gg</string>
<string>ff</string>
<string>火锅</string>
<string>办法</string>
</c>
</abc>
</HelloWorld>
</soap:Body>
</soap:Envelope>");

HttpWebRequest wrt = (HttpWebRequest)WebRequest.Create("http://localhost:9542/MyServices.asmx");
wrt.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
wrt.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8");
wrt.Headers.Add("Cache-Control", "max-age=0");
wrt.Headers.Add("SOAPAction", "http://tempuri.org/HelloWorld");
wrt.ContentType = "text/xml; charset=utf-8";
wrt.Method = "POST";
wrt.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22";
wrt.Referer = "http://localhost:9542/MyServices.asmx?op=HelloWorld";
wrt.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
wrt.Connection = "Open";

byte[] byteArray = Encoding.UTF8.GetBytes(data);
wrt.ContentLength = byteArray.Length;
Stream stream = wrt.GetRequestStream();
stream.Write(byteArray, 0, byteArray.Length);
stream.Close();
string html = ReadZipData(wrt);
...全文
162 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Iovswety 2013-05-21
  • 打赏
  • 举报
回复
走过路过的牛人看进来瞧瞧啊!!
Iovswety 2013-05-21
  • 打赏
  • 举报
回复
走过路过的牛人看进来瞧瞧啊!!

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧