【问题标题】:CloseableHttpClient with BasicAuth creds returning 400 error. With same BasicAuth creds OkHttp3 client works well带有 BasicAuth 凭据的 CloseableHttpClient 返回 400 错误。使用相同的 BasicAuth 凭据 OkHttp3 客户端运行良好
【发布时间】:2022-08-04 02:27:58
【问题描述】:

我有示例代码 sn-p。它返回 400 错误。使用 OkHttp3 客户端使用相同的 BasicAuth 凭据效果很好。这里缺少什么?

```
    String BASIC_AUTHORIZAION = \"Basic *********\"; // masked
    
    String reportRequest = \"{***}\"; //json string

    CloseableHttpClient client = HttpClientBuilder.create().build();

    FormBodyPart bodyPart = FormBodyPartBuilder
        .create()
        .setName(\"ReportRequest\")
        .setBody(new StringBody(reportRequest, ContentType.APPLICATION_JSON))
        .build();

    HttpEntity requestEntity = MultipartEntityBuilder
        .create()
        .addPart(bodyPart)
        .addTextBody(\"type\", ContentType.APPLICATION_JSON.toString())
        .build();

    HttpUriRequest httpPost = RequestBuilder
        .post(MPG_MESSAGE_STATUS_REPORT_URL)
        .addHeader(\"Content-Type\", ContentType.MULTIPART_FORM_DATA.toString())
        .addHeader(\"Authorization\", BASIC_AUTHORIZAION)
        .setEntity(requestEntity)
        .build();

    HttpResponse response = client.execute(httpPost);

    标签: java okhttp basic-authentication


    【解决方案1】:

    已解决,因为它不需要为 Content-Type MULTIPART_FORM_DATA 添加标题的行:

     .addHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 2022-06-25
      • 1970-01-01
      • 2020-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多