简体   繁体   中英

Apache HTTPClient Headers are not correctly set

i'm trying to request a GET via HTTPS trough a Proxy. The Proxy answers with 400:Bad Request. I sniffed the data with wireshark and i have seen, that the headers are not set. Because of security, i replaced some Values with <> Brackets. Can anybody help?

This is a part of my implementation:

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

I have traced the headers in the request with and printed name/value with the output below:

     Header[] headers = request.getAllHeaders();

Header Name:Proxy-Connection

Header Value:close

Header Name:Proxy-Authorization

Header Value:Basic

Header Name:User-Agent

Header Value: MyDevice

Header Name:Accept

Header Value:text/html

At least this is the response: Content length responesCode: 0 400

From Wireshark sniffing i found, that the headers which are defintly inside the request are not set when sending CONNECT.

I attached this picture:

Wireshark Trace

EDIT: Thank you Damian Nikodem, but i found the solution. The first request is always sent without user headers. I changed two things, and the proxy authorization works: request = new HttpGet(urlString); HttpResponse response = httpclient.execute(request);

I am assuming that you are attempting to communicate with a SCADA or production management system. From what I can see you are sending plain HTTP to your target server via the proxy, while trying to connect via HTTPS.

I couldn't see the response in your wireshark dump, but it most likely contains a error message that looks something like this:

<!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>

PS You should edit your image a little bit more because it shows a LOT of information about your employer/customer..

@Thorgas, I don't understand what you sad about "found the solution". Do you mean that you found out how to add extra headers in the CONNECT with HttpClinet? I'm also confusing about this.

But i found out that using HttpsUrlconnection in android doesn't have this kind of problem. URLConnection urlConnection = url.openConnection(proxy);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM