2015-02-11 113 views
0

我想通过HTTPS通过代理请求GET。代理服务器回复400:错误请求。我用wireshark嗅探了数据,并且我看到,标题没有设置。由于安全性,我用<>括号替换了一些值。任何人都可以帮忙吗?Apache HTTPClient头没有正确设置

这是我的实现的一部分:

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(); 

标题名:的Proxy-连接

标题值:关闭

标题名称:Proxy-Au thorization

标头值:基本

标题名称:用户代理

标头值:mydevice在

标题名称:接受

标头值:text/html的

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

从我发现的Wireshark嗅探中发现,当发送CONNECT时,请求内部的头部没有被设置。

我重视这样的画面:

Wireshark Trace

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

+0

IM太困提供一个合适的回答,但对于这样的问题,我会建议一个叫查尔斯·互联网代理(其litrally像这样的调试问题的完美工具,甚至是跨HTTPS)工具 – 2015-02-11 12:11:43

+0

我在使用外部工具时非常有限,因为我的应用程序在嵌入式系统上运行。 – Thorgas 2015-02-11 12:37:24

回答

0

我假设您正在尝试与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> 

附:你应该编辑你的形象多一点,因为它显示LOT有关您的雇主/客户的信息。

0

@Thorgas,我不明白你对“找到解决方案”感到悲伤。你的意思是你发现如何在与HttpClinet的连接中添加额外的头文件?我也对此感到困惑。

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

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