简体   繁体   中英

How to send raw http requests in Java through proxy

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM