Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何方便的设置http代理 #23

Closed
lly835 opened this issue Sep 21, 2020 · 8 comments
Closed

如何方便的设置http代理 #23

lly835 opened this issue Sep 21, 2020 · 8 comments

Comments

@lly835
Copy link

lly835 commented Sep 21, 2020

说一下场景。我有100个账号要抢购,不知道商家什么时候放库存。用一个账号不停的轮询。发现有库存了,剩下99账号使用Http 代理抢购。每个账号使用的代理地址都不一样。
目前知道要在OkHttpClient里设置代理

@OkHttpClientBuilder
    static OkHttpClient.Builder okhttpClientBuilder() {
        return new OkHttpClient.Builder()
                .proxy();
    }

有没有方法在调用RetrofitClient之前,传入okHttpClient

@chentianming11
Copy link
Collaborator

目前的实现是一个接口对应使用一个OkhttpClient,你说的情况目前不支持。
请问下,你这个场景通过proxySelector能解决吗?

@lly835
Copy link
Author

lly835 commented Sep 21, 2020 via email

@chentianming11
Copy link
Collaborator

chentianming11 commented Sep 22, 2020 via email

@lly835
Copy link
Author

lly835 commented Sep 22, 2020

我看了proxySelector, 要在创建okHttpClient时传入

OkHttpClient client = new OkHttpClient();
//构造方法
public OkHttpClient() {
    this(new Builder());
}
public Builder() {
  dispatcher = new Dispatcher();
  protocols = DEFAULT_PROTOCOLS;
  connectionSpecs = DEFAULT_CONNECTION_SPECS;
  proxySelector = ProxySelector.getDefault();
  cookieJar = CookieJar.NO_COOKIES;
  socketFactory = SocketFactory.getDefault();
  hostnameVerifier = OkHostnameVerifier.INSTANCE;
  certificatePinner = CertificatePinner.DEFAULT;
  proxyAuthenticator = Authenticator.NONE;
  authenticator = Authenticator.NONE;
  connectionPool = new ConnectionPool();
  dns = Dns.SYSTEM;
  followSslRedirects = true;
  followRedirects = true;
  retryOnConnectionFailure = true;
  connectTimeout = 10_000;
  readTimeout = 10_000;
  writeTimeout = 10_000;
}

【目前的实现是一个接口对应使用一个OkhttpClient】
如果能做到一个接口对应多个okHttpClient,那么目的就可以达到了

@chentianming11
Copy link
Collaborator

chentianming11 commented Sep 23, 2020 via email

@ruanbaojun1105
Copy link

把rxjava整合一下就更完美了

@chentianming11
Copy link
Collaborator

chentianming11 commented Sep 24, 2020 via email

@ruanbaojun1105
Copy link

@chentianming11 哈哈,我只是个写安卓的,虽然会写java 后台,可是后台那么多大神,我这三脚猫功夫还是不露了~~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants