我尝试通过HTTPS通过代理请求GET。代理回答400:错误请求。我用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();
标题名称:代理连接
标题值:关闭
标题名称:代理授权
标题值:基本
标题名称:用户代理
标题值:MyDevice
标题名称:接受
标题值:text / html
至少这是回复: 内容长度responesCode:0 400
从Wireshark嗅探我发现,发送CONNECT时未设置请求中明确的标头。
我附上了这张照片:
修改: 谢谢Damian Nikodem, 但我找到了解决方案。 始终在没有用户标头的情况下发送第一个请求 我改变了两件事,代理授权有效: request = new HttpGet(urlString); HttpResponse response = httpclient.execute(request);
答案 0 :(得分:0)
我假设您正在尝试与SCADA
或生产管理系统进行通信。从我可以看到,您正在尝试通过HTTPS连接时,通过代理向目标服务器发送纯HTTP。
我无法在你的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>
P.S。您应该稍微编辑一下图像,因为它显示了关于您的雇主/客户的 LOT 信息。
答案 1 :(得分:0)
但我发现在android中使用HttpsUrlconnection没有这种问题。 URLConnection urlConnection = url.openConnection(proxy);
HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) urlConnection;
httpsUrlConnection.setRequestMethod("GET");
httpsUrlConnection.setRequestProperty("HEADER1","HEADER1Content");
httpsUrlConnection.connect();