开发者

How to send raw http requests in Java through proxy

开发者 https://www.devze.com 2023-02-07 22:08 出处:网络
Basically I\'m trying to write a proxy for http requests. I\'ve been able to set up the server fairly easily, and after directing my browser to the proxy instead of the direct connection, it is receiv

Basically I'm trying to write a proxy for http requests. I've been able to set up the server fairly easily, and after directing my browser to the proxy instead of the direct connection, it is receiving the raw http requests sent from the 开发者_如何转开发browser.

My problem now is that I need to be able to now send the raw request from the proxy to retrieve the response from the destination server. I've been trying to dig around through Apache's HttpClient, but as far as I can see, I'm still unsure how it can be used to facilitate in sending raw requests.

Any suggestions?


You don't need any HTTP-aware code in yur proxy at all beyond recognizing the CONNECT command. The rest of it is just copying bytes backwards and forwards. You can use two threads for this (per connection), or you can do it all in one thread with NIO.


It looks like all you need to do is create a connection to the remote server. Depending on how you are rerouting the browser's request, you may need to implement DNS look-up yourself. As EJP said, just copy the bytes from one stream to the other and copy the response back.

0

精彩评论

暂无评论...
验证码 换一张
取 消