跨域问题

原理:通过本地的js,访问java程序,然后把返回的值传回js

 得到跨域地址

   function convertURL(url){

     if(url.substring(0,7)=="http://"){

         url = url.replace("?","&");

         url ="Poxy.do?url="url;

  }

            return url;

}

 

同域服务端

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.net.*;
import java.util.Enumeration;

public class Proxy extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
            throws javax.servlet.ServletException, java.io.IOException  {
        response.setContentType("text/html;charset=GB2312");
        String url = request.getParameter("url");
        StringBuffer param = new StringBuffer();
        Enumeration enu = request.getParameterNames();
        int total = 0;
        while(enu.hasMoreElements()){
            String name = (String)enu.nextElement();
            if(!name.equals("url")){
                if(total == 0){
                    param.append(name).append("=").append(URLEncoder.encode(request.getParameter(name),"UTF-8"));
                } else{
                    param.append("&").append(name).append("=").append(URLEncoder.encode(request.getParameter(name),"UTF-8"));
                }
                total++;

            }
        }
        PrintWriter out = response.getWriter();
        if(url != null){
            URL connect = new URL(url.toString());
            URLConnection connection = connect.openConnection();
            connection.setDoOutput(true);
            OutputStreamWriter paramout = new OutputStreamWriter(connection.getOutputStream());
            paramout.write(param.toString());
            paramout.flush();
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"GB2312"));
            String line;
            while((line = reader.readLine()) != null){
               out.println(line);
            }
            paramout.close();
            reader.close();
        }

    }
    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
        response.setContentType("text/html;charset=GB2312");
        StringBuffer url = new StringBuffer();
        url.append(request.getParameter("url"));
        Enumeration enu = request.getParameterNames();
        int total = 0;
        while(enu.hasMoreElements()){
            String name = (String)enu.nextElement();
            if(!name.equals("url")){
                if(total == 0){
                    url.append("?").append(name).append("=").append(URLEncoder.encode(request.getParameter(name),"UTF-8"));
                } else{
                    url.append("&").append(name).append("=").append(URLEncoder.encode(request.getParameter(name),"UTF-8"));
                }
                total++;

            }
        }
        PrintWriter out = response.getWriter();
        if(url != null){
             URL connect = new URL(url.toString());
             BufferedReader reader = new BufferedReader(new InputStreamReader(connect.openStream(),"GB2312"));
             String line;
             while((line = reader.readLine()) != null){
                out.println(line);
             }
             reader.close();
        }

      //  http://www.sohu.com/index.html?name=123&id=000
       //  Proxy?url=http://www.sohu.com/index.html&name=123&id=000

       // url=http://www.sohu.com/index.html&name=123&id=000

       // http://www.sohu.com/index.html?id=000&name=123


}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值