如何在java中的HttpPost中设置Proxy
请考虑我有一个带有字段的自定义代理类,
private final String serverName;
private final int port;
private final String username;
private final String password;
我想在下面的现有代码中使用此代理,但我不确定如何添加它。以下代码不支持代理。
public CustomClassForResponse createRequest(CreateCustomRequest request) {
HttpPost httpRequest = new HttpPost();
try {
URIBuilder uriBuilder = new URIBuilder();
uriBuilder.setScheme(URL_SCHEME).setHost(HOST_URL).setPath("/api");
httpRequest.setURI(uriBuilder.build());
httpRequest.setHeader("Authorization", accessToken());
httpRequest.setHeader("Content-Type", "application/json");
httpRequest.setHeader("Accept", "application/json");
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(1000 * 60 * 5)
.setSocketTimeout(1000 * 60 * 5)
.build();
httpRequest.setConfig(requestConfig);
httpRequest = (HttpPost) addJsonBody(request, httpRequest);
return sendRequest(httpRequest, CustomClassForResponse.class);
} catch (Exception e) {
throw new CustomException(e);
}
}
private HttpEntityEnclosingRequest addJsonBody(JsonRequest request, HttpEntityEnclosingRequest httpRequest) throws JsonProcessingException {
EntityBuilder builder = EntityBuilder.create();
builder.setContentType(ContentType.APPLICATION_JSON);
builder.setText(ObjectMapperProvider.getInstance().writeValueAsString(request));
HttpEntity httpEntity = builder.build();
httpRequest.setEntity(httpEntity);
return httpRequest;
}
private <T extends JsonResponse> T sendRequest(HttpRequestBase httpRequest, Class<T> responseClass) throws IOException {
CloseableHttpResponse response = createHttpClient().execute(httpRequest, createHttpClientContext());
HttpEntity entity = response.getEntity();
if (isResponseSuccess(response)) {
T jsonResponse = ObjectMapperProvider.getInstance().readValue(entity.getContent(), responseClass);
return jsonResponse;
} else {
throw new CustomException();
}
}
因此,会调用 createRequest
方法,然后调用 addJsonBody
和 sendRequest
方法。
以下是版本详细信息:
("org.apache.httpcomponents:httpcore:4.4.6")
("org.apache.httpcomponents:httpclient:4.5.13")
("org.apache.httpcomponents:httpmime:4.5.3")
("commons-logging:commons-logging:1.2")
Please consider that I have a Custom Proxy Class with fields,
private final String serverName;
private final int port;
private final String username;
private final String password;
I want to use this Proxy in the below existing code, but I am not sure how to add that. The below code does not support proxy.
public CustomClassForResponse createRequest(CreateCustomRequest request) {
HttpPost httpRequest = new HttpPost();
try {
URIBuilder uriBuilder = new URIBuilder();
uriBuilder.setScheme(URL_SCHEME).setHost(HOST_URL).setPath("/api");
httpRequest.setURI(uriBuilder.build());
httpRequest.setHeader("Authorization", accessToken());
httpRequest.setHeader("Content-Type", "application/json");
httpRequest.setHeader("Accept", "application/json");
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(1000 * 60 * 5)
.setSocketTimeout(1000 * 60 * 5)
.build();
httpRequest.setConfig(requestConfig);
httpRequest = (HttpPost) addJsonBody(request, httpRequest);
return sendRequest(httpRequest, CustomClassForResponse.class);
} catch (Exception e) {
throw new CustomException(e);
}
}
private HttpEntityEnclosingRequest addJsonBody(JsonRequest request, HttpEntityEnclosingRequest httpRequest) throws JsonProcessingException {
EntityBuilder builder = EntityBuilder.create();
builder.setContentType(ContentType.APPLICATION_JSON);
builder.setText(ObjectMapperProvider.getInstance().writeValueAsString(request));
HttpEntity httpEntity = builder.build();
httpRequest.setEntity(httpEntity);
return httpRequest;
}
private <T extends JsonResponse> T sendRequest(HttpRequestBase httpRequest, Class<T> responseClass) throws IOException {
CloseableHttpResponse response = createHttpClient().execute(httpRequest, createHttpClientContext());
HttpEntity entity = response.getEntity();
if (isResponseSuccess(response)) {
T jsonResponse = ObjectMapperProvider.getInstance().readValue(entity.getContent(), responseClass);
return jsonResponse;
} else {
throw new CustomException();
}
}
so the createRequest
method is called and then it calls the addJsonBody
and then the sendRequest
method.
Here are the version details:
("org.apache.httpcomponents:httpcore:4.4.6")
("org.apache.httpcomponents:httpclient:4.5.13")
("org.apache.httpcomponents:httpmime:4.5.3")
("commons-logging:commons-logging:1.2")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建客户端时设置代理,或者在Request上设置代理。
由于您已经引用了 createHttpClient() 方法:
或者在创建请求时设置它:
由于您暗示它是经过身份验证的代理,那么我想一些凭据是正确的。
Set the proxy when you create the client, or set the proxy on the Request.
Since you already refer to a createHttpClient() method :
or setting it when you create the request :
Since you're implying that it's an authenticated proxy then I guess some credentials are in order.