模拟登陆获取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; */
}
解决方案 »
- 局部验证码刷新的问题?
- 页面数据的保存
- 急!!关于客户端进行文件的提交
- 求救,IIS6与tomcat5整合!!!!谢谢谢谢!!!
- 刚才下了netbeans下的visual web,用了一下,很不习惯,不知道有没有jsp+beans的做法?
- JSP页面中控制输出字符的长度
- SQL与JSP的问题 简单但我不理解什么回事,求救!
- 作一个工资表程序,动态生从数据库中检索出的人名数量长度的表。。。
- 请大神帮我做一道补考题,关于struts2的!
- SSH的框架,我想弄个查询,可以方便开发的。而不需要每次都要对每个查询进行分页,条件筛选这样重复的操作。有什么好的思路吗?
- 抓网页得到中文乱码的问题!哪位大哥帮我看看..实在看不出哪儿错的地方!
- forward和redirect的区别
BufferedReader httpBufferedReader = new BufferedReader(new InputStreamReader(reader ,"UTF-8"));