Java HttpClient4.X 如何登陆ASP.NET网站?

Code菜鸟 2015-06-26 10:10:09
请教大神,如何用HttpClient4.X版本登录ASP. NET网站?我一直登陆不成功,总是返回登录界面?如何解决?
我用的登录方法如下:

package com.java.Test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

@SuppressWarnings("deprecation")
public class LoginASP
{
HttpClient client = new DefaultHttpClient();

String VIEWSTATE = "";
String EVENTVALIDATION = "";

String username = "";//登录的帐号
String pwd = "";//登录密码

public LoginASP()
{
}

public static void main(String args[])
{
LoginASP login = new LoginASP();
if(login.HttpGet())
{
login.Login();
}
}

public boolean HttpGet()
{
try
{
HttpGet get = new HttpGet("http://passport.efeihu.com/");
HttpResponse response = client.execute(get);
HttpEntity entiry = response.getEntity();
InputStream is = entiry.getContent();
InputStreamReader isr = new InputStreamReader(is,"UTF-8");
BufferedReader br = new BufferedReader(isr);
String html = "";
while((html=br.readLine())!=null)
{
if(html.indexOf("VIEWSTATE")!=-1)
{
VIEWSTATE = (html.substring(html.lastIndexOf("value=\"")+7, html.lastIndexOf("\"")));
}
else if(html.indexOf("EVENTVALIDATION")!=-1)
{
EVENTVALIDATION = (html.substring(html.lastIndexOf("value=\"")+7, html.lastIndexOf("\"")));
}
}
br.close();
isr.close();
is.close();
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
return true;
}


public void Login()
{
try {
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("__VIEWSTATE",VIEWSTATE));
parameters.add(new BasicNameValuePair("__EVENTVALIDATION", EVENTVALIDATION));
parameters.add(new BasicNameValuePair("ctl00$ContentPlaceHolder1$txtUserName", username));
parameters.add(new BasicNameValuePair("ctl00$ContentPlaceHolder1$txtPassword", pwd));
parameters.add(new BasicNameValuePair("txtUserName", username));
parameters.add(new BasicNameValuePair("txtPassword", pwd));
HttpPost post = new HttpPost("http://passport.efeihu.com/login.aspx");
post.setEntity(new UrlEncodedFormEntity(parameters));

HttpResponse response = client.execute(post);
HttpEntity entiry = response.getEntity();
InputStream is = entiry.getContent();
InputStreamReader isr = new InputStreamReader(is,"UTF-8");
BufferedReader br = new BufferedReader(isr);
String html = "";
while((html=br.readLine())!=null)
{
System.out.println(html);
}
br.close();
isr.close();
is.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

...全文
82 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
疯狂熊猫人 2015-06-27
  • 打赏
  • 举报
回复
给浏览器装一个httpwatch插件,按照正常流程走一遍,把所有的url都捕获下来,然后分析。
xinyewa 2015-06-26
  • 打赏
  • 举报
回复
看看网页源码中是否有隐藏值 需要自己获取再提交

50,687

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