【问题标题】:Android cant execute POST request after GETGET后Android无法执行POST请求
【发布时间】:2014-07-23 08:27:32
【问题描述】:

我的问题是这样的:在 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 响应。 谁能说出可能是什么问题?

【问题讨论】:

    标签: android post get internal-server-error


    【解决方案1】:

    问题解决了。我忘记使用 POST 请求发送 csrf 令牌并设置Referer 标头

    nameValuePairs.add(new BasicNameValuePair("csrf", csrf));
    
    httppost.addHeader("Referer", referlink);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      • 2018-04-25
      • 1970-01-01
      • 1970-01-01
      • 2015-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多