有人可以告诉我如何将Drupal
我的Android
应用中的Drupal
登录Cookie信息发送回我的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 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());
}
网站吗?
以下是我在尝试中使用的代码:
{{1}}
我使用“httpPost addHeader”行发送我的cookie,但是很高兴。
有人可以指导我这个问题吗?
答案 0 :(得分:1)
我不相信HttpClient默认管理cookie。因此,您需要设置一个cookie存储,将其绑定到HTTP上下文,然后在POST中使用上下文,如下所示
mHttpContext = new BasicHttpContext();
mCookieStore = new CookieStore();
mHttpContext.setAttribute(ClientContext.COOKIE_STORE, mCookieStore);
...
HttpResponse response = mHttpClient.execute(mRequest, mHttpContext);
一旦到位,任何从Drupal网站发送回复的cookie都将自动包含在后续请求中。
如果您有兴趣了解CookieStore持有哪些Cookie,您可以随时
List<Cookie> cookies = mCookieStore.getCookies();