0

Android アプリで Apache HttpClient を使用しているときに奇妙な問題が発生します。

私のアプリは、ウェブサイトにログインしてデータを取得してからログアウトする必要があります。

私の現在のコードは次のようになります。

public class BlueClient {
    private String hostUrl;
    private DefaultHttpClient client;
    private HttpContext localContext;


    public BlueClient(String hostUrl,DefaultHttpClient httpClient,HttpContext localContext) {

        this.hostUrl = hostUrl;
        this.client = httpClient;
        this.localContext = localContext;

    }

    public boolean doLogin(String userName,String password){
        String url = getHostUrl()+"do_login";//loggin will set a session cookie
        HttpPost post = new HttpPost(url);

        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("username",userName));
            nameValuePairs.add(new BasicNameValuePair("password",password));
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = client.execute(post,localContext);

            //ok now if response is ok then return true

        } catch (Exception e) {

        }
        return false;
    }

    public MyStuff getMyStuff(){
        String url = getHostUrl()+"/getMyStuff/"; //this url requires authentication. the sesion cookie should do that 
        HttpGet get = new HttpGet(url);
        try {
            HttpResponse response = client.execute(get,localContext);
            //ok get my stuff from response and return
        } catch (Exception e) {

        }
        return null;
    }
    public boolean doLogout(){
    String url = getHostUrl()+"do_logout";//it clears the cookie so the session is invalidated
    HttpGet get = new HttpGet(url);
    try {
        HttpResponse response = client.execute(get,localContext);
        //ok the cookie is cleared
      }
    } catch (Exception e) {

    }
    return false;
  }
}

そして、これらの関数を呼び出すと、私はこれが好きです。エミュレータでは機能しますが、デバイスでは機能しません

HttpContext context = new BasicHttpContext();
CookieStore cookieStore = new BasicCookieStore();
context.setAttribute(ClientContext.COOKIE_STORE,cookieStore);
DefaultHttpClient httpClient = new DefaultHttpClient();

BlueClient myClient = new BlueClient("http://myHost.com/",httpClient,context);

myClient.doLogin("user","pass");
 // it should've printed the cookies set by the server but i get nothing here !
D.log(context.getAttribute(ClientContext.COOKIE_STORE));
// as this is another subsequesnt request it shoud carry the cookies back to the server but as the cookies are not set this function gives me nothig :-(
myClient.getMyStuff();
myClient.doLogout();

誰でもこれに光を当ててください。デバイスで動作しないのはなぜですか?

4

1 に答える 1

0

ああ、私はついに問題を見つけました!

私のサーバーは CodeIgniter で動作します。また、CodeIgniter は、セッション cookie (ci_session) の有効期限を Netscape cookie ドラフト準拠として設定しました。形式はEEE、dd-MMM-yy HH:mm:ss zzzで、HttpClient が使用するデフォルトの CookiePoicy は RFC_2109 です。そのため、httpClient が Cookie データを解析しようとすると、有効期限の解析に失敗します。Cookie ポリシーと日付形式を明示的に設定する必要がありました。

したがって、私の最終的なコードは次のようになります。

BasicHttpParams params = new BasicHttpParams();
String[] dateFormats = {"EEE, dd-MMM-yy HH:mm:ss zzz"};
params.setParameter(CookieSpecPNames.DATE_PATTERNS,Arrays.asList(dateFormats));

DefaultHttpClient httpClient = new DefaultHttpClient(params);
httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.NETSCAPE);//explicitly set the cookie policy

HttpContext context = new BasicHttpContext();
CookieStore cookieStore = new BasicCookieStore();
context.setAttribute(ClientContext.COOKIE_STORE,cookieStore);


BlueClient myClient = new BlueClient("http://myHost.com/",httpClient,context);

myClient.doLogin("user","pass");
myClient.getMyStuff();//now i get my stuff !
myClient.doLogout();

それが誰かの時間を節約することを願っています:)

于 2012-11-23T07:08:58.070 に答える