Adding parameter to HttpPost on Apache's httpclient - java

I am trying to set some Http parameters in the HttpPost object.
HttpPost post=new HttpPost(url);
HttpParams params=new BasicHttpParams();
params.setParameter("param", "value");
post.setParams(params);
HttpResponse response = client.execute(post);
It looks like the parameter is not set at all. Do you have any idea why this is happening?
Thank you

For those who hopes to find the answer using HttpGet, here's one (from https://stackoverflow.com/a/4660576/330867) :
StringBuilder requestUrl = new StringBuilder("your_url");
String querystring = URLEncodedUtils.format(params, "utf-8");
requestUrl.append("?");
requestUrl.append(querystring);
HttpClient httpclient = new DefaultHttpClient();
HttpGet get = new HttpGet(requestUrl.toString());
NOTE: This doesn't take in consideration the state of your_url : if there is already some parameters, if it already contains a "?", etc. I assume you know how to code/search and will adapt regarding your case.

HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("param", "value"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
httpClient.execute(httpPost);

Related

How do contents in name value pair pass as arguments in Android Studio?

Suppose i have this
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("pin", "date"));
And When i use the following code
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://X.X.X.X/abcdef.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
what will be the post request? How will the arguments pass in the url?
found the answer
can refer this link for more details
arguments are passed as this
http://localhost/xyz.php?pin=date&package=123&requirements=xxx&last_date=25%20april

How to make Post Request using Smali code?

Need to make simple POST request with one parameter using org.apache.http.HttpRequest
in Smali code, or something like translate Java to Smali?
Code like that, but in Smali.
HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://www.mywebsite.com");
List<NameValuePair> params = new ArrayList<NameValuePair>(1);
params.add(new BasicNameValuePair("param-1", "12345"));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = httpclient.execute(httppost);

Remove or replace special character in namevaluepair for http post request android

I have a problem when I tried to post my data it logged as :
[ObserverTRID=5QEET3TE10,
ObsType=Evaluate,
ObsDate=17-Jul-2014,
ObsTime=09:22:12,
// == loc
ObsTitle=bolllloooo,
//==obstset
MediaData={"MediaData":[{"RunNo":0,"Filename":"http://url/5QEET3TE10_3_5_20140717092206.jpg","MediaNo":4"Desc":01 Image_17-Jul-14No Marker.jpg}]},
PVPMediaCount=1,
SourceDevice=motorola XT1032 Android v19 App v2.0001,
ObsDept,
ObsSite,
TargetTRID=5QEET3TE10]
But I need to append every namevalue with "&" instead of "," This is my httppost request.
httppost.setEntity(new UrlEncodedFormEntity(mNameValuePairs));
This is my request i am posting the URL with the parameter nameevaluepair
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(mUrl);
if(mNameValuePairs==null){
mNameValuePairs = new ArrayList<NameValuePair>(1);
mNameValuePairs.add(new BasicNameValuePair("PostEvent", "Null"));
}
Log.e("namevalue ", mNameValuePairs.toString());
There I am settig entity by URLEncoding
httppost.setEntity(new UrlEncodedFormEntity(mNameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
String result = EntityUtils.toString(response.getEntity(),HTTP.UTF_8);
HttpEntity httpEntity = response.getEntity();
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
use this code in for parsing class

Post request to server. Different responses with different defaultClients

When i try to send post request using Java (JVM on Mac) with correct credentials I got correct response code 302 FOUND.
But when using the same code on the android, with SAME correct params, i got response CODE 200 OK.
Code to send post request:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(POST_URL);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("auth", "login"));
nvps.add(new BasicNameValuePair("login", login));
nvps.add(new BasicNameValuePair("password", password));
nvps.add(new BasicNameValuePair("set_cookie", "on"));
httpost.setEntity(new UrlEncodedFormEntity(nvps));
HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
302 FOUND is the common way of doing a "redirection", it seems Android or MacJVM is not performing that redirection. Are you using the same SDK version?
Try to use :
final HttpParams params = new BasicHttpParams();
HttpClientParams.setRedirecting(params, false);

HttpPost - type of http request method

Hi :) I got a simple problem, but very annoying.
I'm trying to send http post request using HttpPost class
This is part of the method which returns InputStream:
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url+path);
ArrayList<NameValuePair> paramsList = new ArrayList<NameValuePair>();
paramsList.add(new BasicNameValuePair("key1", value1));
paramsList.add(new BasicNameValuePair("key2", value2));
paramsList.add(new BasicNameValuePair("key3", value3));
httpPost.setEntity(new UrlEncodedFormEntity(paramsList));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
return is;
But the problem is on the server, because server "thinks" that im sending GET request instead of POST. The question is: where is mistake? Im using similar part of code in other application and it works fine.
Cheers. :)
Please try
httpPost.setEntity(new UrlEncodedFormEntity(paramsList, HTTP.UTF_8));
Instead Of
httpPost.setEntity(new UrlEncodedFormEntity(paramsList));

Categories