簡體   English   中英

向 python 中的 HTTPS 代理發出請求

[英]Make request with HTTPS proxy in python

我的 VPN 提供商 (NordVP) 剛剛關閉了其代理服務器上的所有 HTTP 端口。 我可以通過cURL確認 HTTPS 端口工作正常,例如以下工作:

curl -x https://proxy_server:89 --proxy-user username:password -L url

雖然這剛剛停止工作:

curl -x http://proxy_server:80 --proxy-user username:password -L url

它曾經在兩天前工作。 在 python 我正在嘗試使用request模塊發出 HTTP 請求:

import requests
proxies = {
  "https":"https://user:password@proxy_server:port"
}

response = requests.get("https://api64.ipify.org?format=json", proxies=proxies)
print(response.text)

但我收到以下 3 個錯誤:

ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

During handling of the above exception, another exception occurred:

...

MaxRetryError: HTTPSConnectionPool(host='api64.ipify.org', port=443): Max retries exceeded with url: /?format=json (Caused by ProxyError('Cannot connect to proxy.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)))

During handling of the above exception, another exception occurred:

...

ProxyError: HTTPSConnectionPool(host='api64.ipify.org', port=443): Max retries exceeded with url: /?format=json (Caused by ProxyError('Cannot connect to proxy.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)))

我看到關於這個主題有一個非常古老的問題,但它似乎不再相關。

這是一種解決方法:

Authenticator proxyAuthenticator = new Authenticator() {
    @Override public Request authenticate(Route route, Response response) throws IOException {
        String credential = Credentials.basic(username, password);
        return response.request().newBuilder().header("Proxy-Authorization", credential).build();
    }
};

OkHttpClient client = new OkHttpClient.Builder()
        .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)))
        .proxyAuthenticator(proxyAuthenticator);
        .socketFactory(new DelegatingSocketFactory(SSLSocketFactory.getDefault()))
        .build();

在 Android 上,將此添加到您的 gradle:

implementation 'org.conscrypt:conscrypt-android:2.5.0'

這對您的應用程序onCreate()

Security.insertProviderAt(Conscrypt.newProvider(), 1);

https://github.com/square/okhttp/issues/6561

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM