Adding a body to a HttpWebRequest that is being used with the azure service mgmt api

主宰稳场 提交于 2019-12-05 00:09:40
byte[] buf = Encoding.UTF8.GetBytes(xml);

request.Method = "POST";
request.ContentType = "text/xml";
request.ContentLength = buf.Length;
request.GetRequestStream().Write(buf, 0, buf.Length);

var HttpWebResponse = (HttpWebResponse)request.GetResponse();

Don't know about Azure, but here just the general outline to send data with a HttpWebRequest :

string xml = "<someXml></someXml>";
var payload = UTF8Encoding.UTF8.GetBytes(xml);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://foo.com");
request.Method = "POST";
request.ContentLength = payload.Length;
using(var stream = request.GetRequestStream())
stream.Write(payload, 0, payload.Length);

If you don't need a HttpWebRequest for some reason, using a WebClient for uploading data is much more concise:

using (WebClient wc = new WebClient())
{
    var result = wc.UploadData("http://foo.com", payload);
}

My book chapter showing how to use the Windows Azure Service Management API (and create the payload) can be downloaded for free.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!