URL t=new URL("http://sso.gxnews.com.cn/login.php");
HttpURLConnection huc=(HttpURLConnection)t.openConnection();
huc.setDoInput(true);
huc.setDoOutput(true);
huc.setRequestMethod("POST");
huc.setUseCaches(false);
huc.setInstanceFollowRedirects(true);
huc.addRequestProperty("Accept","text/html, application/xhtml+xml, */*");
huc.addRequestProperty("Referer","http://sso.gxnews.com.cn/login.php");
huc.addRequestProperty("Accept-Language","zh-CN");
huc.addRequestProperty("User-Agent","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
huc.addRequestProperty("Content-Type","application/x-www-form-urlencoded");
huc.addRequestProperty("Accept-Encoding","gzip, deflate");
huc.addRequestProperty("Host","sso.gxnews.com.cn");
huc.addRequestProperty("Connection","Keep-Alive");
huc.addRequestProperty("Cache-Control","no-cache");
huc.connect();
DataOutputStream out =new DataOutputStream(huc.getOutputStream());
String postdata="username=ABCD00000&password=33445200&submit=%B5%C7%C2%BC&action=login&server=sso";
out.writeBytes(postdata);
out.flush();
out.close();
int i=huc.getResponseCode();
if(i<200 && i>300) return;
GZIPInputStream gzip = new GZIPInputStream(huc.getInputStream());
String line="";
int num;
byte[] buf=new byte[1024];
while ((num=gzip.read(buf,0,buf.length)) != -1)
{
line=line+new String(buf);
}正确的响应内容是这样的:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta http-equiv="refresh" content="2;URL=http://sso.gxnews.com.cn/login.php">
<title>正在登录.........</title>
</head>但以上代码返回后,中文会乱码
在论坛里找了一个例子:BufferedReader reader =newBufferedReader(newInputStreamReader(connection.getInputStream(),"utf-8"));//设置编码,否则中文乱码这个不行,全部都是乱码?
请问正常应该怎样取字符编码!?
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
---------------------------------------------------------------------
BufferedReader reader =newBufferedReader(newInputStreamReader(connection.getInputStream(),"gb2312"));这样我也试过,读出也乱码????
问题出在这句:
huc.addRequestProperty("Accept-Encoding","gzip, deflate");
删除了以后就可以用"gb2312"取代码
但问题又来了
为什么用了gzip后,再用GZIPInputStream解析后中文会错呢???