C# 使用IHttpContent发出HttpClient.PutAsync请求

C# 使用IHttpContent发出HttpClient.PutAsync请求,c#,httprequest,httpclient,put,httpcontent,C#,Httprequest,Httpclient,Put,Httpcontent,如何创建内容为PutAsync请求正文的IHttpContent对象 请求如下: public class HttpJsonContent : IHttpContent { IJsonValue jsonValue; HttpContentHeaderCollection headers; public HttpContentHeaderCollection Headers { get { return heade

如何创建内容为PutAsync请求正文的IHttpContent对象

请求如下:

    public class HttpJsonContent : IHttpContent {
        IJsonValue jsonValue;
        HttpContentHeaderCollection headers;

        public HttpContentHeaderCollection Headers {
            get { return headers; }
        }

        public HttpJsonContent(IJsonValue jsonValue) {
            if (jsonValue == null) {
                throw new ArgumentException("jsonValue cannot be null.");
            }

            this.jsonValue = jsonValue;
            headers = new HttpContentHeaderCollection();
            headers.ContentType = new HttpMediaTypeHeaderValue("application/json");
            headers.ContentType.CharSet = "UTF-8";
        }
    ...
// Optionally, define HTTP Headers
httpClient.DefaultRequestHeaders.Accept.TryParseAdd("application/json");

JsonObject jsonObj = new JsonObject();
jsonObj["content_one"] = JsonValue.CreateNumberValue(600);
jsonObj["content_two"] = JsonValue.CreateStringValue("my content value");

// Create the IHttpContent
IHttpContent jsonContent = new HttpJsonContent(jsonObj);

// Make the call
HttpResponseMessage response = await httpClient.PutAsync(uri, jsonContent);

我正在使用Windows.Web.HttpClient库。

通过我找到的示例,我设法解决了我的问题。 我用IHttpContent接口实现了一个新类,并对我的内容使用了Json.Parse。类接口如下所示:

    public class HttpJsonContent : IHttpContent {
        IJsonValue jsonValue;
        HttpContentHeaderCollection headers;

        public HttpContentHeaderCollection Headers {
            get { return headers; }
        }

        public HttpJsonContent(IJsonValue jsonValue) {
            if (jsonValue == null) {
                throw new ArgumentException("jsonValue cannot be null.");
            }

            this.jsonValue = jsonValue;
            headers = new HttpContentHeaderCollection();
            headers.ContentType = new HttpMediaTypeHeaderValue("application/json");
            headers.ContentType.CharSet = "UTF-8";
        }
    ...
// Optionally, define HTTP Headers
httpClient.DefaultRequestHeaders.Accept.TryParseAdd("application/json");

JsonObject jsonObj = new JsonObject();
jsonObj["content_one"] = JsonValue.CreateNumberValue(600);
jsonObj["content_two"] = JsonValue.CreateStringValue("my content value");

// Create the IHttpContent
IHttpContent jsonContent = new HttpJsonContent(jsonObj);

// Make the call
HttpResponseMessage response = await httpClient.PutAsync(uri, jsonContent);
我的要求是:

    public class HttpJsonContent : IHttpContent {
        IJsonValue jsonValue;
        HttpContentHeaderCollection headers;

        public HttpContentHeaderCollection Headers {
            get { return headers; }
        }

        public HttpJsonContent(IJsonValue jsonValue) {
            if (jsonValue == null) {
                throw new ArgumentException("jsonValue cannot be null.");
            }

            this.jsonValue = jsonValue;
            headers = new HttpContentHeaderCollection();
            headers.ContentType = new HttpMediaTypeHeaderValue("application/json");
            headers.ContentType.CharSet = "UTF-8";
        }
    ...
// Optionally, define HTTP Headers
httpClient.DefaultRequestHeaders.Accept.TryParseAdd("application/json");

JsonObject jsonObj = new JsonObject();
jsonObj["content_one"] = JsonValue.CreateNumberValue(600);
jsonObj["content_two"] = JsonValue.CreateStringValue("my content value");

// Create the IHttpContent
IHttpContent jsonContent = new HttpJsonContent(jsonObj);

// Make the call
HttpResponseMessage response = await httpClient.PutAsync(uri, jsonContent);

查看上的备注部分。我认为这是一个好主意,但我建议不要手动构建json字符串。至少,使用JavaScriptSerializer类将匿名类型转换为json字符串。您还可以将HttpJsonContent类设置为泛型,这样您就可以在类内部进行解析了。答案已经更新。