java http请求设置代理Proxy的两种常见方法

 更新时间:2023年11月25日 14:35:58   作者:几层山下  
代理是一种常见的设计模式,其目的就是为其他对象提供一个代理以控制对某个对象的访问,这篇文章主要给大家介绍了关于java http请求设置代理Proxy的两种常见方法,需要的朋友可以参考下

HttpURLConnection、HttpClient设置代理Proxy

有如下一种需求,原本A要给C发送请求,但是因为网络原因,需要借助B才能实现,所以由原本的A->C变成了A->B->C。

这种情况,更多的见于内网请求由统一的网关做代理然后转发出去,比如你本地的机器想要对外上网,都是通过运营商给的出口IP也就是公网地址实现的。这种做法就是代理了。

研究了一下针对 HttpURLConnection和HttpClient这两种常见的http请求的代理:

一、HttpURLConnection设置请求代理

贴出一个utils类

具体代码如下:

public class ProxyUtils {

    public static final String CONTENT_TYPE = "application/x-www-form-urlencoded";

    public static String getResultByHttpConnectionProxy(String url, String content, String proxyHost, int proxyPort) {

        String result = "";
        OutputStream outputStream = null;
        InputStream inputStream = null;
        try {
            //设置proxy
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
            URL proxyUrl = new URL(url);
            //判断是哪种类型的请求
            if (url.startsWith("https")) {
                HttpsURLConnection httpsURLConnection = (HttpsURLConnection) proxyUrl.openConnection(proxy);
                httpsURLConnection.setRequestProperty("Content-Type", CONTENT_TYPE);
                //允许写入
                httpsURLConnection.setDoInput(true);
                //允许写出
                httpsURLConnection.setDoOutput(true);
                //请求方法的类型 POST/GET
                httpsURLConnection.setRequestMethod("POST");
                //是否使用缓存
                httpsURLConnection.setUseCaches(false);
                //读取超时
                httpsURLConnection.setReadTimeout(15000);
                //连接超时
                httpsURLConnection.setConnectTimeout(15000);
                //设置SSL
                httpsURLConnection.setSSLSocketFactory(getSsf());
                //设置主机验证程序
                httpsURLConnection.setHostnameVerifier((s, sslSession) -> true);

                outputStream = httpsURLConnection.getOutputStream();
                outputStream.write(content.getBytes(StandardCharsets.UTF_8));
                outputStream.flush();
                inputStream = httpsURLConnection.getInputStream();
            } else {
                HttpURLConnection httpURLConnection = (HttpURLConnection) proxyUrl.openConnection(proxy);
                httpURLConnection.setRequestProperty("Content-Type", CONTENT_TYPE);
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setUseCaches(false);
                httpURLConnection.setConnectTimeout(15000);
                httpURLConnection.setReadTimeout(15000);

                outputStream = httpURLConnection.getOutputStream();
                outputStream.write(content.getBytes("UTF-8"));
                outputStream.flush();
                inputStream = httpURLConnection.getInputStream();
            }

            byte[] bytes = read(inputStream, 1024);
            result = (new String(bytes, "UTF-8")).trim();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static byte[] read(InputStream inputStream, int bufferSize) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[bufferSize];

        for (int num = inputStream.read(buffer); num != -1; num = inputStream.read(buffer)) {
            baos.write(buffer, 0, num);
        }

        baos.flush();
        return baos.toByteArray();
    }

    private static SSLSocketFactory getSsf() {
        SSLContext ctx = null;
        try {
            ctx = SSLContext.getInstance("TLS");
            ctx.init(new KeyManager[0],
                    new TrustManager[]{new ProxyUtils.DefaultTrustManager()},
                    new SecureRandom());
        } catch (Exception e) {
            e.printStackTrace();
        }
        assert ctx != null;
        return ctx.getSocketFactory();
    }

    private static final class DefaultTrustManager implements X509TrustManager {
        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    }
}

上面的代码就是对httpsURLConnection设置了Proxy代理,也就是请求先会发到proxyHost:proxyPort,然后由其代理发到url。

二、HttpClient设置请求代理

贴出一个utils类

具体代码如下:

public class HttpclientUtils {

    private static final String CONTENT_TYPE = "application/x-www-form-urlencoded";

