求助httpclient模拟登录一直登录不上去
1个回答
展开全部
import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.util.LinkedList;
import java.util.List;
public class Login{
public static void main(String[] args) {
try {
CloseableHttpClient httpclient = HttpClients.createDefault();
List<NameValuePair> formparams = new LinkedList<NameValuePair>();
formparams.add(new BasicNameValuePair("mcei.app.terminalID", "__tid__1_"));
formparams.add(new BasicNameValuePair("mcei.html.screen", "BOLogin"));
formparams.add(new BasicNameValuePair("ehi._bid", ""));
formparams.add(new BasicNameValuePair("ehi.BannerClicked", ""));
formparams.add(new BasicNameValuePair("ehi.JAVASCRIPT_ENABLED", "TRUE"));
formparams.add(new BasicNameValuePair("mcei.html.screen++", "MainMenu"));
formparams.add(new BasicNameValuePair("bo.admin", ""));
formparams.add(new BasicNameValuePair("mcei.db.cadLogin", ""));
formparams.add(new BasicNameValuePair("bo.allid", ""));
formparams.add(new BasicNameValuePair("bo.toPage", ""));
formparams.add(new BasicNameValuePair("mcei.html.auctionId", ""));
formparams.add(new BasicNameValuePair("mcei.html.bidId", ""));
formparams.add(new BasicNameValuePair("bo.returnUrl", ""));
formparams.add(new BasicNameValuePair("mcei.bo.username", "huihuiz"));
formparams.add(new BasicNameValuePair("mcei.bo.password", "密码"));
formparams.add(new BasicNameValuePair("ehi.ProductLine", "IFP"));
formparams.add(new BasicNameValuePair("Click here to login.x", "0"));
formparams.add(new BasicNameValuePair("Click here to login.y", "0"));
//URLEncodedUtils.format(formparams,"UTF-8");
UrlEncodedFormEntity entity1 = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
HttpPost httppost = new HttpPost("登录页面");
httppost.setEntity(entity1);
HttpResponse response = httpclient.execute(httppost);//login
System.out.println(response.getStatusLine().getStatusCode());
HttpEntity entity = response.getEntity();
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpGet get = new HttpGet("登录成功后url");
CloseableHttpResponse getResponse = httpclient.execute(get);
if (getResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
Header[] heades = getResponse.getAllHeaders();
for (int i = 0; i < heades.length; i++) {
Header h = heades[i];
System.out.println(h);
}
HttpEntity getEntity = getResponse.getEntity();
String getBody = EntityUtils.toString(getEntity, "UTF-8");
System.out.println(getBody);
}
getResponse.close();
}
httpclient.close();
}
catch (java.io.UnsupportedEncodingException ex) {
ex.printStackTrace();
} catch (java.io.IOException ex) {
ex.printStackTrace();
} finally {
}
}
}
import org.apache.http.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.util.LinkedList;
import java.util.List;
public class Login{
public static void main(String[] args) {
try {
CloseableHttpClient httpclient = HttpClients.createDefault();
List<NameValuePair> formparams = new LinkedList<NameValuePair>();
formparams.add(new BasicNameValuePair("mcei.app.terminalID", "__tid__1_"));
formparams.add(new BasicNameValuePair("mcei.html.screen", "BOLogin"));
formparams.add(new BasicNameValuePair("ehi._bid", ""));
formparams.add(new BasicNameValuePair("ehi.BannerClicked", ""));
formparams.add(new BasicNameValuePair("ehi.JAVASCRIPT_ENABLED", "TRUE"));
formparams.add(new BasicNameValuePair("mcei.html.screen++", "MainMenu"));
formparams.add(new BasicNameValuePair("bo.admin", ""));
formparams.add(new BasicNameValuePair("mcei.db.cadLogin", ""));
formparams.add(new BasicNameValuePair("bo.allid", ""));
formparams.add(new BasicNameValuePair("bo.toPage", ""));
formparams.add(new BasicNameValuePair("mcei.html.auctionId", ""));
formparams.add(new BasicNameValuePair("mcei.html.bidId", ""));
formparams.add(new BasicNameValuePair("bo.returnUrl", ""));
formparams.add(new BasicNameValuePair("mcei.bo.username", "huihuiz"));
formparams.add(new BasicNameValuePair("mcei.bo.password", "密码"));
formparams.add(new BasicNameValuePair("ehi.ProductLine", "IFP"));
formparams.add(new BasicNameValuePair("Click here to login.x", "0"));
formparams.add(new BasicNameValuePair("Click here to login.y", "0"));
//URLEncodedUtils.format(formparams,"UTF-8");
UrlEncodedFormEntity entity1 = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
HttpPost httppost = new HttpPost("登录页面");
httppost.setEntity(entity1);
HttpResponse response = httpclient.execute(httppost);//login
System.out.println(response.getStatusLine().getStatusCode());
HttpEntity entity = response.getEntity();
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpGet get = new HttpGet("登录成功后url");
CloseableHttpResponse getResponse = httpclient.execute(get);
if (getResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
Header[] heades = getResponse.getAllHeaders();
for (int i = 0; i < heades.length; i++) {
Header h = heades[i];
System.out.println(h);
}
HttpEntity getEntity = getResponse.getEntity();
String getBody = EntityUtils.toString(getEntity, "UTF-8");
System.out.println(getBody);
}
getResponse.close();
}
httpclient.close();
}
catch (java.io.UnsupportedEncodingException ex) {
ex.printStackTrace();
} catch (java.io.IOException ex) {
ex.printStackTrace();
} finally {
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询