Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用包含文件的JAVA发送Post HTTP请求_Java_Post_Apache Httpclient 4.x - Fatal编程技术网

如何使用包含文件的JAVA发送Post HTTP请求

如何使用包含文件的JAVA发送Post HTTP请求,java,post,apache-httpclient-4.x,Java,Post,Apache Httpclient 4.x,我已经做了很多谷歌搜索和尝试错误,但不能让这个工作 我正在尝试使用JAVA发送一个HTTP POST请求,其中包含一个要发送的文件。 这是我尝试过的代码,由于某些原因似乎不起作用: public void iSendARequestToAnAPI() throws Exception { //This is the httpClient that you will use to send your http request CloseableHttpClient httpClient

我已经做了很多谷歌搜索和尝试错误,但不能让这个工作

我正在尝试使用JAVA发送一个HTTP POST请求,其中包含一个要发送的文件。
这是我尝试过的代码,由于某些原因似乎不起作用:

public void iSendARequestToAnAPI() throws Exception {

  //This is the httpClient that you will use to send your http request
  CloseableHttpClient httpClient = HttpClients.createDefault();
  HttpPost httpPost = new HttpPost("https://xxxxxxxxxxxxxxxxx/document");

  httpPost.setHeader("tolerance", "strict");
  httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
  httpPost.setHeader("Authorization", "Bearer xxxx");

  File file = new File("C:\\Matt\\GBG\\drivinglicence.jpg");

  HttpEntity entity = MultipartEntityBuilder
    .create()
    .addBinaryBody("file", file, ContentType.create("image/jpeg"), file.getName())
    .build();

  httpPost.setEntity(entity);

  //Send the request
  CloseableHttpResponse response = httpClient.execute(httpPost);

  //Close the http client
  httpClient.close();

  }
}

关于信息,这是从邮递员请求中获取的curl命令,该命令有效:

curl -X POST \
  https://idaasstaging.gbgplc.com/verify/documents/v2/document \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'Postman-Token: 82966229-2e00-4309-a428-be66b4e8d8ca' \
  -H 'cache-control: no-cache' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -H 'tolerance: strict' \
  -F file=@/C:/Matt/GBG/uk-driving-licence.jpg
有人知道我做错了什么吗

我得到了如下的回应,我不确定这是什么意思。也许我只是被封锁了?不过,在邮递员请求下可以正常工作:

<html style="height:100%"><head><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><meta name="format-detection" content="telephone=no"><meta name="viewport" content="initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"></head><body style="margin:0px;height:100%"><iframe id="main-iframe" src="/_Incapsula_Resource?CWUDNSAI=22&xinfo=12-233465551-0%20NNNN%20RT%281570437116752%20119%29%20q%280%200%20-1%20-1%29%20r%281%20-1%29%20B15%281%2c501%2c51%29%20U6&incident_id=801001040412185768-821262601053865612&edet=15&cinfo=010000003634" frameborder=0 width="100%" height="100%" marginheight="0px" marginwidth="0px">Request unsuccessful. Incapsula incident ID: 801001040412185768-821262601053865612</iframe></body></html>
请求失败。Incapsula事件ID:801001040412185768-821262601053865612

“由于某些原因似乎不起作用”请提供问题/错误的确切详细信息。可能与刚才添加的更多信息重复。我收到一个奇怪的Incapsula html响应。我不知道这是什么意思。也许我只是被封锁了?