В python есть простой рабочий код через requests:
proxies = {'http': 'http://login:password@ip:port',
'https': 'https://login:password@ip:port'}
response = requests.post(target_url, data=json_data, headers=some_headers, proxies=proxies)
В java тоже самое:
Authenticator auth = new Authenticator() {
public Request authenticate(Route route, Response response) throws IOException {
String credential = Credentials.basic("login", "password");
return response.request().newBuilder()
.header("Proxy-Authorization", credential)
.build();
}
};
OkHttpClient client = new OkHttpClient.Builder()
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port)))
.proxyAuthenticator(auth)
.build();
JSONObject data = new JSONObject();
try {
postdata.put("param", "value");
} catch(JSONException e){
e.printStackTrace();
}
RequestBody body = RequestBody.create(MediaType.parse("application/json"), data.toString());
Request request = new Request.Builder()
.url(target_url)
.post(body)
.addHeader("Content-Type", "application/json")
.build();
try {
Response response = client.newCall(request).execute();
} catch (Exception e) {
e.printStackTrace();
}
код python успешно аутентифицируется на прокси получает данные с target_url, код java получает 407 "Proxy Authentication Required"
что я делаю не так?
пс: забыл добавить: запрос - HTTPS
Addon:
В Wireshark вот что:
OkHttp:
запрос:
Hypertext Transfer Protocol
CONNECT somehost.ru:443 HTTP/1.1\r\n
Host: somehost.ru:443\r\n
Proxy-Connection: Keep-Alive\r\n
User-Agent: okhttp/3.8.0\r\n
ответ:
Hypertext Transfer Protocol
HTTP/1.0 407 Proxy Authentication Required\r\n
Proxy-Authenticate: Basic realm="proxy"\r\n
Proxy-Connection: close\r\n
Content-type: text/html; charset=utf-8\r\n
File Data: 255 bytes
Line-based text data: text/html
<html><head><title>407 Proxy Authentication Required</title></head>\r\n
<body><h2>407 Proxy Authentication Required</h2><h3>Access to requested resource disallowed by administrator or you need valid username/password to use this resource</h3></body></html>
второй запрос:
Hypertext Transfer Protocol
CONNECT somehost.ru:443 HTTP/1.1\r\n
Host: somehost.ru:443\r\n
Proxy-Connection: Keep-Alive\r\n
User-Agent: okhttp/3.8.0\r\n
Proxy-Authorization: Basic cnAzMDcwOTQzOlA0WWtuMmM4bFM=\r\n
но к этому моменту уже java.net.protocolexception и до HandShake дело не доходит.
А вот Python requests:
запрос:
Hypertext Transfer Protocol
CONNECT somehost:443 HTTP/1.0\r\n
Proxy-Authorization: Basic cnAzMDcwOTQzOlA0WWtuMmM4bFM=\r\n
ответ:
Hypertext Transfer Protocol
HTTP/1.0 200 Connection established\r\n
Почему OkHttp не шлет сразу Proxy-Authorization?
Апостиль в Лос-Анджелесе без лишних нервов и бумажной волокиты
Основные этапы разработки сайта для стоматологической клиники
Продвижение своими сайтами как стратегия роста и независимости