C# 使用 Web 服务的 HTTP POST
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11450836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTTP POST using web service
提问by CrBruno
I have been doing some Google searches and only getting partial successful on this topic. I was wondering if someone could suggest an example of doing an HTTP POST using C# to send XML to HTTP service.
我一直在做一些谷歌搜索,但在这个话题上只取得了部分成功。我想知道是否有人可以建议一个使用 C# 执行 HTTP POST 将 XML 发送到 HTTP 服务的示例。
I have a asmx web service that extracts data from database and I save that data to XML document. Now I have to send that XML document using SOAP protocol to HTTP service.
我有一个从数据库中提取数据的 asmx Web 服务,并将该数据保存到 XML 文档中。现在我必须使用 SOAP 协议将该 XML 文档发送到 HTTP 服务。
I have this part of code for connectig to service
我有这部分代码用于连接到服务
WebRequest myReq = WebRequest.Create("https://WEB_URL");
System.Net.ServicePointManager.CertificatePolicy = new CertificatePolicyClass();
string username = "SOMETHING";
string password = "ELSE";
string usernamePassword = username + ":" + password;
CredentialCache mycache = new CredentialCache();
mycache.Add(new Uri("https://WEB_URL"), "Basic", new NetworkCredential(username, password));
myReq.Credentials = mycache;
myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
WebResponse wr = myReq.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();
So does anybody have a code to send XML document to http service, this part I don't know how to write, I don't know am I on the write trace, I belive it has to go somethig like this
那么是否有人有将 XML 文档发送到 http 服务的代码,这部分我不知道怎么写,我不知道我是否在写跟踪中,我相信它必须像这样
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
So plese can somebody help me! THANKS!
所以请有人可以帮助我!谢谢!
采纳答案by Alley
Here is something I get, hope it's useful to you:
这是我得到的东西,希望对你有用:
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("https://WEB_URL");
myReq.Method = "POST";
myReq.ContentType = "text/xml";
myReq.Timeout = 30000;
myReq.Headers.Add("SOAPAction", ":\"#save\"");
byte[] PostData = Encoding.UTF8.GetBytes(xmlDocument);
myReq.ContentLength = PostData.Length;
using (Stream requestStream = myReq.GetRequestStream())
{
requestStream.Write(PostData, 0, PostData.Length);
}
HttpWebResponse response = (HttpWebResponse)myReq.GetResponse();
回答by Matvi
string soap =
@"<?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>
<Register xmlns=""http://tempuri.org/"">
<id>123</id>
<data1>string</data1>
</Register>
</soap:Body>
</soap:Envelope>";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost/WebServices/CustomerWebService.asmx");
req.Headers.Add("SOAPAction"http://tempuri.org/Register\"");
req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";
using (Stream stm = req.GetRequestStream())
{
using (StreamWriter stmw = new StreamWriter(stm))
{
stmw.Write(soap);
}
}
WebResponse response = req.GetResponse();
Stream responseStream = response.GetResponseStream();

