AJAX跨域訪問通訊代理,java中的實現

來源:互聯網
上載者:User

AJAX跨域訪問通訊代理在網上也說的很多,大多都是用asp.net或php寫的
俺沒用過DWR不知道有沒有實現跨域通訊的執行個體,所以自己在網上查資料寫了個這個,也算解了當前的燃眉之急
裡面的編碼是為JAVA工程和網站資料交換設定的,具體的編碼應看情況設定

應用串連:

http://192.168.1.8:8888/servlet/com.stone.common.AJAXProxy?x=a&y=b&url=http://www.xxx.com/pagesild/testpage.asp

 

package com.stone.common;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class AJAXProxy extends HttpServlet...{
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException ...{
        doGet(req, res);
    }
    
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException ...{
        req.setCharacterEncoding("UTF-8");
        res.setCharacterEncoding("gb2312");
        res.setContentType("text/html");
        PrintWriter out=res.getWriter();
        String mac;
        String url=null;
        StringBuffer params=new StringBuffer();
        Enumeration enu = req.getParameterNames();
        int total = 0;
        
        while (enu.hasMoreElements()) ...{
            
            // 取得參數名稱列表
            String paramName=(String)enu.nextElement();
            // 處理本請求參數以及發送給第三方伺服器的參數
            if(paramName.equals("url"))...{
                // 第三方伺服器位址
                url=req.getParameter(paramName);
            }else...{
                if(total==0)...{
                    params.append(paramName).append("=").append(URLEncoder.encode(req.getParameter(paramName), "UTF-8"));
                } else ...{
                    params.append("&").append(paramName).append("=").append(URLEncoder.encode(req.getParameter(paramName), "UTF-8"));
                }
                ++total;
            }
        }
        
        if(url != null)...{
            // 使用POST方式向目的伺服器發送請求
            URL connect = new URL(url.toString());
            URLConnection connection = connect.openConnection();
            connection.setDoOutput(true);
            
            OutputStreamWriter paramout = new OutputStreamWriter(connection.getOutputStream());
            paramout.write(params.toString());
            paramout.flush();

            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "gb2312"));
            String line;
            StringBuffer dataSend=new StringBuffer();
            while((line = reader.readLine()) != null)...{
                out.println(line);
                dataSend.append(line);
            }
            paramout.close();
            reader.close();
        }
    }
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.