使用Web服务的HTTP POST [英] HTTP POST using web service
问题描述
我一直在做一些谷歌搜索,只得到关于这个主题的部分成功。我想知道,如果有人可以建议做使用C#发送XML到HTTP服务的HTTP POST的例子。
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.
我有一个从数据库中提取数据的ASMX Web服务我保存这些数据的XML文档。现在我有使用SOAP协议HTTP服务发送的XML文档。
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.
我有这部分代码为connectig以服务
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();
所以,没有任何人有一个代码,XML文档发送到http服务,这部分我不知道怎么写,我不知道我是在写操作痕迹,我相信它有去somethig这样
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
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
所以,普莱舍有人可以帮我! !谢谢
So plese can somebody help me! THANKS!
推荐答案
下面是我得到的,希望这是对你有用:
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();
这篇关于使用Web服务的HTTP POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!