首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在python中使用HTTPS代理发出请求

在python中使用HTTPS代理发出请求
EN

Stack Overflow用户
提问于 2021-02-17 17:38:10
回答 1查看 73关注 0票数 0

我的虚拟专用网提供商(NordVP)刚刚关闭了其代理服务器上的所有HTTP端口。我可以向cURL确认HTTPS端口工作正常,例如,以下工作:

代码语言:javascript
运行
复制
curl -x https://proxy_server:89 --proxy-user username:password -L url

虽然这已经停止了:

代码语言:javascript
运行
复制
curl -x http://proxy_server:80 --proxy-user username:password -L url

两天前就起作用了。在python中,我试图使用request模块发出HTTP请求:

代码语言:javascript
运行
复制
import requests
proxies = {
  "https":"https://user:password@proxy_server:port"
}

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

但我得到以下三个错误:

代码语言:javascript
运行
复制
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)))

我看到关于这个话题有一个很老的问题,但它似乎不再有意义了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-19 20:09:35

以下是一个解决办法:

代码语言:javascript
运行
复制
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中:

代码语言:javascript
运行
复制
implementation 'org.conscrypt:conscrypt-android:2.5.0'

这个给你的应用程序onCreate()

代码语言:javascript
运行
复制
Security.insertProviderAt(Conscrypt.newProvider(), 1);

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66247197

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档