首页 > 解决方案 > 带有自定义标头的 C# HttpClient POST 请求发送不正确的 Content-Type 标头字段

问题描述

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

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

httpRequestMessage:方法:POST,RequestUri:'http://localhost:9998/',版本:1.1,内容:System.Net.Http.ByteArrayContent,标头:{
ContentType:application/vnd.com.documents4j.any-msword Accept : application/pdf Converter-Job-Priority: 1000 }response:StatusCode: 500, ReasonPhrase: 'Request failed.', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Connection: close Date: Sat, 2021 年 4 月 10 日 22:39:24 GMT 内容长度:1031 内容类型:文本/html;charset=ISO-8859-1 }按任意键继续。. .

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

我有使用 RestSharp 并正确发送 Content-Type 并返回成功结果的 C# 代码。我有用 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;
        }
    }
}

标签: c#httpclientcontent-type

解决方案


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

httpRequestMessage:方法:POST,RequestUri:'http://localhost:9998/',版本:1.1,内容:System.Net.Http.ByteArrayContent,标头:{
ContentType:application/vnd.com.documents4j.any-msword Accept : application/pdf Converter-Job-Priority: 1000 Content-Type: application/vnd.com.documents4j.any-msword }response:StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http .StreamContent, Headers: { Vary: Accept-Encoding
Transfer-Encoding: chunked Date: Mon, 12 Apr 2021 03:04:14 GMT
Content-Type: application/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;
}

推荐阅读