Java 通过Http客户端使用cookies登录

Java 通过Http客户端使用cookies登录,java,cookies,apache-httpclient-4.x,Java,Cookies,Apache Httpclient 4.x,我已经做了这个代码 DefaultHttpClient httpclient = new DefaultHttpClient(); CookieStore cookieStore = new BasicCookieStore(); HttpContext httpContext = new BasicHttpContext(); httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

我已经做了这个代码

    DefaultHttpClient httpclient = new DefaultHttpClient();
    CookieStore cookieStore = new BasicCookieStore();
    HttpContext httpContext = new BasicHttpContext();
    httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    HttpGet httpget = new HttpGet("http://localhost:8080/Account/Login");

    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();

    System.out.println("Login form get: " + response.getStatusLine());
    if (entity != null) {
        InputStream is = entity.getContent();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String str ="";
    while ((str = br.readLine()) != null){
        System.out.println(""+str);
    }
        entity.consumeContent();
    }
    System.out.println("Initial set of cookies:");
    List<Cookie> cookies = httpclient.getCookieStore().getCookies();
    if (cookies.isEmpty()) {
        System.out.println("None");
    } else {
        for (int i = 0; i < cookies.size(); i++) {
            System.out.println("- " + cookies.get(i).toString());
        }
    }

    HttpPost httpost = new HttpPost("http://localhost:8080/Account/Login");

    List <NameValuePair> nvps = new ArrayList <NameValuePair>();
    nvps.add(new BasicNameValuePair("Username", "e"));
    nvps.add(new BasicNameValuePair("Password", "password"));

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

    response = httpclient.execute(httpost);
    entity = response.getEntity();
    System.out.println("Double check we've got right page " + EntityUtils.toString(entity));

    System.out.println("Login form get: " + response.getStatusLine());
    if (entity != null) {
        entity.consumeContent();
    }

    System.out.println("Post logon cookies:");
    cookies = httpclient.getCookieStore().getCookies();
    if (cookies.isEmpty()) {
        System.out.println("None");
    } else {
        for (int i = 0; i < cookies.size(); i++) {
            System.out.println("2nd- " + cookies.get(i).toString());
        }
    }
这是get请求:

    GET http://localhost:8080/netbank/login/ajaxSuccess HTTP/1.1 Host: localhost:8080
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0
    Accept: application/json, text/javascript, */*; q=0.01
    Accept-Language: en-US,en;q=0.5
    Accept-Encoding: gzip, deflate
    Referer: http://localhost:8080/netbank/bank/login
    X-Requested-With: XMLHttpRequest
    Content-Type: application/x-www-form-urlencoded
    Cookie: JSESSIONID=E2152CA4CE8B0BB8375F216A30E32EC0
    Connection: keep-alive
    Pragma: no-cache
    Cache-Control: no-cache

    **GET SERVER RESPONSE** 

    HTTP/1.1 200 OK
    Server: Apache-Coyote/1.1
    Content-Type: application/json;charset=UTF-8
    Transfer-Encoding: chunked
    Date: Fri, 04 Oct 2013 02:51:54 GMT

    35
    {"success":true,"change":false,"username":(Username)}
    0

我认为当您执行
response=httpclient.execute(httpget)时
response=httpclient.execute(httpost)您需要包括
HttpContext
作为参数,以便发送cookie。 看

顺便说一句,如果你列出了你的程序的输出以及你希望它是什么,这也会很有帮助


另外,我建议您使用某种HTTP流量检查工具,这样您就可以准确地看到每个请求和响应中发送的内容。例如,如果您使用的是常规(未加密)HTTP,则可以使用。还有一些很好的调试工具可以通过充当HTTP(S)代理来工作,例如。

如果您解释了希望程序执行的操作以及在当前版本中不工作的操作,这将非常有用。@iX3从这段代码中,我可以获得cookie,并且网页的状态为200(确定)。从那以后,我想转到另一个页面,使用给定的cookies,直到现在,这是不起作用的。。每次我在本地主机中获得另一个网页:80。它只显示登录页面的html代码,也就是说,它还没有登录。顺便说一句,你应该小心从任何类型的捕获中删除敏感信息。我假设这一切都来自测试系统,所以没关系,但通常最好用(用户名)和(正确密码)@iX3-yeah、,它的测试系统来替换用户名和密码之类的内容,但谢谢你提醒我,不,对不起,HTTP响应不会以GET、POST开头,等等。它通常是这样的:
HTTP/1.1200ok
其中1.1是协议版本,200是状态码,“OK”是原因短语。Fiddler在inspector的下窗格中显示(单击“原始”视图)。请参阅以了解更多详细信息。输出仅显示登录页面html,,我想它没有使用cookie到我想要的页面。在我添加上下文以执行方法后,它仍然提供相同的输出。请使用我建议的工具之一查看实际的网络流量。你可以将HTTP请求和响应数据粘贴到你的问题中。我不知道这是否是你想到的正确数据。。我把它粘贴到我的问题中是的,这就是我要找的,除了它没有显示服务器的响应,它只显示了两个请求。请张贴回复。不过我注意到的一件事是,cookie中的
JSESSIONID
值在
GET
POST
之间变化。嗯,我不知道这是否是服务器响应的意思,fiddler只是在登录后才给我这些信息。,,
    GET http://localhost:8080/netbank/login/ajaxSuccess HTTP/1.1 Host: localhost:8080
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0
    Accept: application/json, text/javascript, */*; q=0.01
    Accept-Language: en-US,en;q=0.5
    Accept-Encoding: gzip, deflate
    Referer: http://localhost:8080/netbank/bank/login
    X-Requested-With: XMLHttpRequest
    Content-Type: application/x-www-form-urlencoded
    Cookie: JSESSIONID=E2152CA4CE8B0BB8375F216A30E32EC0
    Connection: keep-alive
    Pragma: no-cache
    Cache-Control: no-cache

    **GET SERVER RESPONSE** 

    HTTP/1.1 200 OK
    Server: Apache-Coyote/1.1
    Content-Type: application/json;charset=UTF-8
    Transfer-Encoding: chunked
    Date: Fri, 04 Oct 2013 02:51:54 GMT

    35
    {"success":true,"change":false,"username":(Username)}
    0