1

私はこのコードを持っています.魔女は問題なくコンパイルして実行します:

package isitup;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.client.CookieStore;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.cookie.CookieOrigin;
import org.apache.http.cookie.CookieSpec;
import org.apache.http.cookie.CookieSpecFactory;
import org.apache.http.cookie.MalformedCookieException;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.cookie.BrowserCompatSpec;
import org.apache.http.params.HttpParams;

public class Test {
     public static void main(String args[])
    {
        try
        {

            DefaultHttpClient httpclient = new DefaultHttpClient();

            HttpGet httpget = new HttpGet("http://www.cashialize.com/wp-login.php");

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

            System.out.println("Login form get: " + response.getStatusLine());
            if (entity != null) {
                EntityUtils.consume(entity);
            }
            System.out.println("Initial set of cookies:");
            List<Cookie> cookies = httpclient.getCookieStore().getCookies();
            if (cookies.isEmpty()) {
                System.out.println("None");
            } else {
                for (Cookie cooky : cookies) {
                    System.out.println("- " + cooky.toString());
                }
            }

            HttpPost httpost = new HttpPost("http://www.cashialize.com/wp-login.php");

            List <NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("log", "test"));
            nvps.add(new BasicNameValuePair("pwd", "test"));
            nvps.add(new BasicNameValuePair("rememberme", "forever"));
            nvps.add(new BasicNameValuePair("redirect_to", "http://www.articlepub.com/wp-admin/"));
            nvps.add(new BasicNameValuePair("testcookie", "1"));
            nvps.add(new BasicNameValuePair("wp-submit", "Log In"));


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

            response = httpclient.execute(httpost);
            entity = response.getEntity();

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

            System.out.println("Post logon cookies:");
            cookies = httpclient.getCookieStore().getCookies();
            if (cookies.isEmpty()) {
                System.out.println("None");
            } else {
                for (Cookie cooky : cookies) {
                    CookieStore cookieStore = new BasicCookieStore();

            // Bind custom cookie store to the local context
            httpclient.setCookieStore(cookieStore);
            CookieSpecFactory csf = new CookieSpecFactory() {
                public CookieSpec newInstance(HttpParams params) {
                    return new BrowserCompatSpec() {
                        @Override
                        public void validate(Cookie cookie, CookieOrigin origin)
                                throws MalformedCookieException {
                            // allow all cookies
                        }
                    };
                }
            };
            httpclient.getCookieSpecs().register("easy", csf);
            httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, "easy");
                }
            }




            HttpGet httpGet = new HttpGet("http://www.articlepub.com/wp-admin/profile.php");
            response = httpclient.execute(httpGet);
            entity = response.getEntity();
           // System.out.println("Page Contents: " + EntityUtils.toString(entity));


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

            System.out.println("Post get cookies:");
            cookies = httpclient.getCookieStore().getCookies();
            if (cookies.isEmpty()) {
                System.out.println("None");
            } else {
                for (Cookie cooky : cookies) {
                    System.out.println("- " + cooky.toString());
                }
            }


            httpclient.getConnectionManager().shutdown();

        } catch (Exception e) {
            System.out.println(e);

        }

    }

}

問題は、ユーザーがログインできないことです。Cookie の設定に何かが欠けていることは確かですが、最初のログイン試行の 1 つであるため、それを見つけることができません。

コンソール出力も問題ないようです。唯一の問題は、Cookie を見つけられないことです。ここにあります:

Login form get: HTTP/1.1 200 OK
Initial set of cookies:
None
Login form get: HTTP/1.1 302 Found
Post logon cookies:
None
Login form get: HTTP/1.1 200 OK
Post get cookies:
None

この悪夢を助けてください!本当に小さなものが欠けていると確信していますが、それを理解することはできません。

4

1 に答える 1