Android to Drupal cookie transfer
Can someone please enlighten me on how I might send Drupal
login cookie information from my Android
app back to my Drupal
site?
Below is the code I am using in my attempts:
HttpResponse response;
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://test2.icerge.com/testpoint/node/");
**httpPost.addHeader("Cookie: " + USERPREFERENCES.getString(COOKIE_NAME, ""), " "+USERPREFERENCES.getString(COOKIE_VALUE, ""));**
Toast.LENGTH_LONG).show();
// TODO Auto-generated method stub
try{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add( new BasicNameValuePair("node[title]", "sample node from开发者_Go百科 app") );
nameValuePairs.add( new BasicNameValuePair("node[type]", "story") );
nameValuePairs.add( new BasicNameValuePair("node[body]", "sample app node body content") );
httpPost.setEntity( new UrlEncodedFormEntity(nameValuePairs));
response = httpClient.execute(httpPost);
Log.i("SEEMS TO WORK", response.toString());
Log.v("CODE", httpPost.getRequestLine().toString() + " - " + response.toString());
}catch(Exception e){
Log.e("HTTP-ERROR: node creation", e.toString());
}
I am using the "httpPost addHeader" line to send my cookie, but nojoy.
Can someone please direct me on this issue, please?
I do not believe that the HttpClient manages cookies by default. So you need to set up a cookie store, bind it the HTTP Context, and then use the context in POSTs, like this
mHttpContext = new BasicHttpContext();
mCookieStore = new CookieStore();
mHttpContext.setAttribute(ClientContext.COOKIE_STORE, mCookieStore);
...
HttpResponse response = mHttpClient.execute(mRequest, mHttpContext);
Once that is in place any cookies that sent from the Drupal site on responses will be automatically included on subsequent requests.
If you are interested to see what cookies are held by the CookieStore you can always do
List<Cookie> cookies = mCookieStore.getCookies();
精彩评论