Java 使用收到的cookie登录后再次发布
我正在开发一个登录服务,它让用户登录,然后在成功登录后,它会使用登录时提供的cookie再次发布到新脚本,以获取更多信息。这是我的登录帖子:Java 使用收到的cookie登录后再次发布,java,android,Java,Android,我正在开发一个登录服务,它让用户登录,然后在成功登录后,它会使用登录时提供的cookie再次发布到新脚本,以获取更多信息。这是我的登录帖子: @Override protected Boolean doInBackground(Void... params) { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("htt
@Override
protected Boolean doInBackground(Void... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://testsite.com/login");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("userid", "john"));
nameValuePairs.add(new BasicNameValuePair("password", "test"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String TAG = "com.imtins.worryfree";
String responseAsText = EntityUtils.toString(response.getEntity());
Log.d(TAG, "Response from server: " + responseAsText.toString());
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
@覆盖
受保护的布尔doInBackground(Void…params){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://testsite.com/login");
试一试{
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“userid”,“john”));
添加(新的BasicNameValuePair(“密码”、“测试”));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
String TAG=“com.imtins.worryfree”;
字符串responseAsText=EntityUtils.toString(response.getEntity());
d(标记“来自服务器的响应:”+responsesText.toString());
}捕获(客户端协议例外e){
}捕获(IOE异常){
}
现在,从我读到的内容来看,如果我使用同一个hpptClient而不启动新的客户端,当我发布另一篇文章时,它将使用我收到的cookie?我可以在我的示例中在哪里添加第二篇文章,或者它的外观如何。刚刚开始使用android/Java,所以这对我来说有点困惑
谢谢。您可以使用HttpContext+CookieStore来跟踪请求之间的cookie状态。我认为这样的功能对您很有用(未经测试):
在httpclient.execute(httppost)之后的同一方法中,您可以创建另一个httppost对象并激发httpclient.execute()again@Gaurav你能告诉我那会是什么样子吗?我会接受你的回答
@Override
protected Boolean doInBackground(Void... params) {
HttpClient httpclient = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpPost httppost = new HttpPost("http://testsite.com/login");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("userid", "john"));
nameValuePairs.add(new BasicNameValuePair("password", "test"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost, localContext);
String TAG = "com.imtins.worryfree";
String responseAsText = EntityUtils.toString(response.getEntity());
Log.d(TAG, "Response from server: " + responseAsText.toString());
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
// replace XXX below with correct URL
httppost = new HttpPost("http://testsite.com/XXXXXX");
try {
// set entities here ...
HttpResponse response = httpclient.execute(httppost, localContext);
String TAG = "com.imtins.worryfree";
String responseAsText = EntityUtils.toString(response.getEntity());
Log.d(TAG, "Response from server: " + responseAsText.toString());
} catch (ClientProtocolException e) {
} catch (IOException e) {
}