A
A
Allesad2013-06-09 16:20:31
Java
Allesad, 2013-06-09 16:20:31

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'
});

I'm trying to send a similar request in two ways: using Jsoup and Apache tools (package org.apache.http.client).
jsoup:
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());

Through Apache:
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();

// Обработка ответа

In both cases, the same result is returned:
{&quot;system_errors&quot;:[[&quot;Действие не определено&quot;],&quot;Объект указан неверно&quot;,&quot;Тип объекта указан неверно&quot;]}

, which means that the tt, ti, action parameters were not passed or processed. The same result can be obtained if we send a request from the console in the browser:
$.post('/json/favorites/', {}, function(json){console.log(json)})

But parameters after all should be transferred by both methods without problems. For testing, I even launched a local server, wrote a method with parameters on it, and accessed this method both from the browser and from the device - in both cases, the parameters were transferred normally.
I tried to experiment with headers and sent the same ones that are sent through the browser during a normal request - there is no result.
I do not understand where to dig further.

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
Allesad, 2013-06-09
@Allesad

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.

D
Dmitry Skogorev, 2013-06-09
@EnterSandman

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 question

Ask a Question

731 491 924 answers to any question