我正在使用HttpClient使用以下代码发送POST多部分请求
String apiUrl = API_URL_DEV;
String name = API_USER_DEV;
String password = API_PASSWORD_DEV;
String authString = name + ":" + password;
String encoding = Base64.getEncoder().encodeToString(authString.getBytes());
apiUrl = "https://<servicenow host>/api/now/attachment/upload";
try {
Header header = new BasicHeader(HttpHeaders.CONTENT_TYPE, "multipart/form-data");
Header header2 = new BasicHeader(HttpHeaders.ACCEPT, "application/json");
Header header3 = new BasicHeader(HttpHeaders.AUTHORIZATION, "Basic " + encoding);
List<Header> headers = new ArrayList<Header>();
headers.add(header);
headers.add(header2);
headers.add(header3);
HttpHost proxy = new HttpHost("xxxx", 8080, "http");
CloseableHttpClient client = HttpClientBuilder.create()
.setDefaultHeaders(headers)
.setProxy(proxy)
.build();
HttpPost post = new HttpPost(apiUrl);
String textFileName = "C:/Heena_Code/Test.xlsx";
File file = new File(textFileName);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
StringBody stringBody1 = new StringBody("change_request", ContentType.MULTIPART_FORM_DATA);
StringBody stringBody2 = new StringBody("a81c6a1ddb2948d04af824f4059619a9", ContentType.MULTIPART_FORM_DATA);
builder.addPart("table_name", stringBody1);
builder.addPart("table_sys_id", stringBody2);
builder.addPart("uploadFile", fileBody);
HttpEntity entity = builder.build();
post.setEntity(entity);
// Execute HTTP Post Request
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = client.execute(post, responseHandler); -----> exception here
System.out.println(responseBody);
获取异常
严重:org.apache.http.ConnectionClosedException:块编码消息正文的过早结束:预期关闭块 在org.apache.http.impl.io.ChunkedInputStream.getChunkSize(ChunkedInputStream.java:263) 在org.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream.java:222) 在org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:183) 在org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:210) 在org.apache.http.impl.io.ChunkedInputStream.close(ChunkedInputStream.java:312) 在org.apache.http.impl.execchain.ResponseEntityProxy.streamClosed(ResponseEntityProxy.java:142) 在org.apache.http.conn.EofSensorInputStream.checkClose(EofSensorInputStream.java:228) 在org.apache.http.conn.EofSensorInputStream.close(EofSensorInputStream.java:172) 在org.apache.http.client.entity.LazyDecompressingInputStream.close(LazyDecompressingInputStream.java:97) 在org.apache.http.util.EntityUtils.consume(EntityUtils.java:90) 在org.apache.http.impl.client.AbstractResponseHandler.handleResponse(AbstractResponseHandler.java:69) 在org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:66) 在org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:52) 在org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:223) 在org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:165) 在org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:140)
答案 0 :(得分:0)
我改变了方法,并使用MultiPartUtility类通过HTTPUrlConnection触发请求,以文本/纯格式传递了两个表单字段,并以应用程序/八位字节流格式和二进制编码传递了文件