我有以下代码:
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
FileSystemResource fileSystemResource = new FileSystemResource(fileNameWithPath);
map.add("file", fileSystemResource);
map.add("type", fileType);
map.add("org_id", systemSettingsService.getSystemSettings().getOrganizationId());
map.add("stone_id", stoneId);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class)
如果我提供的所有参数都正确-enpoint会像邮递员应用程序一样返回200个http stsatus代码。
但是如果我提供了错误的url-我看到异常:
org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://external_server/api/v1/push_file1": Broken pipe (Write failed); nested exception is java.net.SocketException: Broken pipe (Write failed)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:666)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531)
at com.pack.GenericDataServiceImpl.pushAttachment(GenericDataServiceImpl.java:311)
at com.pack.FailedAttachmentPushReSender.retryFailedPushes(FailedAttachmentPushReSender.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
at java.util.concurrent.Executors$RunnableAdapter.call$$$capture(Executors.java:511)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java)
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
at java.util.concurrent.FutureTask.run(FutureTask.java)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748) Caused by: java.net.SocketException: Broken pipe (Write failed)
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)
at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
at sun.security.ssl.OutputRecord.writeBuffer(OutputRecord.java:431)
at sun.security.ssl.OutputRecord.write(OutputRecord.java:417)
at sun.security.ssl.SSLSocketImpl.writeRecordInternal(SSLSocketImpl.java:886)
at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:857)
at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:123)
at org.apache.http.impl.io.SessionOutputBufferImpl.streamWrite(SessionOutputBufferImpl.java:124)
at org.apache.http.impl.io.SessionOutputBufferImpl.write(SessionOutputBufferImpl.java:160)
at org.apache.http.impl.io.ContentLengthOutputStream.write(ContentLengthOutputStream.java:113)
at org.apache.http.entity.ByteArrayEntity.writeTo(ByteArrayEntity.java:114)
at org.apache.http.impl.DefaultBHttpClientConnection.sendRequestEntity(DefaultBHttpClientConnection.java:156)
at org.apache.http.impl.conn.CPoolProxy.sendRequestEntity(CPoolProxy.java:160)
at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:238)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:272)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:89)
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:652) ... 20 more
但是当我以同样的方式在postman中修改url时——我看到404错误:
我想有像邮递员一样的回应和像邮递员一样的回应。
如果我提供File
而不是FileSystemResources
,我会得到正确的HttpStatusException
,我可以在其中提取我想要的数据:
catch (HttpStatusCodeException e) {
String response = e.getResponseBodyAsString();
logger.error("Error during attachment push for file: {}, responseText: {}", fileNameWithPath, response, e);
}
如何编写将提取响应
和httpStatusCode
的通用代码?
重新模板初始化:
CloseableHttpClient httpClient
= HttpClients.custom()
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.build();
HttpComponentsClientHttpRequestFactory requestFactory
= new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
restTemplate = new RestTemplate(requestFactory);
我已经阅读了RESTTemplateClientHTTPResponse。getBody()抛出I/O错误,但看起来已经使用了建议的工厂
我试过这个:
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(buildAttachmentPushUrl());
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("file", fileSystemResource.getFile(), ContentType.APPLICATION_OCTET_STREAM, fileSystemResource.getFilename());
builder.addTextBody("type", fileType);
builder.addTextBody("org_id", systemSettingsService.getSystemSettings().getOrganizationId());
builder.addTextBody("stone_id", stoneId);
HttpEntity multipart = builder.build();
httpPost.setEntity(multipart);
CloseableHttpResponse response = client.execute(httpPost);
logger.info("response code {}", response.getStatusLine().getStatusCode());
client.close();
} catch (IOException e) {
e.printStackTrace();
}
但结果相同-<代码>套接字写入错误
我通常使用这种模式,也许它对你有用
try{
ResponseEntity<String> responseEntity = restTemplate.exchange(url, httpMethod, httpEntity, String.class);
HttpStatus.Series responseSeries = responseEntity.getStatusCode().series();
if(!HttpStatus.Series.SERVER_ERROR.equals(responseSeries)){
return responseEntity;
}
}
catch(HttpClientErrorException e){
//handle exception here
//Response received from server: {} ", e.getResponseBodyAsString());
}
catch (RestClientException e) {
//handle exception here
}
catch (Exception e) {
//handle exception here
}
如何使用restTemplate获得真正的http代码和响应正文?
简而言之,你不能,因为根本没有。
组织。html" target="_blank">springframework。网状物客户ResourceAccessException:POST请求时的I/O错误“https://external_server.ventures/api/v1/push_file1“:软件导致的连接中止:套接字写入错误;嵌套异常为java.net.SocketException:软件导致的连接中止:套接字写入错误
阅读异常消息。原因(您省略了stacktrace)是java。网SocketException出现底层套接字写入错误。
您从未收到响应,因为您可能从未完成发送请求。请参阅链接问题,了解原因。
ResourceAccessException
是您想要捕获的内容。它涵盖了与您可能收到的响应相关的所有错误,并提供了对状态、标头和正文的访问。您被抛出的是ResourceAccessException
,它涵盖了所有可能发生且没有任何响应的I/O错误。
我想要ot实现WebFlux示例客户端,它可以用http params发出请求,并获得响应体和http响应代码。我试过这个: 您能在之后给我一些建议吗?我如何获得http状态代码和所有可用的主体。
我试图创建一个regex来捕获HTTP状态代码和curl请求的主体。下面的regex模式可以在多个在线站点上工作,但在我的Mac命令行上的shell if-语句中不匹配。是我的regex关闭了还是有其他事情发生? 我也对其他方法持开放态度(我的目标是运行Centos5的机器)。
问题内容: 我正在尝试让Apache HttpClient触发HTTP请求,然后显示HTTP响应代码(200、404、500等)以及HTTP响应正文(文本字符串)。重要的是要注意我正在使用,因为那里有大多数HttpClient示例,并且API从版本3到版本4有了很大的变化。 不幸的是,我只能使HttpClient返回状态代码 或 响应正文(但不能同时返回两者)。 这是我所拥有的: 所以我问: 使用
我试图在我的应用程序中获取一些数据,并通过使用带有截击的HTTP POST请求向用户显示。我的问题是,我不知道如何获得服务器主体对请求的响应。在下面的代码中,响应总是“200”,这是HTTP状态代码。 如何提取身体反应并作为字符串进行管理?
问题内容: 我已经使用Selenium2 / WebDriver编写了测试,并且想要测试HTTP请求是否返回HTTP 403 Forbidden。 是否可以使用Selenium WebDriver获取HTTP响应状态代码? 问题答案: 一言以蔽之。无法使用Selenium WebDriver API。此问题已在项目的问题跟踪器中进行了讨论,该功能不会添加到API中。
问题内容: 我已经使用Selenium2 / WebDriver编写了测试,并且想要测试HTTP请求是否返回HTTP 403 Forbidden。 是否可以使用Selenium WebDriver获取HTTP响应状态代码? 问题答案: 一言以蔽之。无法使用Selenium WebDriver API。此问题已在项目的问题跟踪器中进行了讨论,并且该功能不会添加到API中。