首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HttpClient4模拟登录总是失败,Cookie不工作,

HttpClient4模拟登录总是失败,Cookie不工作,
EN

Stack Overflow用户
提问于 2014-03-02 22:59:35
回答 1查看 315关注 0票数 0

第一个请求是成功的,但是第二个请求总是被重定向到登录页面。我使用httpclient4.0.1。

代码语言:javascript
复制
public class HttpClientLoginTest {
    <!-- The configuration items-->
    private static String userName = "admin";
    private static String password = "12315";
    private static String redirectURL = "/manager/index.php";

    private static String LoginURL = "http://localhost/manager/login.php";

    private HttpResponse response;

    private static DefaultHttpClient httpclient = new DefaultHttpClient();
    <!-- the first request is login-->
    private void login() throws Exception {

        HttpPost httpost = new HttpPost("http://localhost/manager/login.php");
        <!--  All the parameters post to the web site-->
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("gotopage", redirectURL));
        nvps.add(new BasicNameValuePair("dopost", "login"));
        nvps.add(new BasicNameValuePair("adminstyle", "newdedecms"));
        nvps.add(new BasicNameValuePair("userid", userName));
        nvps.add(new BasicNameValuePair("pwd", password));
        nvps.add(new BasicNameValuePair("sm1", ""));

        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        response = httpclient.execute(httpost);

        int responseCode = response.getStatusLine().getStatusCode();

        System.out.println("Sending 'POST' request to URL : " + LoginURL);
        System.out.println("Response Code : " + responseCode);
            <!-- print reponsebody-->
        BufferedReader rd = new BufferedReader(new InputStreamReader(response
                .getEntity().getContent()));

        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }

        System.out.println(result.toString());

        httpost.abort();

    }
    <!-- the second request-->
    private String getText(String redirectLocation) throws Exception,
            IOException {
        HttpGet request = new HttpGet(redirectLocation);

        response = httpclient.execute(request);

        int responseCode = response.getStatusLine().getStatusCode();

        System.out.println("request to URL : " + redirectLocation);
        System.out.println("Response Code : " + responseCode);
            <!-- print reponsebody-->
        BufferedReader rd = new BufferedReader(new InputStreamReader(response
                .getEntity().getContent()));

        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }

        return result.toString();
    }

    public void printText() {
        try {
            login();
            getText("http://localhost/manager/index.php");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        HttpClientLoginTest test = new HttpClientLoginTest();
        test.printText();
        httpclient.getConnectionManager().shutdown();
    }

}

在这里,日志!非常感谢你!

