【问题标题】:Java code for HTTP post request to send two(certificate and certificateKey) files as multipart-formdata用于将两个(证书和证书密钥)文件作为 multipart-formdata 发送的 HTTP 发布请求的 Java 代码
【发布时间】:2019-06-20 05:46:35
【问题描述】:

我需要使用 JAVA 将两个带有 post 请求的文件作为多部分表单数据发送,但我收到此代码的 BAD REQUEST 400。

RESPONSE I GET -- 
{
  "status" : "BAD_REQUEST",
  "message" : "Failed to parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found",
  "errors" : null,
  "timestamp" : "2019-06-20 05:48:31"
}

我为此使用了 apache HttpComponents

File certFile = new File("/home/vijay/Downloads/facilio.crt");
    FileBody data = new FileBody(certFile);

    File keyFile = new File("/home/vijay/Downloads/facilio-private.key");
    FileBody data2 = new FileBody(keyFile);

    HttpClient httpclient = new DefaultHttpClient();
    String authString = username + ":" + password;
    System.out.println("auth string: " + authString);
    byte[] authEncBytes =Base64.getEncoder().encode(authString.getBytes());
    String authStringEnc = new String(authEncBytes);
    System.out.println("Base64 encoded auth string: " + authStringEnc);
    HttpPost httpPost = new HttpPost("https://api.xxxx/xxx/xxx");
    httpPost.addHeader(HttpHeaders.AUTHORIZATION, "Basic "+authStringEnc);
    // httpPost.addHeader("Accept", "application/json");
    System.out.println(" content type -"+ContentType.MULTIPART_FORM_DATA.getMimeType());
    httpPost.addHeader(HttpHeaders.CONTENT_TYPE,ContentType.MULTIPART_FORM_DATA.getMimeType());



    MultipartEntity reqEntity = new MultipartEntity();
    MultipartEntity reqEntity1 = new MultipartEntity();

    reqEntity.addPart("certificate", data);
    reqEntity1.addPart("ceritificateKey",data2);
    httpPost.setEntity(reqEntity);
    httpPost.setEntity(reqEntity1);
    try {
        HttpResponse response = httpclient.execute(httpPost);
        System.out.println("code-------"+response.getStatusLine().getStatusCode());
        System.out.println(response.toString());
        HttpEntity ent = response.getEntity();
        InputStream is = ent.getContent();
        InputStreamReader isr = new InputStreamReader(is);
        int numCharsRead;
        char[] charArray = new char[1024];
        StringBuffer sb = new StringBuffer();
        while ((numCharsRead = isr.read(charArray)) > 0) {
            sb.append(charArray, 0, numCharsRead);
        }
        String result = sb.toString();

        System.out.println("*** BEGIN ***");
        System.out.println(result);
        System.out.println("*** END ***");

    } catch (IOException e) {
        e.printStackTrace();
    }

我应该得到 200 响应字符串。非常感谢任何建议或代码审查。提前致谢

【问题讨论】:

标签: java apache api http multipartform-data


【解决方案1】:

不确定这是您代码中的唯一问题,但 MultipartEntity,顾名思义,它包含一个多部分实体。请求只需要一个:

MultipartEntity reqEntity = new MultipartEntity();

reqEntity.addPart("certificate", data);
reqEntity.addPart("ceritificateKey",data2);
httpPost.setEntity(reqEntity);

【讨论】:

    猜你喜欢
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-24
    • 2012-04-18
    相关资源
    最近更新 更多