问题
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