给调用Http 接口的多种方式加代理 proxy

一、http 接口添加代理

(一)httpClient CloseableHttpClient

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.12</version>
        </dependency>
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.HttpGet;
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;

/**
 * A simple example that uses HttpClient to execute an HTTP request
 * over a secure connection tunneled through an authenticating proxy.
 */
public class ClientProxyAuthentication {
    
    

    public static void main(String[] args) throws Exception {
    
    
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope("proxyip", proxyport),
                new UsernamePasswordCredentials("", ""));
        credsProvider.setCredentials(
                new AuthScope("targetip", targetport),
                new UsernamePasswordCredentials("user", "passwd"));
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCredentialsProvider(credsProvider).build();
        try {
    
    
            HttpHost target = new HttpHost("targetip", targetport, "http");
            HttpHost proxy = new HttpHost("proxyip", proxyport);

            RequestConfig config = RequestConfig.custom()
                    .setProxy(proxy)
                    .build();
            HttpGet httpget = new HttpGet("targeturi");
            httpget.setConfig(config);

            System.out.println("Executing request " + httpget.getRequestLine() + " to " + target + " via " + proxy);

            CloseableHttpResponse response = httpclient.execute(target, httpget);
            try {
    
    
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                System.out.println(EntityUtils.toString(response.getEntity()));
            } finally {
    
    
                response.close();
            }
        } finally {
    
    
            httpclient.close();
        }
    }
}

(二)restassured RequestSpecification

        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>4.0.0</version>
        </dependency>

import io.restassured.config.RestAssuredConfig;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

import java.net.URI;

import static io.restassured.RestAssured.given;
import static io.restassured.config.EncoderConfig.encoderConfig;

public class ClientRestAssuredProxy {
    
    
    public static void main(String[] args) {
    
    
        String baseUrl = "http://targetip:targetport";
        RequestSpecification requestSpecification = given().config(new RestAssuredConfig().encoderConfig(encoderConfig().defaultContentCharset("UTF-8"))).baseUri(baseUrl);
        requestSpecification.proxy("proxyip",proxyport);
        requestSpecification.basePath("targeturi");
        Response response = requestSpecification.get();

        System.out.println("输出"+response.getBody().prettyPrint());  

    }
}

(三) RestTemplate

        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setProxy(new Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress("proxyip",proxyport)));
        RestTemplate restTemplate = new RestTemplate(factory);

二、websocket 接口添加代理

        client.setProxy(new Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress("proxyip",proxyport)));

猜你喜欢

转载自blog.csdn.net/MDJ_D2T/article/details/119930084