龙空技术网

Java,http代理服务器,基于Netty框架实现的http代理服务器

古怪今人 189

前言:

现在各位老铁们对“java代理http”大致比较着重,大家都需要剖析一些“java代理http”的相关内容。那么小编也在网摘上网罗了一些有关“java代理http””的相关资讯,希望看官们能喜欢,各位老铁们一起来学习一下吧!

http代理服务器

代理服务器,Proxy Server,http代理服务器就是基于http协议的代理服务器。

LittleProxy

LittleProxy是一个用Java编写的高性能HTTP代理服务器,基于Netty事件的网络库之上,它非常稳定,性能良好,易于集成到项目中,支持命令行和嵌入式方式。

地址:

代码案例

pom.xml

<dependencies>    <dependency>        <groupId>org.littleshoot</groupId>        <artifactId>littleproxy</artifactId>        <version>1.1.2</version>    </dependency></dependencies>

使用代码

import org.littleshoot.proxy.HttpProxyServer;import org.littleshoot.proxy.impl.DefaultHttpProxyServer;public class HttpClientServer {    public static void main(String[] args) {        HttpProxyServer server = DefaultHttpProxyServer.bootstrap()                .withPort(11111)                .withFiltersSource(new MyHttpFiltersSourceAdapter())                .start();    }}
import io.netty.channel.ChannelHandlerContext;import io.netty.handler.codec.http.*;import org.littleshoot.proxy.HttpFilters;import org.littleshoot.proxy.HttpFiltersAdapter;import org.littleshoot.proxy.HttpFiltersSourceAdapter;public class MyHttpFiltersSourceAdapter extends HttpFiltersSourceAdapter {    public HttpFilters filterRequest(HttpRequest originalRequest, ChannelHandlerContext ctx) {        String uri = originalRequest.getUri();        HttpMethod method = originalRequest.getMethod();        System.out.println("method=" + method + ",uri=" + uri);        return new HttpFiltersAdapter(originalRequest) {            @Override            public HttpResponse clientToProxyRequest(HttpObject httpObject) {                // TODO: implement your filtering here                System.out.println("clientToProxyRequest=" + httpObject);                return null;            }            @Override            public HttpObject serverToProxyResponse(HttpObject httpObject) {                // TODO: implement your filtering here                System.out.println("serverToProxyResponse=" + httpObject);                return httpObject;            }        };    }    @Override    public int getMaximumResponseBufferSizeInBytes() {        return 10 * 1024 * 1024;    }}

真是个好年代,什么东西别人都替你写好了~~

标签: #java代理http