omalyutin
omalyutin

Reputation: 454

Android HTTP Post authorization request

I am trying to send a HTTP Post request in Android application.

This is what should I send: enter image description here enter image description here

This is what should I receive:

enter image description here

So, to conclude, I need to get cookies from response and response code should be 302.

I have a following Android code:

 private static String makePostRequest() {

    HttpClient httpClient = new DefaultHttpClient();
                            // replace with your url
    HttpPost httpPost = new HttpPost("http://g1.botva.ru/login.php"); 

    boolean flag = httpClient.getParams().isParameterTrue(ClientPNames.HANDLE_REDIRECTS);

    httpPost.addHeader("Accept", "*/*");
    httpPost.addHeader("Accept-Encoding", "gzip, deflate");
    //httpPost.addHeader("Content-Length", "83");
    httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
    httpPost.addHeader("User-Agent", "runscope/0.1");

    Header [] arr = httpPost.getAllHeaders();

    String result123 = "";
    //Post Data
    List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(5);

    nameValuePair.add(new BasicNameValuePair("do_cmd", "login"));
    nameValuePair.add(new BasicNameValuePair("server", "1"));
    nameValuePair.add(new BasicNameValuePair("email", "[email protected]"));
    nameValuePair.add(new BasicNameValuePair("password", "avmalyutin1234"));
    nameValuePair.add(new BasicNameValuePair("remember", "1"));



    //Encoding POST data
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
    } catch (UnsupportedEncodingException e) {
        // log exception
        e.printStackTrace();
    }

    //making POST request.
    try {
        HttpResponse response = httpClient.execute(httpPost);
        String getCookie = response.getHeaders("Pragma").length + "";
        result123 = response.getStatusLine().getStatusCode()+"";
        // write response to log
        Log.d("Http Post Response:", response.toString());
    } catch (ClientProtocolException e) {
        // Log exception
        e.printStackTrace();
    } catch (IOException e) {
        // Log exception
        e.printStackTrace();
    }

    return result123;
}

And I receive code 200. And there is only PHPSESSIONID in Cookies, but there are no other cookies.

Upvotes: 0

Views: 928

Answers (2)

omalyutin
omalyutin

Reputation: 454

Adding function

con.setInstanceFollowRedirects(false);

resolve the problem and it stops redirecting. And also I received code 302 as desired.

Thanks everybody for help

Upvotes: 0

Robert Rowntree
Robert Rowntree

Reputation: 6289

You will need to instrument the DefaultHttpClient so that you can see the WIRE and the HEADERS. Google log WIRE HEADERS for the client you are using. If you can not figure out the logging debug ( you may want to consider DIFF client )

Then, compare the 2 frameworks doing the post (your test harness VS android ) just narrowing the differences between what headers + POST BODY are being sent. As you get android to converge with your test harness, the android will produce the same 302 result you report getting from the test harness.

Upvotes: 1

Related Questions