java servlet proxy

import java.io.IOException; import java.io.OutputStreamWriter;
import java.net.URLDecoder; import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang.NotImplementedException;



public class SimpleProxy {

    public final void proxyAjaxCall( String url,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws IOException {

        // URL needs to be url decoded

        url = URLDecoder.decode(url, "utf-8");
        OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream());
        HttpClient client = new HttpClient();
        try {

            HttpMethod method = null;

            // Split this according to the type of request
            if (request.getMethod().equals("GET")) {

                method = new GetMethod(url);

            } else if (request.getMethod().equals("POST")) {

                method = new PostMethod(url);

                // Set any eventual parameters that came with our original // request (POST params, for instance)
                Enumeration paramNames = request.getParameterNames();
                while (paramNames.hasMoreElements()) {
                    String paramName = paramNames.nextElement().toString();
                    ((PostMethod) method).setParameter(paramName, request.getParameter(paramName));
                }
            }else {

                throw new NotImplementedException( "This proxy only supports GET and POST methods.");
            }

            // Execute the method
            client.executeMethod(method);

            // Set the content type, as it comes from the server
            Header[] headers = method.getResponseHeaders();
            for (Header header : headers) {
                if ("Content-Type".equalsIgnoreCase(header.getName())) {
                    response.setContentType(header.getValue());
                }
            }
            // Write the body, flush and close
            writer.write(method.getResponseBodyAsString());
            writer.flush();
            writer.close();
        } catch (HttpException e) {
            //log.error("Oops, something went wrong in the HTTP proxy", null, e);
            writer.write(e.toString());
            throw e;

        } catch (IOException e) {

            e.printStackTrace(); writer.write(e.toString()); throw e;
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值