最简单的方法就是在自己的服务器上添加一个代理,客户端脚本先把请求发送给代理,代理转发请求,把响应发回给客户端脚本。
所以,我们先在自己的服务器上添加一个代理
/** * */ package com.sech.web.servlet; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils; /** * * <p> * Copyright: Copyright (c) 2009 * </p> * * @author sech * * @version 1.0 */ public class RssRead extends HttpServlet { /** * */ private static final long serialVersionUID = 4431127246599359389L; private static int READ_BUFFER_SIZE = 1024; /** * */ public RssRead() { // TODO Auto-generated constructor stub } public void init() throws ServletException { } // Process the HTTP Get request public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String urlString = request.getParameter("url"); writeResponse(response, urlString); } private void writeResponse(HttpServletResponse response, String urlString) throws ServletException { try { URL url = new URL(urlString); URLConnection urlConnection = url.openConnection(); response.setContentType(urlConnection.getContentType()); InputStream ins = urlConnection.getInputStream(); OutputStream outs = response.getOutputStream(); byte[] buffer = new byte[READ_BUFFER_SIZE]; int bytesRead = 0; while ((bytesRead = ins.read(buffer, 0, READ_BUFFER_SIZE)) != -1) { outs.write(buffer, 0, bytesRead); } outs.flush(); outs.close(); ins.close(); } catch (Exception e) { try { response.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e .getMessage()); } catch (IOException ioe) { throw new ServletException(ioe); } } } // Process the HTTP Post request public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } // Clean up resources public void destroy() { } }