Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On Android, make a POST request with URL Encoded Form data without using UrlEncodedFormEntity

I have a string that is already in the proper URLEncoded Form format and would like to send it through a POST request on Android to a PHP server. I know the method for sending URL encoded forms on Android uses the UrlEncodedFormEntity and I know how to use it. The problem with that is that the data comes into the function already URL encoded and joined by ampersands, so using UrlEncodedFormEntity would involve a lot of extra work to turn it into a List of NameValuePairs and I'd rather not.

So, how do I make a proper POST request sending this string as the content body?

I have already tried using StringEntity, but the PHP server didn't get any of the data (empty $_POST object).

I am testing against http://test.lifewanted.com/echo.json.php which simply is

<?php echo json_encode( $_REQUEST );

Here is an example of the already-encoded data:

partnerUserID=email%40example.com&partnerUserSecret=mypassword&command=Authenticate

like image 876
Oz. Avatar asked Dec 01 '10 23:12

Oz.


People also ask

How do you send a POST request on Android?

build(); Request request = new Request. Builder() . url("https://yourdomain.org/callback.php") // The URL to send the data to . post(formBody) .

Does POST data need to be URL encoded?

The general answer to your question is that it depends. And you get to decide by specifying what your "Content-Type" is in the HTTP headers. A value of "application/x-www-form-urlencoded" means that your POST body will need to be URL encoded just like a GET parameter string.

What is URL encoded form data?

The application/x-www-form-urlencoded content type describes form data that is sent in a single block in the HTTP message body. Unlike the query part of the URL in a GET request, the length of the data is unrestricted.

What are URL encoded requests?

URL encoding is a mechanism for translating unprintable or special characters to a universally accepted format by web servers and browsers.


1 Answers

If you don't mind using an HttpURLConnection instead of the (recommended) HttpClient then you could do it this way:

public void performPost(String encodedData) {
    HttpURLConnection urlc = null;
    OutputStreamWriter out = null;
    DataOutputStream dataout = null;
    BufferedReader in = null;
    try {
        URL url = new URL(URL_LOGIN_SUBMIT);
        urlc = (HttpURLConnection) url.openConnection();
        urlc.setRequestMethod("POST");
        urlc.setDoOutput(true);
        urlc.setDoInput(true);
        urlc.setUseCaches(false);
        urlc.setAllowUserInteraction(false);
        urlc.setRequestProperty(HEADER_USER_AGENT, HEADER_USER_AGENT_VALUE);
        urlc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        dataout = new DataOutputStream(urlc.getOutputStream());
        // perform POST operation
        dataout.writeBytes(encodedData);
        int responseCode = urlc.getResponseCode();
        in = new BufferedReader(new InputStreamReader(urlc.getInputStream()),8096);
        String response;
        // write html to System.out for debug
        while ((response = in.readLine()) != null) {
            System.out.println(response);
        }
        in.close();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
like image 52
dave.c Avatar answered Oct 20 '22 18:10

dave.c