    public static String getResultByProxy(String url, String request, String proxyHost, int proxyPort) throws Exception {
        String response = null;
        HttpPost httpPost = null;
        try {
            HttpClient httpClient = getHttpClient(url);
            //设置请求配置类  重点就是在这里添加setProxy 设置代理
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(15000).setConnectTimeout(15000)
                    .setConnectionRequestTimeout(15000).setProxy(new HttpHost(proxyHost, proxyPort)).build();
            httpPost = new HttpPost(url);
            
            httpPost.setConfig(requestConfig);
            httpPost.addHeader("Content-Type", CONTENT_TYPE);
            httpPost.setEntity(new StringEntity(request, "utf-8"));

            response = getHttpClientResponse(httpPost, httpClient);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != httpPost) {
                httpPost.releaseConnection();
            }
        }
        return response;
    }

    private static String getHttpClientResponse(HttpPost httpPost, HttpClient httpClient) throws Exception {
        String result = null;
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity entity = httpResponse.getEntity();

        if (null != entity) {
            try (InputStream inputStream = entity.getContent()) {
                byte[] bytes = read(inputStream, 1024);
                result = new String(bytes, StandardCharsets.UTF_8);
            }
        }

        return result;
    }

    private static HttpClient getHttpClient(String url) throws Exception {
        HttpClient httpClient;
        String lowerURL = url.toLowerCase();
        if (lowerURL.startsWith("https")) {
            httpClient = createSSLClientDefault();
        } else {
            httpClient = HttpClients.createDefault();
        }
        return httpClient;
    }

    private static CloseableHttpClient createSSLClientDefault() throws Exception {
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (chain, authType) -> true).build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, (s, sslSession) -> true);
        return HttpClients.custom().setSSLSocketFactory(sslsf).build();
    }

    public static byte[] read(InputStream inputStream, int bufferSize) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[bufferSize];

        for (int num = inputStream.read(buffer); num != -1; num = inputStream.read(buffer)) {
            baos.write(buffer, 0, num);
        }

        baos.flush();
        return baos.toByteArray();
    }
}

以上就是针对http、https的代理汇总,其实想想,就是通过 Proxy 对象,添加对应的代理地址和端口,实现了一层转发,可以想到nginx、gateway这种思想。

总结

到此这篇关于java http请求设置代理Proxy的两种常见方法的文章就介绍到这了,更多相关java http请求设置代理Proxy内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • spring中BeanUtils.copyProperties的使用(深拷贝,浅拷贝)

    spring中BeanUtils.copyProperties的使用(深拷贝,浅拷贝)

    本文主要介绍了spring中BeanUtils.copyProperties的使用(深拷贝,浅拷贝),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • Java Spring boot日期和时间统一设置三种方法

    Java Spring boot日期和时间统一设置三种方法

    时间和日期的统一设置在项目中经常是会遇到的,下面这篇文章主要给大家介绍了关于Java Spring boot日期和时间统一设置的三种方法,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-08-08
  • 聊聊@RequestBody和Json之间的关系

    聊聊@RequestBody和Json之间的关系

    这篇文章主要介绍了@RequestBody和Json之间的关系,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • 浅析Java中的动态代理

    浅析Java中的动态代理

    动态代理指代理类和目标类的关系在程序运行的时候确定的,客户通过代理类来调用目标对象的方法。本文将通过案例详细讲解一下Java动态代理的原理及实现,需要的可以参考一下
    2022-09-09
  • java实现装饰器模式(Decorator Pattern)

    java实现装饰器模式(Decorator Pattern)

    这篇文章主要为大家详细介绍了java实现装饰器模式Decorator Pattern,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-10-10
  • java后台调用接口及处理跨域问题的解决

    java后台调用接口及处理跨域问题的解决

    这篇文章主要介绍了java后台调用接口,处理跨域的问题及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • SpringBoot接收前端参数的几种常用方式

    SpringBoot接收前端参数的几种常用方式

    在Spring Boot开发中接收参数是非常常见且重要的一部分,依赖于请求的不同场景,Spring Boot提供了多种方式来处理和接收参数,这篇文章主要给大家介绍了关于SpringBoot接收前端参数的几种常用方式,需要的朋友可以参考下
    2024-07-07
  • Java设计模式之创建者模式简介

    Java设计模式之创建者模式简介

    这篇文章主要介绍了Java设计模式之创建者模式,需要的朋友可以参考下
    2014-07-07
  • 使用spring-boot-admin对spring-boot服务进行监控的实现方法

    使用spring-boot-admin对spring-boot服务进行监控的实现方法

    这篇文章主要介绍了使用spring-boot-admin对spring-boot服务进行监控的实现方法,需要的朋友可以参考下
    2018-02-02
  • SpringBoot用JdbcTemplates访问Mysql实例代码

    SpringBoot用JdbcTemplates访问Mysql实例代码

    本篇文章主要介绍了SpringBoot用JdbcTemplates访问Mysql实例代码,非常具有实用价值,需要的朋友可以参考下
    2017-05-05

最新评论