StatusCode:400 BadRequest 使用 HttpClient POST 并上传 PDF

StatusCode: 400 BadRequest using HttpClient for POST and uploading a PDF

我有问题。我在一台接受请求并进一步发送请求以进行签名的机器上创建了一个 API(必须这样做)。它像这样通过 Postman 正常工作:

headers:

但是当我尝试连接客户端(在我的示例中是默认控制台应用程序)时,我收到 400 个状态代码响应:

StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
  Transfer-Encoding: chunked
  Server: Microsoft-IIS/10.0
  X-Powered-By: ASP.NET
  Date: Fri, 04 Mar 2022 12:48:43 GMT
  Content-Type: application/problem+json; charset=utf-8
}

调用的代码是:

static async void SendPDFToEsign2()
    {
        var fileInfo = new FileInfo(@"C:\Users\Raul\Documents\PDFReadTest\Test.pdf");
        var participantSSN = "14812149181";

        var postContent = new MultipartFormDataContent();
        var fileContent = new StreamContent(new FileStream(fileInfo.FullName, FileMode.Open));
        fileContent.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/pdf");
        postContent.Headers.ContentType = new MediaTypeWithQualityHeaderValue("multipart/form-data");
        postContent.Add(fileContent, "receivedPdf");
        postContent.Add(new StringContent(participantSSN), "signerID");


        var client = new HttpClient();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/form-data"));
        client.BaseAddress = new Uri("https://esign.testapp.uib.no");

        var result = client.PostAsync("/api/", postContent).Result;

        Console.WriteLine(result);
    }

我已经尝试了很多我发现的解决方案,还有

using (var client = new HttpClient())
        {
            using (var fs = File.OpenRead(pathtofile))
            {
                //code to send it
            }
        }

但在这种情况下似乎没有任何效果?如果你认为这很重要,被调用的 API 看起来像 this,但它通过 postman 正常工作,所以我只想通过 c# 重新创建 postman 调用,这样我就可以开始将它集成到主程序中系统.

--> 编辑 <--

设法获得这样的 200 OK 响应:

    var url = new Uri("https://esign.testapp.uib.no");
    var participantSSN = "14812149181";
    var fileInfo = new FileInfo(@"C:\Users\Raul\Documents\PDFReadTest\Test.pdf");
    using var client = new HttpClient();
    using var content = new ByteArrayContent(File.ReadAllBytes(fileInfo.FullName));
    var postContent = new MultipartFormDataContent();
    postContent.Add(content, "receivedPdf", "Test.pdf");
    postContent.Add(new StringContent(participantSSN), "signerID");
    content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
    var requestUri = "/api/";
    client.BaseAddress = url;

    var result = client.PostAsync(requestUri, postContent).Result;

    Console.WriteLine(result);
    Console.WriteLine($"Response : {result.StatusCode}");

但是我无法得到我需要的 JSON 响应(上面 Postman 图片中显示的响应)。我只能得到这个:

知道如何获得 Postman 给我的良好回复吗?

---->>> 编辑 2:已修复 <<<---- 我使用以下代码得到了正确的结果:

public static async Task<HttpResponseMessage> PostPDF(System.IO.FileStream fsPDF, Participant participant)
        {
            // setup
            using var client = new HttpClient();
            var url = new Uri("https://esign.testapp.uib.no");
            using var fileContent = new StreamContent(fsPDF);
            fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
            var requestUri = "/api/";
            client.BaseAddress = url;

            // add auth token
            //var _bearerToken = "";
            //client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _bearerToken);

            // setup request data 
            MultipartFormDataContent httpContent = new MultipartFormDataContent();
            httpContent.Add(fileContent, "receivedPdf", "Test4.pdf"); // Hardcoded Name Test4
            httpContent.Add(new StringContent(participant.SocialSecurityNo), "signerID");

            // make the call
            var response = client.PostAsync(requestUri, httpContent);

            // return the response. perhaps save it in the DB too
            return response.Result;
        }

希望这对某人有所帮助!

代码看起来很合理。

如果文件为空或太大,API 似乎会 return 400。我打赌你已经检查过它不是 > 5MB,所以我的猜测是文件实际上没有被读取,你正在发送 0 字节的 PDF。

您是否通过检查内容来检查实际发生了什么?

检查最后的编辑,我自己修复了它。对此发表评论,以便我可以将问题标记为已解决