Android无法在GET后执行POST请求

时间:2014-07-23 08:27:32

标签: android post get internal-server-error

我的问题是:在GET请求授权和保存cookie后尝试执行POST请求添加数据,但服务器响应500代码。什么有趣,因为如果在浏览器中POST查询字符串形式,它就会正确执行。下面的代码。

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(site + "/admin/users/login_do/?login=admin&password=demo");
HttpResponse response = httpclient.execute(httpget);
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
HttpPost httppost = new HttpPost(site + "/admin/news/add/5/item/do/");
httppost.addHeader("Cookie", "PHPSESSID="+cookies.get(0).getValue()+"; umicms_session="+cookies.get(1).getValue()+"; stat_id="+cookies.get(2).getValue());

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("active", "1"));
nameValuePairs.add(new BasicNameValuePair("name", "test"));
nameValuePairs.add(new BasicNameValuePair("data[new][anons]", "anno"));
nameValuePairs.add(new BasicNameValuePair("data[new][content]", "cont"));
nameValuePairs.add(new BasicNameValuePair("data[new][publish_time]", "1420202020"));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
response = httpclient.execute(httppost);
StatusLine status = response.getStatusLine();
Log.d("my",String.valueOf(status.getStatusCode()));
Log.d("my",String.valueOf(status.getReasonPhrase()));

我尝试使用HttpURLConnection运行POST,但也收到500响应。 谁能说出可能出现的问题?

1 个答案:

答案 0 :(得分:0)

问题解决了。我忘了发送带有POST请求的csrf令牌并设置Referer头

nameValuePairs.add(new BasicNameValuePair("csrf", csrf));

httppost.addHeader("Referer", referlink);
相关问题