将正文添加到与azure服务mgmt api一起使用的HttpWebRequest

Ste*_*enR 12 c# configuration-files azure

我将如何添加到HttpWebRequest的主体?

身体需要由以下组成

<?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>
Run Code Online (Sandbox Code Playgroud)

任何帮助深表感谢

L.B*_*L.B 27

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();
Run Code Online (Sandbox Code Playgroud)


Bro*_*ass 6

不了解Azure,但这里只是发送数据的一般大纲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);
Run Code Online (Sandbox Code Playgroud)

如果没有需要一个HttpWebRequest出于某种原因,使用WebClient上传的数据更简洁:

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