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

これはもう大丈夫ですか??そして、別のURLにアクセスする方法??? ログインページから後で、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);と思います。HttpClient.execute(HttpUriRequest リクエスト、HttpContext コンテキスト)を参照してください。response = httpclient.execute(httpost);HttpContext

ところで、プログラムの出力と、代わりに何をしたいのかをリストしていただけると助かります。

また、何らかの HTTP トラフィック検査ツールを使用して、各リクエストとレスポンスで何が送信されているかを正確に確認することをお勧めします。たとえば、通常の (暗号化されていない) HTTP を使用している場合は、Wiresharkを使用できます。Fiddlerなど、HTTP(S) プロキシとして機能する優れたデバッグ ツールもいくつかあります。

于 2013-09-27T15:39:37.797 に答える