如何使用Java中的Google App Engine创建代理服务器?

时间:2012-11-02 11:37:36

标签: java google-app-engine proxy

我正试图在谷歌引擎应用程序上用java编写一个简单的代理服务器

servlet是:

import java.io.*;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.List;
import java.util.logging.*;

import javax.servlet.*;
import javax.servlet.http.*;

import java.net.URL;
import java.net.HttpURLConnection;

public class ProxyServlet extends HttpServlet {

    private ServletContext servletContext;
    private Logger log;

    public void init(ServletConfig servletConfig) throws ServletException {
        servletContext = servletConfig.getServletContext();
        log = Logger.getLogger(ProxyServlet.class.getName());
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response){
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response){
        System.out.println( "doPost"  );


        BufferedInputStream webToProxyBuf = null;
        BufferedOutputStream proxyToClientBuf = null;
        HttpURLConnection con;

        try{
            int statusCode;
            int oneByte;
            String methodName;
            String headerText;

            String urlString = request.getRequestURL().toString();
            String queryString = request.getQueryString();

            urlString += queryString==null?"":"?"+queryString;
            URL url = new URL(urlString);

            log.info("Fetching >"+url.toString());

            con =(HttpURLConnection) url.openConnection();

            methodName = request.getMethod();
            con.setRequestMethod(methodName);
            con.setDoOutput(true);
            con.setDoInput(true);
//            HttpURLConnection.setFollowRedirects(false);
            con.setUseCaches(true);

            for( Enumeration e = request.getHeaderNames() ; e.hasMoreElements();){
                String headerName = e.nextElement().toString();
                con.setRequestProperty(headerName,    request.getHeader(headerName));
            }

            con.connect();

            if(methodName.equals("POST")){
                BufferedInputStream clientToProxyBuf = new BufferedInputStream(request.getInputStream());
                BufferedOutputStream proxyToWebBuf     = new BufferedOutputStream(con.getOutputStream());

                while ((oneByte = clientToProxyBuf.read()) != -1) 
                    proxyToWebBuf.write(oneByte);

                proxyToWebBuf.flush();
                proxyToWebBuf.close();
                clientToProxyBuf.close();
            }

            statusCode = con.getResponseCode();
            response.setStatus(statusCode);

            for( Iterator i = con.getHeaderFields().entrySet().iterator() ; i.hasNext() ;){
                Map.Entry mapEntry = (Map.Entry)i.next();
                if(mapEntry.getKey()!=null)
                    response.setHeader(mapEntry.getKey().toString(), ((List)mapEntry.getValue()).get(0).toString());
            }

            webToProxyBuf = new BufferedInputStream(con.getInputStream());
            proxyToClientBuf = new BufferedOutputStream(response.getOutputStream());

            StringBuffer sb = new StringBuffer(); 

            while ((oneByte = webToProxyBuf.read()) != -1) {
                proxyToClientBuf.write(oneByte);
                sb.append(oneByte);
            }

            System.out.println( sb );

            proxyToClientBuf.flush();
            proxyToClientBuf.close();

            webToProxyBuf.close();
            con.disconnect();

        }catch(Exception e){
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        finally{
        }
    }
}

部署后,我使用代理http = myproxyappname.appspot.com和port = 80设置firefox代理配置 但Firefox只能在谷歌网页上浏览

2 个答案:

答案 0 :(得分:4)

这是一个有效的代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



public class ProxyServlet extends HttpServlet {



    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse rsp)
            throws ServletException, IOException {
        try {

            String fwdurlstring=req.getParameter("fwdurl");
            if(fwdurlstring==null)
            {
                fwdurlstring="http://www.mcbjam.com";
            }


            URL fwdURL = new URL(fwdurlstring);
            HttpURLConnection fwdConnection = 
                (HttpURLConnection) fwdURL.openConnection();

            for (String key : fwdConnection.getHeaderFields().keySet()) {
                rsp.setHeader(key, fwdConnection.getHeaderField(key));
            }

            copy(fwdConnection.getInputStream(), rsp.getOutputStream());

        } catch (Exception e) {
            e.printStackTrace(rsp.getWriter());

        }
    }


    private void copy(InputStream in, OutputStream out) throws IOException {
        int len = 0;
        byte[] buf = new byte[1024];

        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }

        in.close();
    }

}

在此处查看此操作: http://paint.mcbjam.com/proxy?fwdurl=https://stackoverflow.com/questions/13194501/how-create-your-proxy-server-using-google-app-engine-in-java/16658914#16658914

答案 1 :(得分:1)

据我所知,您正在寻找托管在Google App Engine上的Web代理服务器。

这个已经在这个帖子中讨论过了。 Proxy for Google App Engine

你只需要移植python项目就可以在java中实现相同的功能。

尝试在GAE上托管的此演示代理服务器 http://mirrorrr.appspot.com/