Apache HTTPClient标头未正确设置

时间:2015-02-11 12:06:45

标签: apache https proxy header httpclient

我尝试通过HTTPS通过代理请求GET。代理回答400:错误请求。我用wireshark嗅探数据,我看到,标题没有设置。出于安全考虑,我用<>替换了一些值。括号。有人可以帮忙吗?

这是我实施的一部分:

String urlString = ctx.getUrl();
HttpHost target;
CloseableHttpClient httpclient;
HttpClientContext localContext;
try
{
     CredentialsProvider credsProvider = new BasicCredentialsProvider();
     int proxyport = Integer.parseInt(ctx.getProxyPort());

     credsProvider.setCredentials(
         new AuthScope(<MyProxyUrl>, <MyProxyPort>),
         new UsernamePasswordCredentials(<MyProxyUser>, <MyProxyPassword>));

     httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();

     target = new HttpHost(urlString, 443, "https");
     HttpHost proxy = new HttpHost(<MyProxyUser>, <MyProxyPassword>);

     RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
     request = new HttpGet("/");
     request.setURI(new URI(urlString));
     //this method sets different header
     HttpProperty.setHeaders(request, ctx);
     request.setConfig(config);

     HttpResponse response = httpclient.execute(target, request);

我已跟踪请求中的标题,并使用以下输出打印名称/值:

     Header[] headers = request.getAllHeaders();

标题名称:代理连接

标题值:关闭

标题名称:代理授权

标题值:基本

标题名称:用户代理

标题值:MyDevice

标题名称:接受

标题值:text / html

至少这是回复: 内容长度responesCode:0 400

从Wireshark嗅探我发现,发送CONNECT时未设置请求中明确的标头。

我附上了这张照片:

Wireshark Trace

修改: 谢谢Damian Nikodem, 但我找到了解决方案。 始终在没有用户标头的情况下发送第一个请求 我改变了两件事,代理授权有效: request = new HttpGet(urlString); HttpResponse response = httpclient.execute(request);

2 个答案:

答案 0 :(得分:0)

我假设您正在尝试与SCADA或生产管理系统进行通信。从我可以看到,您正在尝试通过HTTPS连接时,通过代理向目标服务器发送纯HTTP。

我无法在你的wireshark转储中看到响应,但它很可能包含一条看起来像这样的错误消息:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
 <head>
  <title>400 Bad Request</title>
 </head>
 <body>
    <h1>Bad Request</h1>
    <p>Your browser sent a request that this server could not understand.<br />
       Reason: You're speaking plain HTTP to an SSL-enabled server port.<br />
       Instead use the HTTPS scheme to access this URL, please.<br />
       <blockquote>Hint: <a href="https://????/"><b>https://???/</b></a></blockquote>
    </p>
 </body>
</html>

P.S。您应该稍微编辑一下图像,因为它显示了关于您的雇主/客户的 LOT 信息。

答案 1 :(得分:0)

@Thorgas,我不明白你对“找到解决方案”感到难过。你的意思是你发现如何在CONNECT中添加额外的标题与HttpClinet?我也对此感到困惑。

但我发现在android中使用HttpsUrlconnection没有这种问题。    URLConnection urlConnection = url.openConnection(proxy);

HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) urlConnection;
httpsUrlConnection.setRequestMethod("GET");
httpsUrlConnection.setRequestProperty("HEADER1","HEADER1Content");
httpsUrlConnection.connect();