代码语言:javascript
复制
Get connection for route HttpRoute[{}->http://localhost]
CookieSpec selected: best-match
Attempt 1 to execute request
Sending request: POST /manager/login.php HTTP/1.1
>> "POST /manager/login.php HTTP/1.1[EOL]"
>> "Content-Length: 94[EOL]"
>> "Content-Type: application/x-www-form-urlencoded; charset=UTF-8[EOL]"
>> "Host: localhost[EOL]"
>> "Connection: Keep-Alive[EOL]"
>> "User-Agent: Apache-HttpClient/4.0.1 (java 1.5)[EOL]"
>> "Expect: 100-Continue[EOL]"
>> "[EOL]"
>> POST /manager/login.php HTTP/1.1
>> Content-Length: 94
>> Content-Type: application/x-www-form-urlencoded; charset=UTF-8
>> Host: localhost
>> Connection: Keep-Alive
>> User-Agent: Apache-HttpClient/4.0.1 (java 1.5)
>> Expect: 100-Continue
<< "HTTP/1.1 100 Continue[EOL]"
<< "[EOL]"
Receiving response: HTTP/1.1 100 Continue
<< HTTP/1.1 100 Continue
>> "gotopage=%2Fmanager%2Findex.php&dopost=login&adminstyle=newdedecms&userid=admin&pwd=12315&sm1="
<< "HTTP/1.1 200 OK[EOL]"
<< "Date: Sun, 02 Mar 2014 15:18:49 GMT[EOL]"
<< "Server: Apache/2.2.4 (Win32) PHP/5.2.4[EOL]"
<< "X-Powered-By: PHP/5.2.4[EOL]"
<< "Set-Cookie: PHPSESSID=kkjhvrbaoq2rbtk88f0nljaua2; path=/[EOL]"
<< "Expires: Thu, 19 Nov 1981 08:52:00 GMT[EOL]"
<< "Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0[EOL]"
<< "Pragma: no-cache[EOL]"
<< "Set-Cookie: DedeUserID=1; expires=Mon, 03-Mar-2014 15:18:49 GMT; path=/[EOL]"
<< "Set-Cookie: DedeUserID__ckMd5=e22b0794b6991249; expires=Mon, 03-Mar-2014 15:18:49 GMT; path=/[EOL]"
<< "Set-Cookie: DedeLoginTime=1393773529; expires=Mon, 03-Mar-2014 15:18:49 GMT; path=/[EOL]"
<< "Set-Cookie: DedeLoginTime__ckMd5=4e56d5c9755c8116; expires=Mon, 03-Mar-2014 15:18:49 GMT; path=/[EOL]"
<< "Content-Length: 932[EOL]"
<< "Keep-Alive: timeout=5, max=100[EOL]"
<< "Connection: Keep-Alive[EOL]"
<< "Content-Type: text/html; charset=gb2312[EOL]"
<< "[EOL]"
Receiving response: HTTP/1.1 200 OK
<< HTTP/1.1 200 OK
<< Date: Sun, 02 Mar 2014 15:18:49 GMT
<< Server: Apache/2.2.4 (Win32) PHP/5.2.4
<< X-Powered-By: PHP/5.2.4
<< Set-Cookie: PHPSESSID=kkjhvrbaoq2rbtk88f0nljaua2; path=/
<< Expires: Thu, 19 Nov 1981 08:52:00 GMT
<< Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
<< Pragma: no-cache
<< Set-Cookie: DedeUserID=1; expires=Mon, 03-Mar-2014 15:18:49 GMT; path=/
<< Set-Cookie: DedeUserID__ckMd5=e22b0794b6991249; expires=Mon, 03-Mar-2014 15:18:49 GMT; path=/
<< Set-Cookie: DedeLoginTime=1393773529; expires=Mon, 03-Mar-2014 15:18:49 GMT; path=/
<< Set-Cookie: DedeLoginTime__ckMd5=4e56d5c9755c8116; expires=Mon, 03-Mar-2014 15:18:49 GMT; path=/
<< Content-Length: 932
<< Keep-Alive: timeout=5, max=100
<< Connection: Keep-Alive
<< Content-Type: text/html; charset=gb2312
Cookie accepted: "[version: 0][name: PHPSESSID][value: kkjhvrbaoq2rbtk88f0nljaua2][domain: localhost][path: /][expiry: null]". 
Cookie accepted: "[version: 0][name: DedeUserID][value: 1][domain: localhost][path: /][expiry: Mon Mar 03 23:18:49 CST 2014]". 
Cookie accepted: "[version: 0][name: DedeUserID__ckMd5][value: e22b0794b6991249][domain: localhost][path: /][expiry: Mon Mar 03 23:18:49 CST 2014]". 
Cookie accepted: "[version: 0][name: DedeLoginTime][value: 1393773529][domain: localhost][path: /][expiry: Mon Mar 03 23:18:49 CST 2014]". 
Cookie accepted: "[version: 0][name: DedeLoginTime__ckMd5][value: 4e56d5c9755c8116][domain: localhost][path: /][expiry: Mon Mar 03 23:18:49 CST 2014]". 
Connection can be kept alive for 5000 ms
<< "<html>[\r][\n]"
<< "<head>[\r][\n]"
<< "<title>DedeCMS[0xcc][0xe1][0xca][0xbe][0xd0][0xc5][0xcf][0xa2]</title>[\r][\n]"
<< "<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />[\r][\n]"
<< "<base target='_self'/>[\r][\n]"
<< "<style>div{line-height:160%;}</style></head>[\r][\n]"
<< "<body leftmargin='0' topmargin='0' bgcolor='#FFFFFF'>[\r][\n]"
<< "<center>[\r][\n]"
<< "<script>[\r][\n]"
<< "      var pgo=0;[\r][\n]"
<< "      function JumpUrl(){[\r][\n]"
<< "        if(pgo==0){ location='/manager/index.php'; pgo=1; }[\r][\n]"
<< "      }[\r][\n]"
<< "document.write("<br /><div style='width:450px;padding:0px;border:1px solid #DADADA;'><div style='padding:6px;font-size:12px;border-bottom:1px solid #DADADA;background:#DBEEBD url(/plus/img/wbg.gif)';'><b>DedeCMS [0xcc][0xe1][0xca][0xbe][0xd0][0xc5][0xcf][0xa2][0xa3][0xa1]</b></div>");[\r][\n]"
<< "document.write("<div style='height:130px;font-size:10pt;background:#ffffff'><br />");[\r][\n]"
<< "document.write("[0xb3][0xc9][0xb9][0xa6][0xb5][0xc7][0xc2][0xbc][0xa3][0xac][0xd5][0xfd][0xd4][0xda][0xd7][0xaa][0xcf][0xf2][0xb9][0xdc][0xc0][0xed][0xb9][0xdc][0xc0][0xed][0xd6][0xf7][0xd2][0xb3][0xa3][0xa1]");[\r][\n]"
<< "document.write("<br /><a href='/manager/index.php'>[0xc8][0xe7][0xb9][0xfb][0xc4][0xe3][0xb5][0xc4][0xe4][0xaf][0xc0][0xc0][0xc6][0xf7][0xc3][0xbb][0xb7][0xb4][0xd3][0xa6][0xa3][0xac][0xc7][0xeb][0xb5][0xe3][0xbb][0xf7][0xd5][0xe2][0xc0][0xef]...</a><br/></div>");[\r][\n]"
<< "setTimeout('JumpUrl()',1000);</script>[\r][\n]"
<< "</center>[\r][\n]"
<< "</body>[\r][\n]"
<< "</html>[\r][\n]"
Releasing connection org.apache.http.impl.conn.SingleClientConnManager$ConnAdapter@158bc22
Get connection for route HttpRoute[{}->http://localhost]
Stale connection check
CookieSpec selected: best-match
Cookie [version: 0][name: PHPSESSID][value: kkjhvrbaoq2rbtk88f0nljaua2][domain: localhost][path: /][expiry: null] match [localhost:80/manager/index.php]
Cookie [version: 0][name: DedeUserID][value: 1][domain: localhost][path: /][expiry: Mon Mar 03 23:18:49 CST 2014] match [localhost:80/manager/index.php]
Cookie [version: 0][name: DedeUserID__ckMd5][value: e22b0794b6991249][domain: localhost][path: /][expiry: Mon Mar 03 23:18:49 CST 2014] match [localhost:80/manager/index.php]
Cookie [version: 0][name: DedeLoginTime][value: 1393773529][domain: localhost][path: /][expiry: Mon Mar 03 23:18:49 CST 2014] match [localhost:80/manager/index.php]
Cookie [version: 0][name: DedeLoginTime__ckMd5][value: 4e56d5c9755c8116][domain: localhost][path: /][expiry: Mon Mar 03 23:18:49 CST 2014] match [localhost:80/manager/index.php]
Attempt 1 to execute request
Sending request: GET /manager/index.php HTTP/1.1
>> "GET /manager/index.php HTTP/1.1[EOL]"
>> "Host: localhost[EOL]"
>> "Connection: Keep-Alive[EOL]"
>> "User-Agent: Apache-HttpClient/4.0.1 (java 1.5)[EOL]"
>> "Cookie: PHPSESSID=kkjhvrbaoq2rbtk88f0nljaua2; DedeUserID=1; DedeUserID__ckMd5=e22b0794b6991249; DedeLoginTime=1393773529; DedeLoginTime__ckMd5=4e56d5c9755c8116[EOL]"
>> "Cookie2: $Version=1[EOL]"
>> "[EOL]"
>> GET /manager/index.php HTTP/1.1
>> Host: localhost
>> Connection: Keep-Alive
>> User-Agent: Apache-HttpClient/4.0.1 (java 1.5)
>> Cookie: PHPSESSID=kkjhvrbaoq2rbtk88f0nljaua2; DedeUserID=1; DedeUserID__ckMd5=e22b0794b6991249; DedeLoginTime=1393773529; DedeLoginTime__ckMd5=4e56d5c9755c8116
>> Cookie2: $Version=1
<< "HTTP/1.1 302 Found[EOL]"
<< "Date: Sun, 02 Mar 2014 15:18:49 GMT[EOL]"
<< "Server: Apache/2.2.4 (Win32) PHP/5.2.4[EOL]"
<< "X-Powered-By: PHP/5.2.4[EOL]"
<< "Expires: Thu, 19 Nov 1981 08:52:00 GMT[EOL]"
<< "Cache-Control: private[EOL]"
<< "Pragma: no-cache[EOL]"
<< "location: login.php?gotopage=%2Fmanager%2Findex.php[EOL]"
<< "Content-Length: 0[EOL]"
<< "Keep-Alive: timeout=5, max=99[EOL]"
<< "Connection: Keep-Alive[EOL]"
<< "Content-Type: text/html; charset=gb2312[EOL]"
<< "[EOL]"
Receiving response: HTTP/1.1 302 Found
<< HTTP/1.1 302 Found
<< Date: Sun, 02 Mar 2014 15:18:49 GMT
<< Server: Apache/2.2.4 (Win32) PHP/5.2.4
<< X-Powered-By: PHP/5.2.4
<< Expires: Thu, 19 Nov 1981 08:52:00 GMT
<< Cache-Control: private
<< Pragma: no-cache
<< location: login.php?gotopage=%2Fmanager%2Findex.php
<< Content-Length: 0
<< Keep-Alive: timeout=5, max=99
<< Connection: Keep-Alive
<< Content-Type: text/html; charset=gb2312
Connection can be kept alive for 5000 ms
Redirect requested to location 'login.php?gotopage=%2Fmanager%2Findex.php'
Redirecting to 'http://localhost/manager/login.php?gotopage=%2Fmanager%2Findex.php' via HttpRoute[{}->http://localhost]
CookieSpec selected: best-match
Cookie [version: 0][name: PHPSESSID][value: kkjhvrbaoq2rbtk88f0nljaua2][domain: localhost][path: /][expiry: null] match [localhost:80/manager/login.php]
Cookie [version: 0][name: DedeUserID][value: 1][domain: localhost][path: /][expiry: Mon Mar 03 23:18:49 CST 2014] match [localhost:80/manager/login.php]
Cookie [version: 0][name: DedeUserID__ckMd5][value: e22b0794b6991249][domain: localhost][path: /][expiry: Mon Mar 03 23:18:49 CST 2014] match [localhost:80/manager/login.php]
Cookie [version: 0][name: DedeLoginTime][value: 1393773529][domain: localhost][path: /][expiry: Mon Mar 03 23:18:49 CST 2014] match [localhost:80/manager/login.php]
Cookie [version: 0][name: DedeLoginTime__ckMd5][value: 4e56d5c9755c8116][domain: localhost][path: /][expiry: Mon Mar 03 23:18:49 CST 2014] match [localhost:80/manager/login.php]
Attempt 2 to execute request
Sending request: GET /manager/login.php?gotopage=%2Fmanager%2Findex.php HTTP/1.1
>> "GET /manager/login.php?gotopage=%2Fmanager%2Findex.php HTTP/1.1[EOL]"
>> "Host: localhost[EOL]"
>> "Connection: Keep-Alive[EOL]"
>> "User-Agent: Apache-HttpClient/4.0.1 (java 1.5)[EOL]"
>> "Cookie: PHPSESSID=kkjhvrbaoq2rbtk88f0nljaua2; DedeUserID=1; DedeUserID__ckMd5=e22b0794b6991249; DedeLoginTime=1393773529; DedeLoginTime__ckMd5=4e56d5c9755c8116[EOL]"
>> "Cookie2: $Version=1[EOL]"
>> "[EOL]"
>> GET /manager/login.php?gotopage=%2Fmanager%2Findex.php HTTP/1.1
>> Host: localhost
>> Connection: Keep-Alive
>> User-Agent: Apache-HttpClient/4.0.1 (java 1.5)
>> Cookie: PHPSESSID=kkjhvrbaoq2rbtk88f0nljaua2; DedeUserID=1; DedeUserID__ckMd5=e22b0794b6991249; DedeLoginTime=1393773529; DedeLoginTime__ckMd5=4e56d5c9755c8116
>> Cookie2: $Version=1
<< "HTTP/1.1 200 OK[EOL]"
<< "Date: Sun, 02 Mar 2014 15:18:49 GMT[EOL]"
<< "Server: Apache/2.2.4 (Win32) PHP/5.2.4[EOL]"
<< "X-Powered-By: PHP/5.2.4[EOL]"
<< "Expires: Thu, 19 Nov 1981 08:52:00 GMT[EOL]"
<< "Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0[EOL]"
<< "Pragma: no-cache[EOL]"
<< "Content-Length: 2069[EOL]"
<< "Keep-Alive: timeout=5, max=98[EOL]"
<< "Connection: Keep-Alive[EOL]"
<< "Content-Type: text/html; charset=gb2312[EOL]"
<< "[EOL]"
Receiving response: HTTP/1.1 200 OK
<< HTTP/1.1 200 OK
<< Date: Sun, 02 Mar 2014 15:18:49 GMT
<< Server: Apache/2.2.4 (Win32) PHP/5.2.4
<< X-Powered-By: PHP/5.2.4
<< Expires: Thu, 19 Nov 1981 08:52:00 GMT
<< Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
<< Pragma: no-cache
<< Content-Length: 2069
<< Keep-Alive: timeout=5, max=98
<< Connection: Keep-Alive
<< Content-Type: text/html; charset=gb2312
Connection can be kept alive for 5000 ms
<< "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[\r][\n]"
<< "<html xmlns="http://www.w3.org/1999/xhtml">[\r][\n]"
<< "<head>[\r][\n]"
<< "<meta http-equiv="Content-Type" content="text/html; charset=gb2312">[\r][\n]"
<< "<title>[0xd6][0xaf][0xc3][0xce][0xc4][0xda][0xc8][0xdd][0xb9][0xdc][0xc0][0xed][0xcf][0xb5][0xcd][0xb3] V57_GBK</title>[\r][\n]"
<< "<link href="css/base.css" rel="stylesheet" type="text/css" />[\r][\n]"
<< "<link href="css/login.css" rel="stylesheet" type="text/css" />[\r][\n]"
<< "<script src="../include/js/jquery/jquery.js" language="javascript" type="text/javascript"></script>[\r][\n]"
<< "<script type="text/javascript">[\r][\n]"
<< "$ = jQuery;[\r][\n]"
<< "function changeAuthCode() {[\r][\n]"
<< "[0x9]var num = [0x9]new Date().getTime();[\r][\n]"
<< "[0x9]var rand = Math.round(Math.random() * 10000);[\r][\n]"
<< "[0x9]num = num + rand;[\r][\n]"
<< "[0x9]$('#ver_code').css('visibility','visible');[\r][\n]"
<< "[0x9]if ($("#vdimgck")[0]) {[\r][\n]"
<< "[0x9][0x9]$("#vdimgck")[0].src = "../include/vdimgck.php?tag=" + num;[\r][\n]"
<< "[0x9]}[\r][\n]"
<< "[0x9]return false;[0x9][\r][\n]"
<< "}[\r][\n]"
<< "</script>[\r][\n]"
<< "</head>[\r][\n]"
<< "<body>[\r][\n]"
<< "<div id="login-box">[\r][\n]"
<< "   <div class="login-top"><a href="../index.php" target="_blank" title="[0xb7][0xb5][0xbb][0xd8][0xcd][0xf8][0xd5][0xbe][0xd6][0xf7][0xd2][0xb3]">[0xb7][0xb5][0xbb][0xd8][0xcd][0xf8][0xd5][0xbe][0xd6][0xf7][0xd2][0xb3]</a></div>[\r][\n]"
<< "      <div class="login-main">[\r][\n]"
<< "    <form name="form1" method="post" action="login.php">[\r][\n]"
<< "      <input type="hidden" name="gotopage" value="/manager/index.php" />[\r][\n]"
<< "      <input type="hidden" name="dopost" value="login" />[\r][\n]"
<< "      <input name='adminstyle' type='hidden' value='newdedecms' />[\r][\n]"
<< "      <dl>[\r][\n]"
<< "[0x9]   <dt>[0xd3][0xc3][0xbb][0xa7][0xc3][0xfb][0xa3][0xba]</dt>[\r][\n]"
<< "[0x9]   <dd><input type="text" name="userid"/></dd>[\r][\n]"
<< "[0x9]   <dt>[0xc3][0xdc]&nbsp;&nbsp;[0xc2][0xeb][0xa3][0xba]</dt>[\r][\n]"
<< "[0x9]   <dd><input type="password" class="alltxt" name="pwd"/></dd>[\r][\n]"
<< "[0x9]   [0x9][0x9]<dt>&nbsp;</dt>[\r][\n]"
<< "[0x9][0x9]<dd><button type="submit" name="sm1" class="login-btn" onclick="this.form.submit();">[0xb5][0xc7][0xc2][0xbc]</button></dd>[\r][\n]"
<< "[0x9] </dl>[\r][\n]"
<< "[0x9]</form>[\r][\n]"
<< "   </div>[\r][\n]"
<< "   <div class="login-power">Powered by<a href="http://www.dedecms.com" title="DedeCMS[0xb9][0xd9][0xcd][0xf8]"><strong>DedeCMSV57_GBK</strong></a>&copy; 2004-2011 <a href="http://www.desdev.cn" target="_blank">DesDev</a> Inc.</div>[\r][\n]"
<< "</div>[\r][\n]"
<< "<div class="dede-iframe"><iframe name="loginad" src="login.php?dopost=showad" frameborder="0" id="loginad" scrolling="no" marginwidth="0" marginheight="0" width="100%"></iframe></div>[\r][\n]"
<< "</body>[\r][\n]"
<< "</html>[\r][\n]"
Releasing connection org.apache.http.impl.conn.SingleClientConnManager$ConnAdapter@5353c8
Connection shut down
EN

回答 1

Stack Overflow用户

发布于 2014-03-06 19:22:36

我得到了一些情况下的annswer.in,在表单发布之前,我们需要访问的页面first.when我们访问的页面,它会给我们一个sessionid.then的表单和会话i发布到服务器together.if我们没有先访问页面,我们直接发布的表单,我们不能登录。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22129202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档