欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java HTTP 代理demo

程序员文章站 2022-03-09 08:35:00
...

HTTP 代理服务器使用 little proxy实现https://github.com/adamfisk/LittleProxy

 

maven依赖:

<dependency>
            <groupId>org.littleshoot</groupId>
            <artifactId>littleproxy</artifactId>
            <version>1.0.0-beta7</version>
</dependency>

 

 

客户端代码:

package com.bigtian;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.SocketAddress;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by bigtian on 15/5/30.
 */
public class ProxyTestCase {

    public static void main(String[] args) throws Exception {
        URL url = new URL("http://www.baidu.com");

        SocketAddress address = new InetSocketAddress("localhost", 8080);

        Proxy proxy = new Proxy(Proxy.Type.HTTP, address);

        URLConnection connection = url.openConnection(proxy);
        connection.connect();
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        String result = "";
        while ((line = br.readLine()) != null) {
            result += line;
        }

        System.out.println(result);

    }

}

 

代理服务器代码:

package com.bigtian;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import org.littleshoot.proxy.HttpFilters;
import org.littleshoot.proxy.HttpFiltersAdapter;
import org.littleshoot.proxy.HttpFiltersSourceAdapter;
import org.littleshoot.proxy.HttpProxyServer;
import org.littleshoot.proxy.impl.DefaultHttpProxyServer;

/**
 * Created by bigtian on 15/5/30.
 */
public class ProxyTest {

    public static void main(String[] args) {
        HttpProxyServer server =
                DefaultHttpProxyServer.bootstrap()
                        .withPort(8080)
                        .withFiltersSource(new HttpFiltersSourceAdapter() {
                            public HttpFilters filterRequest(HttpRequest originalRequest, ChannelHandlerContext ctx) {
                                return new HttpFiltersAdapter(originalRequest) {
                                    @Override
                                    public HttpResponse requestPre(HttpObject httpObject) {
                                        return null;
                                    }

                                    @Override
                                    public HttpResponse requestPost(HttpObject httpObject) {
                                        return null;
                                    }

                                    @Override
                                    public HttpObject responsePre(HttpObject httpObject) {
                                        return httpObject;
                                    }

                                    @Override
                                    public HttpObject responsePost(HttpObject httpObject) {
                                        return httpObject;
                                    }
                                };
                            }
                        })
                        .start();

    }
}