Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Form-data Post request using Http Post in android

I want to send an HTTP Post request using 'form-data'. Here is a screenshot of the rest-client to show what do i want:

enter image description here

More Information related to headers:

POST /action HTTP/1.1
Host: requsrt-utl
Cache-Control: no-cache

----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="email"

[email protected]
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="password"

123456
----WebKitFormBoundaryE19zNvXGzXaLvS5C

I tried using UrlEncodedFormEntity but it is setting the content-type as 'application/x-www-form-urlencoded' which is not correct.

My Android Code:

HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(urlStr);
        try {
            UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(nameValuePairs);
            httppost.setEntity(encodedFormEntity);
            HttpResponse httpresponse = httpclient.execute(httppost);
            return httpresponse.getEntity().getContent();

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

When i am using this, web-service is not getting any parameter and hence it sending a message that 'parameters are absent'.

Please help!

like image 354
mudit Avatar asked Oct 05 '13 20:10

mudit


1 Answers

This may help you

HttpPost httppost = new HttpPost(urlStr);

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);  

nameValuePairs.add(new BasicNameValuePair("userid", "12312"));  
nameValuePairs.add(new BasicNameValuePair("sessionid", "234"));  

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

how to add parameters in android http post?

like image 168
BlaShadow Avatar answered Sep 29 '22 18:09

BlaShadow