[英]Apache HTTPClient Headers are not correctly set
i'm trying to request a GET via HTTPS trough a Proxy. 我试图通过HTTPS通过代理请求GET。 The Proxy answers with 400:Bad Request.
代理回答400:错误请求。 I sniffed the data with wireshark and i have seen, that the headers are not set.
我用wireshark嗅探了数据,并且看到没有设置标头。 Because of security, i replaced some Values with <> Brackets.
由于安全性,我用<> 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 标头名称:Proxy-Connection
Header Value:close 标头值:关闭
Header Name:Proxy-Authorization 标题名称:代理授权
Header Value:Basic 标头值:基本
Header Name:User-Agent 标头名称:User-Agent
Header Value: MyDevice 标头值:MyDevice
Header Name:Accept 标题名称:接受
Header Value:text/html 标头值:text / html
At least this is the response: Content length responesCode: 0 400 至少这是响应:内容长度responesCode:0 400
From Wireshark sniffing i found, that the headers which are defintly inside the request are not set when sending CONNECT. 从Wireshark嗅探我发现,发送CONNECT时没有设置请求中明确的标头。
I attached this picture: 我附上这张照片:
EDIT: Thank you Damian Nikodem, but i found the solution. 编辑:谢谢Damian Nikodem,但我找到了解决方案。 The first request is always sent without user headers.
第一个请求始终在没有用户标头的情况下发送。 I changed two things, and the proxy authorization works: request = new HttpGet(urlString);
我做了两件事,代理授权起作用了:request = new HttpGet(urlString); HttpResponse response = httpclient.execute(request);
HttpResponse response = httpclient.execute(request);
I am assuming that you are attempting to communicate with a SCADA
or production management system. 我假设您正在尝试与
SCADA
或生产管理系统进行通信。 From what I can see you are sending plain HTTP to your target server via the proxy, while trying to connect via HTTPS. 从我可以看到,您正在尝试通过HTTPS连接时,通过代理向目标服务器发送纯HTTP。
I couldn't see the response in your wireshark dump, but it most likely contains a error message that looks something like this: 我在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>
PS You should edit your image a little bit more because it shows a LOT of information about your employer/customer.. PS你应该稍微编辑你的图像,因为它显示了很多关于你的雇主/客户的信息。
@Thorgas, I don't understand what you sad about "found the solution". @Thorgas,我不明白你对“找到解决方案”感到难过。 Do you mean that you found out how to add extra headers in the CONNECT with HttpClinet?
您的意思是您发现如何在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. 但我发现在android中使用HttpsUrlconnection没有这种问题。 URLConnection urlConnection = url.openConnection(proxy);
URLConnection urlConnection = url.openConnection(proxy);
HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) urlConnection;
httpsUrlConnection.setRequestMethod("GET");
httpsUrlConnection.setRequestProperty("HEADER1","HEADER1Content");
httpsUrlConnection.connect();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.