我这边需要做一个平台,调度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);
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);
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货