Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未正确设置Apache HTTPClient头_Apache_Https_Proxy_Header_Httpclient - Fatal编程技术网

未正确设置Apache HTTPClient头

未正确设置Apache HTTPClient头,apache,https,proxy,header,httpclient,Apache,Https,Proxy,Header,Httpclient,我正试图通过代理请求通过HTTPS获取。代理以400:Bad请求回答。我用wireshark嗅探了一下数据,发现没有设置标题。出于安全考虑,我用括号替换了一些值。有人能帮忙吗 这是我实施的一部分: String urlString = ctx.getUrl(); HttpHost target; CloseableHttpClient httpclient; HttpClientContext localContext; try { CredentialsProvider credsP

我正试图通过代理请求通过HTTPS获取。代理以400:Bad请求回答。我用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);
头名称:代理连接

标题值:关闭

标题名称:代理授权

标题值:基本

标题名称:用户代理

标题值:MyDevice

标题名称:接受

标题值:text/html

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

通过Wireshark嗅探,我发现在发送CONNECT时,没有设置请求内部的头文件

我附上这张照片:

编辑: 谢谢Damian Nikodem, 但我找到了解决办法。 第一个请求总是在没有用户头的情况下发送。 我改变了两件事,代理授权有效: 请求=新的HttpGeturlString;
HttpResponse response=httpclient.executerequest

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

另外,你应该多编辑一点你的图片,因为它显示了很多关于你雇主/客户的信息

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

但我发现在android中使用HttpsUrlconnection并没有这种问题。 URLConnection URLConnection=url.openConnectionproxy

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

我太困了,无法给出正确的答案,但是对于这样的问题,我推荐一个叫做Charles internet proxy的工具,它是调试类似问题的完美工具,即使是跨https。我在使用外部工具方面非常有限,因为我的应用程序运行在嵌入式系统上。
HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) urlConnection;
httpsUrlConnection.setRequestMethod("GET");
httpsUrlConnection.setRequestProperty("HEADER1","HEADER1Content");
httpsUrlConnection.connect();