使用jsp代理解决跨域问题引发的乱码问题

今天在做一个界面优化的时候顺便解决其中的QQ昵称查询问题,使用了“http://users.qzone.qq.com/fcg...号码”,出现两个问题,第一个跨域问题,后来用jsp代理解决了,但是出现了中文乱码问题,只要QQ昵称中包含中文一律以“???”显示,现贴出来原网页中js代码和使用的jsp代理代码,希望高手鞥帮看下,谢谢
图片描述
原网页代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html;charset=gb2312" />

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />

<title>QQ昵称查询</title>

<link href="/vs/css/style.css" rel="stylesheet" type="text/css" />

<link href="css/game.css" rel="stylesheet" type="text/css" />

<script language="javascript" src="js/common.js"></script>

<script language="javascript" src="js/game_ui.js"></script>

<script type="text/javascript" >

function gb2utf8(data){

var glbEncode = [];

gb2utf8_data = data;

execScript("gb2utf8_data = MidB(gb2utf8_data, 1)", "VBScript");

var t=escape(gb2utf8_data).replace(/%u/g,"").replace(/(.{2})(.{2})/g,"%$2%$1").replace(/%([A-Z].)%(.{2})/g,"@$1$2");

t=t.split("@");

var i=0,j=t.length,k;

while(++i<j) {

k=t[i].substring(0,4);

if(!glbEncode[k]) {

gb2utf8_char = eval("0x"+k);

execScript("gb2utf8_char = Chr(gb2utf8_char)", "VBScript");

glbEncode[k]=escape(gb2utf8_char).substring(1,6);

}

t[i]=glbEncode[k]+t[i].substring(4);

}

gb2utf8_data = gb2utf8_char = null;

return unescape(t.join("%"));

}

function cxName()

{

var xmlhttp;

var puname = document.getElementById("playerusername").value;

if (window.XMLHttpRequest)

{

xmlhttp = new XMLHttpRequest();

}

else

{

var MSXML = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];

for(var n = 0; n < MSXML.length; n ++)

{

try

{

xmlhttp = new ActiveXObject(MSXML[n]);

break;

}

catch(e)

{

}

}

}

xmlhttp.onreadystatechange=function()

{

if (xmlhttp.readyState==4 && xmlhttp.status==200)

{

var myString = xmlhttp.responseBody;//responseText responseBody

//alert(myString);

myString = gb2utf8(myString);

var str= new Array();

str = myString.split(",");

var name = str[6];

str = name.split("\"");

name = str[1];

document.getElementById("qqnicheng").value = name;

}

}

//portraitCallBack({"89022090":["http://qlogo3.store.qq.com/qzone/89022090/89022090/100",412,-1,0,0,0,"威讯",0]})

xmlhttp.open("GET","proxy.jsp?url=http://users.qzone.qq.com/fcg-bin/cgi_get_portrait.fcg?uins="+puname,true);

xmlhttp.send();

}

</script>

<script type="text/javascript">

var t1;

function checkNC()

{

var qq1;

var qq2;

if (orderlist.length==0)

{

return false;

}

for (i=0 ;i<orderlist.length;i++)

{

var tmp =document.getElementById(orderlist[i]);

if (orderlist[i]=="qqnicheng")//

{

qq1 = document.getElementById("playerusername").value;

qq2 = document.getElementById("replayerusername").value;

if ((qq1 == qq2 )&&qq1!="undefined"&&qq1!="")

{

//document.getElementById("qqnicheng").value = document.getElementById("playerusername").value;

//window.clearInterval(t1);

cxName();

//window.clearInterval(t1);

}

}

}

}

t1 = window.setInterval(checkNC,5000);

</script>

jsp代理文件(proxy.jsp):

<%@ page import="java.net.*,java.util.*,java.lang.*,java.io.*"%><%@ page contentType="text/xml;charset=gb2312"%>\

<%

String url = null;

StringBuffer params = new StringBuffer();

Enumeration enu = request.getParameterNames();

//String str=request.getQueryString();

//System.out.println(str);

while (enu.hasMoreElements()) {

String paramName=(String)enu.nextElement();

if(paramName.equals("url")){

url=request.getParameter(paramName);

}else{

//有的url参数顺序有规定,按此读出的是逆序,因此要插到paramName的前面

params.insert(0, URLEncoder.encode(request.getParameter(paramName), "gb2312"));

params.insert(0, "=");

params.insert(0, paramName);

if(enu.hasMoreElements()){

params.insert(0, "&");

}

}

}

url = url + "?" + params.toString();

//out.println(url);

System.out.println("url:"+url);

if(url != null){

// 使用GET方式向目的服务器发送请求

URL connect = new URL(url.toString());

URLConnection connection = connect.openConnection();

connection.connect();

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

String line;

while((line = reader.readLine()) != null){

out.println(line);

}

reader.close();

}

%>

实在不知道原因,希望哪位前辈能帮解答下,谢谢

回答:

如果是JAVA程序要做跨越的问题,你只要引入CORS就好了。根本不需那么复杂。

回答:

@召唤大神
如果uins后面跟的是非数字的话,返回的是utf-8格式
如果是正确的数字,返回的是GBK格式
你可以试试手动返回一个字符串到页面 看是否乱码,不管干什么, 乱码问题无非 页面文件编码,web容器编码,网络传输编码,或者手动设置编码

以上是 使用jsp代理解决跨域问题引发的乱码问题 的全部内容, 来源链接: utcz.com/p/176461.html

回到顶部