我在XML文件中有SOAP请求.我想将请求发布到.net中的Web服务如何实现?
var uri = new Uri("http://localhost/SOAP/SOAPSMS.asmx/add");
var req = (HttpWebRequest) WebRequest.CreateDefault(uri);
req.ContentType = "text/xml; charset=utf-8";
req.Method = "POST";
req.Accept = "text/xml";
req.Headers.Add("SOAPAction", "http://localhost/SOAP/SOAPSMS.asmx/add");
var strSoapMessage = @"
235
";
using (var stream = new StreamWriter(req.GetRequestStream(), Encoding.UTF8))
stream.Write(strSoapMessage);
我做过类似的事情,手动构建一个xml请求,然后使用webrequest对象提交请求:
string data = "the xml document to submit";
string url = "the webservice url";
string response = "the response from the server";
// build request objects to pass the data/xml to the server
byte[] buffer = Encoding.ASCII.GetBytes(data);
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = buffer.Length;
Stream post = request.GetRequestStream();
// post data and close connection
post.Write(buffer, 0, buffer.Length);
post.Close();
// build response object
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream responsedata = response.GetResponseStream();
StreamReader responsereader = new StreamReader(responsedata);
response = responsereader.ReadToEnd();
代码开头的字符串变量就是你设置的,然后你从服务器得到一个字符串响应(希望......).