模拟登陆获取cookie,利用cookie去访问另外一个URL。 获取是返回了,但是怎么乱码= =!已经设置成GBK了。
我实在看不出什么地方有问题。 那位大哥指点指点,我用了2种方法还是乱码第一个我直接post的得到的最早测的时候不会得到乱码加了模拟登陆得到cookie后就变成乱码我实在看不出哪儿有问题。
第二个我是用HttpClient做的我也看不出哪儿有问题啊。。纠结!!
/**
* 获取页面特定值
* */
Vector sum= new Vector();
Vector t= new Vector();
Vector loginNum= new Vector();
Cookie[] cookies;
public String sendPost(String urlstr,String datatime,String name,String pw) {
String s ="纭畾";
String s1 = name; //账号
System.out.println("Nmae:"+name+" pw:"+pw);
HttpClient client = new HttpClient();
client.getParams().setContentCharset("GBK");
//模拟登录页面
PostMethod post = new PostMethod("http://www.newding.com:10010/base/clogin.aspx");
NameValuePair __EVENTTARGET = new NameValuePair("__EVENTTARGET","");
NameValuePair __EVENTARGUMENT = new NameValuePair("__EVENTARGUMENT","");
NameValuePair __VIEWSTATE= new NameValuePair("__VIEWSTATE","/wEPDwULLTEyMzg3MzQ5NTlkZJ/Xc1JN2Rn20LPYKgYysnkAdmg4");
NameValuePair __EVENTVALIDATION = new NameValuePair("__EVENTVALIDATION","/wEWBAKUsbLVDwKJv7HOBgLVqbaRCwKC3IeGDHXkLLYnRkl4rjuQD9WGXWkcgThI");
NameValuePair TxtLoginName = new NameValuePair("TxtLoginName",s1);
NameValuePair TxtPassword = new NameValuePair("TxtPassword",pw);
NameValuePair btnLogin = new NameValuePair("btnLogin",s);
post.setRequestBody(new NameValuePair[]{__EVENTTARGET,__EVENTARGUMENT,__VIEWSTATE,__EVENTVALIDATION,TxtLoginName,TxtPassword,btnLogin});
try {
int status = client.executeMethod(post);
} catch (HttpException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
post.releaseConnection(); cookies = client.getState().getCookies();
if (cookies.length == 0) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.length; i++) {
System.out.println("Cookie:"+cookies[0].toString()); //打印得到的cookie。 利用这个cookie访问下的网页!
}
}
/**
这个是第一个方法。 = =!得到中文乱码,原先我测还没加了新功能变这样了。
*/
try { URL url = new URL(urlstr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//channel=cid=29&cpw=292999&type=1
conn.setRequestProperty("Cookie",cookies[0].toString());
conn.setDoOutput(true);
conn.setUseCaches(false);
OutputStreamWriter out = new OutputStreamWriter(conn
.getOutputStream());
String str = String.format(Locale.CHINESE,"date1=%s&&date1=%s&&ver=%s", datatime, "", "all");
out.write(str);
out.flush();
out.close();
String type = conn.getContentType();
InputStream reader = new BufferedInputStream(conn.getInputStream());
byte[] data = new byte[10000];
int bytesRead = 0;
int offset = 0;
while (offset < 10000) {
bytesRead = reader.read(data, offset, data.length - offset);
if (bytesRead == -1) {
break;
}
offset += bytesRead; } reader.close(); // System.out.println(new String(data, type.split("charset=")[1]));
// System.out.println(line.length());
String line = new String(data, type.split("charset=")[1]);
for(int i=0;i<line.length();i++){
int first = line.indexOf("<td>");
int end = line.indexOf("</td>",first);
if (first != -1 || end != -1) {
String temp =line.substring((first+4 ), end );
int i_item=temp.indexOf("-");
int i_Sum=temp.indexOf("总数");
if(i_item!=-1){
t.addElement(temp);
}else if(i_Sum !=-1){
sum.addElement(temp);
}else{
loginNum.addElement(temp);
}
}
line = line.substring((end+4),line.length());
}
for(int i1=0;i1<t.size();i1++){
// System.out.println("t"+i1+t.elementAt(i1).toString());
if(datatime.equals(t.elementAt(i1).toString())){
return loginNum.elementAt(i1).toString();
}
}
// System.out.println("sum"+i1+sum.elementAt(i1).toString());
if(datatime.equals("sum")){
return loginNum.elementAt(10).toString();
}
for(int i1=0;i1<loginNum.size();i1++){
// System.out.println("loginNum"+i1+loginNum.elementAt(i1).toString());
//if(datatime.equals()){}
}
} catch (Exception x) {
return null;
// System.out.println(x.toString());
}
return null;
/**
这个是我用HttpClient做的第二个方法。 = =!得到中文乱码。
*/ /* NameValuePair date1 = new NameValuePair("date1",datatime);
NameValuePair date2 = new NameValuePair("date2","");
NameValuePair ver = new NameValuePair("ver","all");
HttpClient httpClient = new HttpClient();
httpClient.getParams().setContentCharset("GBK");
PostMethod postMethod = new PostMethod("http://www.newding.com:10010/newdingcall/SearchChannel2_check.aspx");
List<Header> headers = new ArrayList<Header>();
headers.add(new Header("Cookie", cookies[0].toString()));
httpClient.getHostConfiguration().getParams().setParameter("http.default-headers", headers); //发送头信息
postMethod.setRequestBody(new NameValuePair[]{date1,date2,ver});
// 使用系统提供的默认的恢复策略
postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
try
{
// (4)执行postMethod
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode != HttpStatus.SC_OK)
{
System.err.println("Method failed: " + postMethod.getStatusLine());
}
// (6)读取内容
byte[] responseBody = postMethod.getResponseBody();
String line = new String(responseBody);
System.out.println(line);
for(int i=0;i<line.length();i++){
int first = line.indexOf("<td>");
int end = line.indexOf("</td>",first);
if (first != -1 || end != -1) {
String temp =line.substring((first+4 ), end );
int i_item=temp.indexOf("-");
int i_Sum=temp.indexOf("总数");
if(i_item!=-1){
// System.out.println("t:"+temp);
t.addElement(temp);
}else if(i_Sum !=-1){
sum.addElement(temp);
}else{
// System.out.println("loginNum:"+temp);
loginNum.addElement(temp);
}
}
line = line.substring((end+4),line.length());
}
for(int i1=0;i1<t.size();i1++){
// System.out.println("t"+i1+t.elementAt(i1).toString());
if(datatime.equals(t.elementAt(i1).toString())){
System.out.println("datatime:"+loginNum.elementAt(i1).toString()+" "+t.elementAt(i1).toString());
return loginNum.elementAt(i1).toString();
}
}
// System.out.println("sum"+i1+sum.elementAt(i1).toString());
if(datatime.equals("sum")){
return loginNum.elementAt(10).toString();
}
for(int i1=0;i1<loginNum.size();i1++){
// System.out.println("loginNum"+i1+loginNum.elementAt(i1).toString());
//if(datatime.equals()){}
}
} catch (HttpException e)
{
// 发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
} finally {
// 释放连接
postMethod.releaseConnection();
}
return null; */
}
我实在看不出什么地方有问题。 那位大哥指点指点,我用了2种方法还是乱码第一个我直接post的得到的最早测的时候不会得到乱码加了模拟登陆得到cookie后就变成乱码我实在看不出哪儿有问题。
第二个我是用HttpClient做的我也看不出哪儿有问题啊。。纠结!!
/**
* 获取页面特定值
* */
Vector sum= new Vector();
Vector t= new Vector();
Vector loginNum= new Vector();
Cookie[] cookies;
public String sendPost(String urlstr,String datatime,String name,String pw) {
String s ="纭畾";
String s1 = name; //账号
System.out.println("Nmae:"+name+" pw:"+pw);
HttpClient client = new HttpClient();
client.getParams().setContentCharset("GBK");
//模拟登录页面
PostMethod post = new PostMethod("http://www.newding.com:10010/base/clogin.aspx");
NameValuePair __EVENTTARGET = new NameValuePair("__EVENTTARGET","");
NameValuePair __EVENTARGUMENT = new NameValuePair("__EVENTARGUMENT","");
NameValuePair __VIEWSTATE= new NameValuePair("__VIEWSTATE","/wEPDwULLTEyMzg3MzQ5NTlkZJ/Xc1JN2Rn20LPYKgYysnkAdmg4");
NameValuePair __EVENTVALIDATION = new NameValuePair("__EVENTVALIDATION","/wEWBAKUsbLVDwKJv7HOBgLVqbaRCwKC3IeGDHXkLLYnRkl4rjuQD9WGXWkcgThI");
NameValuePair TxtLoginName = new NameValuePair("TxtLoginName",s1);
NameValuePair TxtPassword = new NameValuePair("TxtPassword",pw);
NameValuePair btnLogin = new NameValuePair("btnLogin",s);
post.setRequestBody(new NameValuePair[]{__EVENTTARGET,__EVENTARGUMENT,__VIEWSTATE,__EVENTVALIDATION,TxtLoginName,TxtPassword,btnLogin});
try {
int status = client.executeMethod(post);
} catch (HttpException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
post.releaseConnection(); cookies = client.getState().getCookies();
if (cookies.length == 0) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.length; i++) {
System.out.println("Cookie:"+cookies[0].toString()); //打印得到的cookie。 利用这个cookie访问下的网页!
}
}
/**
这个是第一个方法。 = =!得到中文乱码,原先我测还没加了新功能变这样了。
*/
try { URL url = new URL(urlstr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//channel=cid=29&cpw=292999&type=1
conn.setRequestProperty("Cookie",cookies[0].toString());
conn.setDoOutput(true);
conn.setUseCaches(false);
OutputStreamWriter out = new OutputStreamWriter(conn
.getOutputStream());
String str = String.format(Locale.CHINESE,"date1=%s&&date1=%s&&ver=%s", datatime, "", "all");
out.write(str);
out.flush();
out.close();
String type = conn.getContentType();
InputStream reader = new BufferedInputStream(conn.getInputStream());
byte[] data = new byte[10000];
int bytesRead = 0;
int offset = 0;
while (offset < 10000) {
bytesRead = reader.read(data, offset, data.length - offset);
if (bytesRead == -1) {
break;
}
offset += bytesRead; } reader.close(); // System.out.println(new String(data, type.split("charset=")[1]));
// System.out.println(line.length());
String line = new String(data, type.split("charset=")[1]);
for(int i=0;i<line.length();i++){
int first = line.indexOf("<td>");
int end = line.indexOf("</td>",first);
if (first != -1 || end != -1) {
String temp =line.substring((first+4 ), end );
int i_item=temp.indexOf("-");
int i_Sum=temp.indexOf("总数");
if(i_item!=-1){
t.addElement(temp);
}else if(i_Sum !=-1){
sum.addElement(temp);
}else{
loginNum.addElement(temp);
}
}
line = line.substring((end+4),line.length());
}
for(int i1=0;i1<t.size();i1++){
// System.out.println("t"+i1+t.elementAt(i1).toString());
if(datatime.equals(t.elementAt(i1).toString())){
return loginNum.elementAt(i1).toString();
}
}
// System.out.println("sum"+i1+sum.elementAt(i1).toString());
if(datatime.equals("sum")){
return loginNum.elementAt(10).toString();
}
for(int i1=0;i1<loginNum.size();i1++){
// System.out.println("loginNum"+i1+loginNum.elementAt(i1).toString());
//if(datatime.equals()){}
}
} catch (Exception x) {
return null;
// System.out.println(x.toString());
}
return null;
/**
这个是我用HttpClient做的第二个方法。 = =!得到中文乱码。
*/ /* NameValuePair date1 = new NameValuePair("date1",datatime);
NameValuePair date2 = new NameValuePair("date2","");
NameValuePair ver = new NameValuePair("ver","all");
HttpClient httpClient = new HttpClient();
httpClient.getParams().setContentCharset("GBK");
PostMethod postMethod = new PostMethod("http://www.newding.com:10010/newdingcall/SearchChannel2_check.aspx");
List<Header> headers = new ArrayList<Header>();
headers.add(new Header("Cookie", cookies[0].toString()));
httpClient.getHostConfiguration().getParams().setParameter("http.default-headers", headers); //发送头信息
postMethod.setRequestBody(new NameValuePair[]{date1,date2,ver});
// 使用系统提供的默认的恢复策略
postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
try
{
// (4)执行postMethod
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode != HttpStatus.SC_OK)
{
System.err.println("Method failed: " + postMethod.getStatusLine());
}
// (6)读取内容
byte[] responseBody = postMethod.getResponseBody();
String line = new String(responseBody);
System.out.println(line);
for(int i=0;i<line.length();i++){
int first = line.indexOf("<td>");
int end = line.indexOf("</td>",first);
if (first != -1 || end != -1) {
String temp =line.substring((first+4 ), end );
int i_item=temp.indexOf("-");
int i_Sum=temp.indexOf("总数");
if(i_item!=-1){
// System.out.println("t:"+temp);
t.addElement(temp);
}else if(i_Sum !=-1){
sum.addElement(temp);
}else{
// System.out.println("loginNum:"+temp);
loginNum.addElement(temp);
}
}
line = line.substring((end+4),line.length());
}
for(int i1=0;i1<t.size();i1++){
// System.out.println("t"+i1+t.elementAt(i1).toString());
if(datatime.equals(t.elementAt(i1).toString())){
System.out.println("datatime:"+loginNum.elementAt(i1).toString()+" "+t.elementAt(i1).toString());
return loginNum.elementAt(i1).toString();
}
}
// System.out.println("sum"+i1+sum.elementAt(i1).toString());
if(datatime.equals("sum")){
return loginNum.elementAt(10).toString();
}
for(int i1=0;i1<loginNum.size();i1++){
// System.out.println("loginNum"+i1+loginNum.elementAt(i1).toString());
//if(datatime.equals()){}
}
} catch (HttpException e)
{
// 发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
} finally {
// 释放连接
postMethod.releaseConnection();
}
return null; */
}
解决方案 »
- 麻烦大家看看我这代码。。。。隐藏的错误
- JSP如何连接、读取dbf的数据
- 图片水印坐标值算法?
- 求《spring技术手册》CD光盘
- 自己编写的JAVA类该放在那个文件下,可以用PAGE指令导入,还是先将他编译成CLASS文件放入CLASSPATH指定的目录下?????????
- 一個小問題,誰能幫我解決這段代碼。謝謝
- 请教一个有关连接中文文件名的问题?参与有分,谢谢!
- 用smartupload上传文件时的一个问题,急!
- 怎么得到当前在线人数?
- poolman连接不会释放
- 如何看懂公司的search enginee
- 抓网页得到中文乱码的问题!2种方式都出现乱码。哪位大哥帮我看看..实在看不出哪儿错的地方!
Content-Type text/html; charset=utf-8
说明网页返回的内容需要按照UTF-8来解析
2.你在request请求中设置ContentType的值在头信息中,这样就可以解析正确了,通过请求看了不应该采用GBK编码。