C# 将主体添加到与 azure 服务 mgmt api 一起使用的 HttpWebRequest

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9153181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:10:48  来源:igfitidea点击:

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

c#azureconfiguration-files

提问by StevenR

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

我将如何添加到 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

任何帮助深表感谢

采纳答案by L.B

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

回答by BrokenGlass

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

不知道 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);

If you don't needa HttpWebRequestfor some reason, using a WebClientfor uploading data is much more concise:

如果您出于某种原因不需要a HttpWebRequest,则使用 aWebClient上传数据要简洁得多:

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

回答by Neil Mackenzie

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

我的书籍章节展示了如何使用 Windows Azure 服务管理 API(并创建有效负载)可以免费下载