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

随声附和 提交于 2019-12-22 03:15:29

问题


How would i go about adding to the body of a HttpWebRequest?

The body needs to be made up of the following

<?xml version="1.0" encoding="utf-8"?>
<ChangeConfiguration xmlns="http://schemas.microsoft.com/windowsazure">
   <Configuration>base-64-encoded-configuration-file</Configuration>
   <TreatWarningsAsError>true|false</TreatWarningsAsError>
   <Mode>Auto|Manual</Mode>
</ChangeConfiguration>

Any help is much appreciated


回答1:


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();



回答2:


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);
}



回答3:


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



来源:https://stackoverflow.com/questions/9153181/adding-a-body-to-a-httpwebrequest-that-is-being-used-with-the-azure-service-mgmt

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