50,687
社区成员
发帖
与我相关
我的任务
分享
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();
}
}
}