0

我已经做了这个代码

    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());
        }
    }

这已经对了吗??以及如何访问另一个网址???让我们说从登录页面之后,我想去 localhost:80/test 页面。帮我解决这个问题。我是这个http客户端的新手。

这是发布请求:

    POST http://localhost:8080/netbank/j_spring_security_check?spring-security-redirect=/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
    Content-Type: application/x-www-form-urlencoded; charset=UTF-8
    X-Requested-With: XMLHttpRequest
    Referer: http://localhost:8080/netbank/bank/login
    Content-Length: 176
    Cookie: JSESSIONID=0A219D4AE7171E3C495443C1063B49F1
    Connection: keep-alive
    Pragma: no-cache
    Cache-Control: no-cache

    j_username=(Username)&j_password=(password)

    **POST SERVER RESPONSE**

    HTTP/1.1 302 Moved Temporarily
    Server: Apache-Coyote/1.1
    Set-Cookie: JSESSIONID=E2152CA4CE8B0BB8375F216A30E32EC0; Path=/netbank
    Location: http://localhost:8080/netbank/login/ajaxSuccess
    Content-Length: 0
    Date: Fri, 04 Oct 2013 02:51:54 GMT

这是获取请求:

    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
4

1 回答 1

0

我认为当你这样做时response = httpclient.execute(httpget);response = httpclient.execute(httpost);你需要包含 HttpContext作为参数以便发送 cookie。请参阅HttpClient.execute(HttpUriRequest request, HttpContext context)

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

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

于 2013-09-27T15:39:37.797 回答