How do I send arbitrary JSON data, with a custom header, to a REST server?

▼魔方 西西 提交于 2019-12-04 05:20:57
/// <summary>
    /// Creates a new instance of the <see cref="T:System.Net.Http.StringContent"/> class.
    /// </summary>
    /// <param name="content">The content used to initialize the <see cref="T:System.Net.Http.StringContent"/>.</param><param name="encoding">The encoding to use for the content.</param><param name="mediaType">The media type to use for the content.</param>
    [__DynamicallyInvokable]
    public StringContent(string content, Encoding encoding, string mediaType)
      : base(StringContent.GetContentByteArray(content, encoding))
    {
      this.Headers.ContentType = new MediaTypeHeaderValue(mediaType == null ? "text/plain" : mediaType)
      {
        CharSet = encoding == null ? HttpContent.DefaultStringEncoding.WebName : encoding.WebName
      };
    }

It's constructor of StringContent. Looks like that you should specify appropriate Encoding and mediaType

Daniil Grankin

You can't directly setup an instance of HttpContent, because it is an abstract class. You need to use one of the sub-classes, depending on your need. Most likely StringContent, which lets you set the string value of the response, the encoding, and the media type in the constructor: http://msdn.microsoft.com/en-us/library/system.net.http.stringcontent.aspx

Answer from How do I set up HttpContent for my HttpClient PostAsync second parameter?

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