2021年4月11日星期日

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

I have sample code written in C# in Visual Studio on Windows 10 that attempts to send a POST request with custom headers to a service running at http://localhost:9998 which is failing.

When I look at the request the Content-Type header field is being sent as ContentType (no hyphen).

httpRequestMessage:Method: POST, RequestUri: 'http://localhost:9998/', Version: 1.1, Content: System.Net.Http.ByteArrayContent, Headers: {
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, 10 Apr 2021 22:39:24 GMT Content-Length: 1031 Content-Type: text/html; charset=ISO-8859-1 }Press any key to continue . . .

I am wondering if this is the cause of the problem?

I have code written in C# that uses RestSharp and that sends Content-Type correctly and returns a successful result. I have code written in Java that also sends Content-Type correctly and returns a successful result.

Sample Code 1 [Problem sending Content-Type as 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;          }      }  }  
https://stackoverflow.com/questions/67050528/c-sharp-httpclient-post-request-with-custom-headers-sends-incorrect-content-type April 12, 2021 at 05:52AM

没有评论:

发表评论