求助httpclient模拟登录一直登录不上去

 我来答
就烦条0o
2017-01-01 · 知道合伙人软件行家
就烦条0o
知道合伙人软件行家
采纳数:33315 获赞数:46485
从事多年系统运维,喜欢编写各种小程序和脚本。

向TA提问 私信TA
展开全部
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 {

}
}

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式