代理

正向代理

正向代理是客户端的代理,帮助客户端访问其无法访问的服务器资源。比如我通过一个代理服务器可以访问很多国内无法访问的网站

反向代理

反向代理则是服务器的代理,帮助服务器做负载均衡,安全防护等。所有的用户都访问同一个网址,而我可以用多台服务器来处理这一个网址接收的请求

使用代理发送HTTP请求

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5 -->
<dependency>
    <groupId>org.apache.httpcomponents.client5</groupId>
    <artifactId>httpclient5</artifactId>
    <version>5.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5-fluent -->
<dependency>
    <groupId>org.apache.httpcomponents.client5</groupId>
    <artifactId>httpclient5-fluent</artifactId>
    <version>5.2.1</version>
</dependency>
import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.core5.http.HttpHost;
import java.net.URI;
import java.util.Base64;

public static void main(String[] args) throws Exception {
    String target = "http://www.baidu.com";
    URI proxyURI = new URI("http://代理用户名:代理密码@代理服务器:端口号");
    String basicAuth = new String(Base64.getEncoder().encode(proxyURI.getUserInfo().getBytes()));
    String response = Request.get(target)
            .addHeader("Proxy-Authorization", "Basic " + basicAuth)
            .viaProxy(HttpHost.create(proxyURI))
            .execute()
            .returnContent()
            .asString();
    System.out.println(response);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值