繁体   English   中英

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

[英]C# HttpClient POST Request with Custom Headers sends incorrect Content-Type header field

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

当我查看请求时, Content-Type header 字段作为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,标题:{ 连接:关闭日期:星期六,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;
        }
    }
}

谢谢 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: 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM