Project to use, here to simplify and general, highlighting the basic ideas, specific can be based on this class to modify.
Based on Java containers and Servlets.
Package Com.xxx.first;import Java.io.bufferedreader;import Java.io.ioexception;import java.io.InputStream;import Java.io.inputstreamreader;import Java.io.outputstream;import Java.io.printwriter;import Java.net.httpurlconnection;import Java.net.url;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.apache.commons.compress.utils.ioutils;public class SimpleProxy { /** * Submit to target server using get. * * @param request * @param response * @param targeturl * @throws IOException */private void Get ( HttpServletRequest request, HttpServletResponse response, String TargetUrl) throws ioexception {URL url = new URL (targe Turl); BufferedReader in = new BufferedReader (New InputStreamReader (Url.openstream ())); String Line; PrintWriter out = Response.getwriter (), while (line = In.readline ())! = null) {out.println (line);} Out.flush (); In.close (); }/** * submitted to the target server using post. * * @param request * @param response * @param targeturl * @throws IOException */private void post (HttpServletRequest request, httpservletrespons E response, String TargetUrl) throws ioexception {URL url = new URL (targeturl); HttpURLConnection conn = (httpurlconnection) url.openconnection ();//The Send POST request must be set to the following two lines conn.setdooutput (true); Conn.setdoinput (True); Conn.setrequestmethod ("POST");//Can copy the client's head information as the requested head parameter//Conn.setrequestproperty (" Charsert "," UTF-8 "), Conn.setrequestproperty (" Content-type "," Application/json ");//Direct The body of the client to the target server OutputStream Send = Conn.getoutputstream (); InputStream BODY = Request.getinputstream (); Ioutils.copy (body, send); Send.flush (); Send.close (); Body.close ();//Gets the return value BufferedReader in = new BufferedReader (New InputStreamReader (Conn.getinputstream () )); PrintWriter out = Response.getwriter (); String Line;while (line = In.readline ())! = null) {out.println (line);} Out.flush (); }}
A Java class that can be used as a proxy server or forwarded