HTTP GET/CONNECT代理区别

HTTP GET/CONNECT代理区别举例:

HTTP Proxy Server :  127.0.0.1 8080
Access Web Site: www.example.com/index.php


1) 无代理:
GET /index.php HTTP/1.1
HOST:
 www.example.com

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,**;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: xxxxxx
Cookie: xxxxxx
Cache-Control: max-age=0


2) HTTP GET 代理(HTTP1.0/HTTP1.1都支持) - socket首先与127.0.0.1:8080建立连接
GET http:/www.example.com/index.php HTTP/1.1
HOST:
 www.example.com

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,**;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Proxy-Connection: keep-alive
Referer: xxxxxx
Cookie: xxxxxx
Cache-Control: max-age=0


3) HTTP CONNECT代理(只有HTTP1.1支持) - socket首先与127.0.0.1:8080建立连接
CONNECT http:/www.example.com/index.php HTTP/1.1
HOST:
 www.example.com

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Proxy-Connection: keep-alive
Referer: xxxxxx
Cookie: xxxxxx
Cache-Control: max-age=0


注:HTTP CONNECT是在HTTP1.1协议上才新增的命令,用于支撑https加密。
所以,许多支持http connect的http proxy,比如squid,默认都限制只允许访问外部的443端口,其它端口不提供代理服务。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的 Java Netty 实现的 HTTP/HTTPS 代理服务代码示例: ```java import io.netty.bootstrap.Bootstrap; import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.http.DefaultFullHttpResponse; import io.netty.handler.codec.http.DefaultHttpRequest; import io.netty.handler.codec.http.HttpClientCodec; import io.netty.handler.codec.http.HttpContent; import io.netty.handler.codec.http.HttpHeaders; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpObject; import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.HttpServerCodec; import io.netty.handler.codec.http.HttpVersion; import io.netty.handler.ssl.SslContext; import io.netty.handler.ssl.SslContextBuilder; import io.netty.handler.ssl.SslHandler; import io.netty.handler.ssl.util.InsecureTrustManagerFactory; import io.netty.util.CharsetUtil; import javax.net.ssl.SSLEngine; import java.net.InetSocketAddress; import java.util.HashMap; import java.util.Map; public class HttpsProxyServer { private final int port; public HttpsProxyServer(int port) { this.port = port; } public void start() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .localAddress(new InetSocketAddress(port)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new HttpServerCodec()); ch.pipeline().addLast(new HttpsProxyServerHandler()); } }) .childOption(ChannelOption.AUTO_READ, false) .bind().sync().channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } public static void main(String[] args) throws Exception { if (args.length != 1) { System.err.println("Usage: " + HttpsProxyServer.class.getSimpleName() + " <port>"); return; } int port = Integer.parseInt(args[0]); new HttpsProxyServer(port).start(); } private static final class HttpsProxyServerHandler extends SimpleChannelInboundHandler<HttpObject> { private Channel clientChannel; private Channel serverChannel; private SSLEngine sslEngine; private boolean isHttps; private final Map<String, String> headers = new HashMap<>(); @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { clientChannel = ctx.channel(); } @Override public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception { if (msg instanceof DefaultHttpRequest) { DefaultHttpRequest request = (DefaultHttpRequest) msg; String requestUri = request.getUri(); isHttps = requestUri.startsWith("https://"); String[] hostParts = HttpHeaders.getHost(request).split(":"); String host = hostParts[0]; int port = isHttps ? 443 : (hostParts.length > 1 ? Integer.parseInt(hostParts[1]) : 80); Bootstrap b = new Bootstrap(); b.group(clientChannel.eventLoop()) .channel(NioSocketChannel.class) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { if (isHttps) { sslEngine = SslContextBuilder.forClient() .trustManager(InsecureTrustManagerFactory.INSTANCE) .build().newEngine(ch.alloc(), host, port); ch.pipeline().addLast(new SslHandler(sslEngine)); } ch.pipeline().addLast(new HttpClientCodec()); ch.pipeline().addLast(new HttpsProxyServerHandler()); } }) .connect(host, port) .addListener((ChannelFuture future) -> { if (future.isSuccess()) { serverChannel = future.channel(); } else { clientChannel.close(); } }); headers.clear(); for (Map.Entry<String, String> entry : request.headers()) { headers.put(entry.getKey(), entry.getValue()); } headers.remove(HttpHeaders.Names.HOST); b.channel(NioSocketChannel.class) .connect(host, port) .addListener((ChannelFuture future) -> { if (future.isSuccess()) { serverChannel = future.channel(); serverChannel.writeAndFlush(request.retain()); clientChannel.read(); } else { clientChannel.close(); } }); } else if (msg instanceof HttpResponse) { HttpResponse response = (HttpResponse) msg; response.headers().remove(HttpHeaders.Names.TRANSFER_ENCODING); response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); clientChannel.writeAndFlush(response.retain()); } else if (msg instanceof HttpContent) { HttpContent content = (HttpContent) msg; clientChannel.writeAndFlush(content.retain()); if (content instanceof LastHttpContent) { if (isHttps) { sslEngine.closeOutbound(); } clientChannel.flush(); clientChannel.close(); serverChannel.close(); } } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { if (clientChannel.isActive()) { clientChannel.writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_GATEWAY, Unpooled.copiedBuffer("Failure: " + cause.getMessage(), CharsetUtil.UTF_8))) .addListener(ChannelFutureListener.CLOSE); } } } } ``` 这是一个简单的 HTTP/HTTPS 代理服务,可以接收来自客户端的请求,并将其转发到目标服务器。如果请求是 HTTPS 请求,它还会与目标服务器建立安全连接。 在这个示例中,我们使用了 Netty 的 HTTP/HTTPS 编解码器和 SSL 处理程序来处理请求和响应,并将它们转发到目标服务器。在建立与目标服务器的连接时,我们还使用了 Netty 的 Bootstrap 类来创建客户端通道。 当客户端发送请求时,我们从请求中提取目标主机和端口,并使用 Bootstrap 类创建一个新的客户端通道,然后将请求发送到目标服务器。在接收到来自目标服务器的响应时,我们将响应转发给客户端。 如果请求是 HTTPS 请求,我们还需要使用 SSL 处理程序来建立与目标服务器的安全连接。我们使用 Netty 的 SslContextBuilder 类创建一个 SSL 引擎,并将其添加到客户端通道的管道中。在建立与目标服务器的连接时,我们还需要使用 SslHandler 将 SSL 引擎添加到客户端通道的管道中,以便进行 SSL 握手。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值