Answer the question
In order to leave comments, you need to log in
POST request to Habr in Android?
How to make a POST request to Habr with parameters on Android? Not even so much on android as using Java.
I'm trying to implement a request to add a post or question to favorites. I tracked how a similar request occurs in the browser - using jQuery.post () with parameters:
$.post({
url: '/json/favorite',
data: {tt: target_type, ti: target_id, action: action},
function(json){
// Обработка результата
},
'json'
});
Connection.Response response = Jsoup.connect("http://habrahabr.ru/json/favorites/")
.header("Content-Type","application/x-www-form-urlencoded;charset=UTF-8")
.ignoreContentType(true)
.cookie(User.PHPSESSION_ID, cookie1)
.cookie(User.HSEC_ID, cookie2)
.data("tt", targetType)
.data("ti", Integer.toString(targetId))
.data("action", "add")
.method(Connection.Method.POST)
.execute();
Log.i(Utils.TAG_MINE, "Response: " + response.parse().text());
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://habrahabr.ru/json/favorites/");
httppost.setHeader("Content-type", "application/x-www-form-urlencoded");
httppost.setHeader("Accept", "application/json");
httppost.addHeader("Cookie", " PHPSESSID=" + cookie1 + "; hsec_id=" + cookie2);
List<NameValuePair> params = new ArrayList<NameValuePair>(3);
params.add(new BasicNameValuePair("tt", targetType));
params.add(new BasicNameValuePair("ti", Integer.toString(targetId)));
params.add(new BasicNameValuePair("action", "add"));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
// Обработка ответа
{"system_errors":[["Действие не определено"],"Объект указан неверно","Тип объекта указан неверно"]}
$.post('/json/favorites/', {}, function(json){console.log(json)})
Answer the question
In order to leave comments, you need to log in
Hooray, I found what the problem is - really in the headers. Added the Referer header and everything worked correctly. Looks like Habré is validating the source of the request.
POST /json/favorites/ HTTP/1.1
Host: habrahabr.ru
Connection: keep-alive
Content-Length: 25
Accept: application/json, text/javascript, */*; q=0.01
Origin: habrahabr.ru
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: habrahabr.ru/qa/41576/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q= 0.4
Cookie: PHPSESSID=fffraae8d5; hsec_id=4fffcbdbdebe539a313368f36e6; hl_flow=posts; __utma=164318880.1913179960.1370773129.1370773129.1370784648.2; __utmb=164318880.8.10.1370784648; __utmc=164318880; __utmz=164318880.1370773129.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); _ym_visorc=b
tt=16&ti=41576&action=add
look at what headers are missing for complete happiness
, for example X-Requested-With: XMLHttpRequest
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question