3

I've got an AsyncTask set up to create an AndroidHttpClient and call Execute, with a HttpPost and BasicHttpContext objects I provide. I set up the HttpPost like so:

HttpPost myPost = new HttpPost("http://192.168.1.66:8080/login.html");
BasicHttpParams httpParams = new BasicHttpParams();
httpParams.setParameter("cmd", "Login");
httpParams.setParameter("Username", "test");
httpParams.setParameter("Password", "password");
myPost.setParams(httpParams);

When I execute the command, it retrieves the login page instead of the results of logging in. After monitoring the process on the server side, it seems that this is because the HTTP request arrived with no params attached! So what am I missing?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477

4 Answers4

1
List<NameValuePair> nameValuePairs=...;
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

Try this.

HttpPost docs

The HTTP POST method is defined in section 9.5 of RFC2616:

The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line...

The reason why this class has setParams() is because it implements HttpMessage that has it. It may/may not be used?(not sure, it might be there for other impelmentations?) but basically you use setEntity(new UrlEncodedFormEntity(nameValuePairs)); to post it

wtsang02
  • 18,603
  • 10
  • 49
  • 67
1
            HttpPost post = new HttpPost("http://192.168.1.66:8080/login.html");
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                    3);
            nameValuePairs.add(new BasicNameValuePair("cmd", "Login"
                    .getText().toString()));
            nameValuePairs.add(new BasicNameValuePair("Username", "test"
                    .getText().toString()));

            nameValuePairs.add(new BasicNameValuePair("Password", "Password"
                    .getText().toString()));

            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
stinepike
  • 54,068
  • 14
  • 92
  • 112
0

You need to actually set the httpParams object into the request using setEntity:

myPost.setEntity(new UrlEncodedFormEntity(httpParams, HTTP.UTF_8));

Cody Caughlan
  • 32,456
  • 5
  • 63
  • 68
0

setParams() sets parameters governing the processing of the message. These message parameters have nothing to do with the Entity transmitted with the message.

The simplest way I have found to construct a multipart entity is using the MultipartEntityBuilder (from org.apache.http.entity.mime):

  mpe = MultipartEntityBuilder.create();
  mpe.addTextBody("Username", "test");
  mpe.addTextBody("Password", "password");
  mpe.addTextBody("cmd",      "Login");

  req.setEntity(mpe.build());

The answer previously accepted is unnecessarily complex and actually yields a x-www-form-urlencoded entity, not a multipart form as you intended.

Deltics
  • 22,162
  • 2
  • 42
  • 70