【问题标题】:Apache HTTPClient Headers are not correctly setApache HTTPClient 标头未正确设置
【发布时间】:2015-02-11 12:06:45
【问题描述】:

我正在尝试通过代理通过 HTTPS 请求 GET。代理以 400:Bad Request 回答。我用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();

标题名称:代理连接

标题值:关闭

标题名称:代理授权

标题值:基本

标题名称:用户代理

标头值:我的设备

标题名称:接受

标头值:text/html

至少这是响应: 内容长度响应码:0 400

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

我附上了这张照片:

编辑: 谢谢达米安 Nikodem, 但我找到了解决方案。 第一个请求总是在没有用户标头的情况下发送。 我改变了两件事,代理授权工作: 请求 = 新的 HttpGet(urlString); HttpResponse 响应 = httpclient.execute(request);

【问题讨论】:

  • 我太困了,无法提供正确的答案,但对于这样的问题,我会推荐一个名为 Charles 互联网代理的工具(它确实是调试此类问题的完美工具,即使跨 https 也是如此。)跨度>
  • 我在使用外部工具方面非常有限,因为我的应用程序在嵌入式系统上运行。

标签: apache https proxy header httpclient


【解决方案1】:

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

我在您的 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>

附:你应该多编辑一下你的图片,因为它显示了关于你的雇主/客户的大量信息。

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-18
      • 2014-07-27
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      • 2023-03-31
      • 2018-08-28
      相关资源
      最近更新 更多