`

Set proxy with account/password for CloseableHttpClient

    博客分类:
  • Java
 
阅读更多
package com.hsbc.wealth.srbp;

import java.util.Map;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class LoopAuth extends Thread {

    private static final Logger logger = LoggerFactory.getLogger(LoopAuth.class);

    Map<String, String> ctx = null;

    int sleepTime = 120000;

    @Override
    public void run() {
        logger.info("run()");

        while (ctx == null) {
            try {
                logger.info("sleeping");
                Thread.sleep(sleepTime/10);
            } catch (InterruptedException e) {
                logger.error("sleep failed", e);
            }
        }

        while (true) {
            callLogon();
            try {
                Thread.sleep(sleepTime);
            } catch (InterruptedException e) {
                logger.error("callLogon failed", e);
            }
        }
    }

    private void callLogon() {

        String url = "https://devcluster.cert-proxy/v1/logon";

        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope("intpxy1.hk.lin", 8080),
            new UsernamePasswordCredentials(ctx.get("account"), ctx.get("password")));

        CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();

        try {
            HttpHost target = new HttpHost(url);
            HttpHost proxy = new HttpHost("intpxy1.hk.lin", 8080);

            RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
            HttpPost httpRequest = new HttpPost(url);
            httpRequest.setConfig(config);

            httpRequest.addHeader("X-Locale", ctx.get("x-locale"));

            CloseableHttpResponse response = httpclient.execute(target, httpRequest);

            logger.info(EntityUtils.toString(response.getEntity()));

        } catch (Exception e) {
            logger.error("Cannot get auth.", e);
        }
    }

    @Override
    public void start() {
        super.start();
    }

}

 

分享到:
评论

相关推荐

    boostdesc_bgm.i,vgg_generated_48.i等.zip

    CMake配置生成opencv4的时候,... - http_proxy/https_proxy - and/or HTTP_PROXY/HTTPS_PROXY 可以下载本资源结合我的博文(https://editor.csdn.net/md/?articleId=121682921)进行配置就可以顺利成功解决问题了。

    apache Proxy_Error

    apache Proxy Error apache Proxy Error apache Proxy Error apache Proxy Error

    charles-proxy 抓包工具 v4.2 Win + Mac + Linux全平台破解文件

    下载正版原版程序: https://www.charlesproxy.com/assets/release/4.2/charles-proxy-4.2-win64.msi https://www.charlesproxy.com/assets/release/4.2/charles-proxy-4.2-win32.msi ...

    android 系统 DNS 劫持软件

    adb shell "su -c 'chmod 755 /system/xbin/dnsproxy2 /system/etc/init.d/20dnsproxy2'" adb shell "su -c 'mount -o ro,remount /system'" adb shell "su -c 'rm -f /data/local/tmp/dnsproxy2 /data/local/tmp/20...

    proxyServer squid / varnish / apache traffic server / ATS

    NULL 博文链接:https://lindows.iteye.com/blog/247703

    Set browser Proxy

    auto Set browser Proxy

    proxy_Linux/Unix编程_

    linux上的端口代理程序proxy -l 80 -h 93.184.216.119 -p 80 -i &quot;tee input.log&quot; -o &quot;tee output.log&quot;

    Proxy Server(代理服务器)

    summary: provides proxy tcp service for a host on an isolated network. programmer: Carl Harris (ceharris@vt.edu) date: 22 Feb 94 description: This code implements a daemon process ...

    qzxdh代理设置小工具SetProxy

    qzxdh代理设置小工具SetProxy

    set_proxy:整个代理设置

    WEBAPP ProxyMaster现在可通过 LINUX代理 ... wget https://github.com/thealphadollar/set_proxy/archive/master.zip 解压缩文件。 unzip master.zip 将目录更改为膨胀的存档文件夹。 cd set_proxy-

    charles-proxy-4.2-win64.msi

    官网下载的64位windows安装版 注册码: Registered Name: https://zhile.io License Key: 48891cf209c6d32bf4

    npm install报错解决方案和思路.pdf

    在开发过程中,经常会使用npm安装依赖包来加速开发。但是在执行npm install命令时,有时会...例如,使用npm config set proxy http://proxy-server:port和npm config set https-proxy http://proxy-server:port来设置

    node-http-proxy.zip

    node-http-proxy 是一个全功能的 Nginx 的 HTTP 代理扩展,支持 WebSocket 。

    VC Proxy代理程序:Visual C 专家门诊300问随书代码.rar

    VC Proxy演示程序代码:是Visual C 专家门诊300问中的一个随书源码,简单的代理服务器程序,运行时提供代理服务,先启动服务才可以。来看一些参数定义:  SOCKET user_proxy; //socket : 本地机器到PROXY 服务机  ...

    foxyproxy_standard-7.5.1-an+fx.xpi

    foxyproxy_standard-7.5.1-an+fx.xpi

    firefox代理切换插件proxy_switchyomega

    firefox插件proxy_switchyomega-2.5.0官方签名网络快速切换代理插件,安全可靠,来源于官方插件商城下载保存,请放心使用。 安装方法: 附加组件 &gt; 设置(插件搜索处左边齿轮) &gt; 从文件安装附加组件,选择xpi包即可。

    详解Nginx proxy_pass的一个/斜杠引发的血案

    一个nginx的server模块下需要proxy到两个server,所以就通过location的不同路径来区分转发到不同的服务器上。 一开始是这么写的 location / { proxy_pass http://server1/; } location /index { proxy_pass ...

    fees-manager

    费用经理发展平台安装nodejs : : //nodejs.org 凉亭: npm install bower -g 混帐: : 如果您使用代理,请配置 npm: npm config set proxy http://login:pass@hostname:portnpm config set https-proxy ...

Global site tag (gtag.js) - Google Analytics