文章标题

private HttpResponse response;
private HttpClient httpclient = new DefaultHttpClient();

public Map<String,String> getParams(){

    Map<String,String> map = new HashMap<String,String>();
    String str = getText(loginURL);
    String strs1[] = str.split("name=\"struts.token.name\" value=\"");
    String strs2[] = strs1[1].split("\" />");
    map.put("structs.token.name",strs2[0]);
    String strs3[] = str.split("name=\"struts.token\" value=\"");
    String strs4[] = strs3[1].split("\" />");
    map.put("struts.token",strs4[0]);
    return map;
}
private boolean login() {
    Map map = getParams();

    HttpPost httpost = new HttpPost(loginURL);
    // All the parameters post to the web site
    List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
    nvps.add(new BasicNameValuePair("loginEmail", userName));
    nvps.add(new BasicNameValuePair("loginPassword", password));
    nvps.add(new BasicNameValuePair("__checkbox_rememberMe", "true"));
    Iterator it = map.keySet().iterator();
    while(it.hasNext()) {
        String key = it.next().toString();
        String value = map.get(key).toString();
        nvps.add(new BasicNameValuePair(key, value));
    }

    try {
      UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nvps, "UTF-8");//将表单参数转化为“实体”

        httpost.setEntity(urlEncodedFormEntity);
        response = httpclient.execute(httpost);
        HttpEntity entity = response.getEntity();

        org.apache.http.Header[] headers = response.getHeaders("Location");
        String responseString = EntityUtils.toString(entity, "UTF-8");
        System.out.println(responseString);



    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        httpost.abort();
    }
    return true;
}


private String getRedirectLocation() {
    BufferedHeader locationHeader =  (BufferedHeader) response.getFirstHeader("Location");
    if (locationHeader == null) {
        return null;
    }
    return locationHeader.getValue();
}


private String getText(String redirectLocation) {
    HttpGet httpget = new HttpGet(redirectLocation);
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = "";
    try {
        responseBody = httpclient.execute(httpget, responseHandler);
    } catch (Exception e) {
        e.printStackTrace();
        responseBody = null;
    } finally {
        httpget.abort();
    }
    return responseBody;
}
public void printText() {
    if (login()) {
        System.out.println(getText(redirectURL));
        String test = getText(redirectURL);
        String redirectLocation = getRedirectLocation();
        if (redirectLocation != null) {
            System.out.println(getText(redirectLocation));
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值