带有自定义标头的 C# HttpClient POST 请求发送不正确的 Content-Type 标头字段

时间:2021-04-11 21:52:46

标签: c# httpclient content-type

我在 Windows 10 上的 Visual Studio 中使用 C# 编写了示例代码,该代码尝试将带有自定义标头的 POST 请求发送到在 http://localhost:9998 上运行的服务,但失败了。

当我查看请求时,Content-Type 标头字段将作为 ContentType(无连字符)发送。

<块引用>

httpRequestMessage:Method: POST, RequestUri: 'http://localhost:9998/', 版本:1.1,内容:System.Net.Http.ByteArrayContent,标题:{
内容类型:application/vnd.com.documents4j.any-msword 接受: application/pdf Converter-Job-Priority: 1000 }response:StatusCode: 500,ReasonPhrase:'请求失败。',版本:1.1,内容: System.Net.Http.StreamContent,标题:{ 连接:关闭日期: 2021 年 4 月 10 日星期六 22:39:24 GMT 内容长度:1031 内容类型: 文本/html; charset=ISO-8859-1 }按任意键继续。 . .

我想知道这是否是问题的原因?

我用 C# 编写的代码使用 RestSharp 并正确发送 Content-Type 并返回成功的结果。 我用 Java 编写的代码也可以正确发送 Content-Type 并返回成功的结果。

示例代码 1 [将 Content-Type 作为 ContentType 发送的问题]

using System;
using System.Net;
using System.IO;
using System.Net.Http;

namespace HttpPOST10
{
    class Program
    {
        public static string MyUri { get; private set; }
        static void Main(string[] args)
        {
//            string url = "http://localhost:9998";
            string url = "http://localhost:8888"; // Fiddler
            Uri myUri = new Uri(url);
            string srcFilename = @"C:\temp2\Sample.doc";
            string destFileName = @"C:\temp3\Sample-HttpPOST10.pdf";

            UploadFile(url, srcFilename, destFileName);
        }
        private static bool UploadFile(string url, string srcFilename, string destFileName)
        {
            HttpClient httpClient = new HttpClient();
            byte[] data;
            data = File.ReadAllBytes(srcFilename);
            var httpRequestMessage = new HttpRequestMessage
            {
                Method = HttpMethod.Post,
                RequestUri = new Uri(url),
                Headers = {
                    { HttpRequestHeader.ContentType.ToString(), "application/vnd.com.documents4j.any-msword" },
                    { HttpRequestHeader.Accept.ToString(), "application/pdf" },
                    { "Converter-Job-Priority", "1000" },
//                    {"User-Agent",  "RestSharp/106.11.8.0" }
                },
                Content = new ByteArrayContent(data)
            };
            Console.Write("httpRequestMessage:" + httpRequestMessage);
            var response = httpClient.SendAsync(httpRequestMessage).Result;
            Console.Write("response:" + response);

            return true;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

谢谢 Jimi - 现在得到了成功的回复。

<块引用>

httpRequestMessage:Method: POST, RequestUri: 'http://localhost:9998/', 版本:1.1,内容:System.Net.Http.ByteArrayContent,标题:{
内容类型:application/vnd.com.documents4j.any-msword 接受: application/pdf Converter-Job-Priority: 1000 内容类型: application/vnd.com.documents4j.any-msword }response:StatusCode: 200, ReasonPhrase:'OK',版本:1.1,内容: System.Net.Http.StreamContent,标题:{ 变化:接受编码
传输编码:分块 日期:2021 年 4 月 12 日星期一 03:04:14 GMT
内容类型:应用程序/pdf

代码更改为:

private static bool UploadFile(string url, string srcFilename, string destFileName)
{
    HttpClient httpClient = new HttpClient();
    byte[] data;
    data = File.ReadAllBytes(srcFilename);

    HttpContent content = new ByteArrayContent(data);
    content.Headers.Add("Content-Type", "application/vnd.com.documents4j.any-msword");

    var httpRequestMessage = new HttpRequestMessage
    {
        Method = HttpMethod.Post,
        RequestUri = new Uri(url),
        Headers = {
            { HttpRequestHeader.ContentType.ToString(), "application/vnd.com.documents4j.any-msword" },
            { HttpRequestHeader.Accept.ToString(), "application/pdf" },
            { "Converter-Job-Priority", "1000" },
        },
        Content = content
    };

    Console.Write("httpRequestMessage:" + httpRequestMessage);
                var response = httpClient.SendAsync(httpRequestMessage).Result;
    Console.Write("response:" + response);

    return true;
}