Http GET Request Android - android

I know that it's possible to put some data in GET request body, but I know that it's not a good practice.
Though I want to do it but i don't know how. I searched the web but couldn't find anything.
Can you give me some little code examples or reference me to some usefull info.
Let's say we have this code, how to add nameValuePairs to request body of GET request :
String url = "something";
HttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),
TIMEOUT_MS);
HttpConnectionParams.setSoTimeout(httpClient.getParams(), TIMEOUT_MS);
HttpGet httpGet = new HttpGet(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("dtype", "0"));
nameValuePairs.add(new BasicNameValuePair("category_id", ""));
HttpResponse response = httpClient.execute(httpGet);
System.out.println("log in");
Thanks in advance.

Related

Android cant execute POST request after GET

My problem is this: after the GET request authorization and save cookies trying to perform a POST request to add data, but the server responds with 500 code. What's funny, because if POST query string form in a browser, it is executed correctly. The code below.
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(site + "/admin/users/login_do/?login=admin&password=demo");
HttpResponse response = httpclient.execute(httpget);
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
HttpPost httppost = new HttpPost(site + "/admin/news/add/5/item/do/");
httppost.addHeader("Cookie", "PHPSESSID="+cookies.get(0).getValue()+"; umicms_session="+cookies.get(1).getValue()+"; stat_id="+cookies.get(2).getValue());
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("active", "1"));
nameValuePairs.add(new BasicNameValuePair("name", "test"));
nameValuePairs.add(new BasicNameValuePair("data[new][anons]", "anno"));
nameValuePairs.add(new BasicNameValuePair("data[new][content]", "cont"));
nameValuePairs.add(new BasicNameValuePair("data[new][publish_time]", "1420202020"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
response = httpclient.execute(httppost);
StatusLine status = response.getStatusLine();
Log.d("my",String.valueOf(status.getStatusCode()));
Log.d("my",String.valueOf(status.getReasonPhrase()));
I've tried to run POST using HttpURLConnection, but also received 500 response.
Who can tell what might be the problem?
Problem solved. I Forgot to send csrf token with POST request and set Referer header
nameValuePairs.add(new BasicNameValuePair("csrf", csrf));
httppost.addHeader("Referer", referlink);

posting the data using httpput in android

I have a problem in posting the data to server by using httpput methods in android.I have to send feedback to server and getting json response. but i am getting 404 bad request. but i dont know where is the problem.
I am strucked here and didn't find any solution. Any suggestions?
My code is as follows:
HttpClient client = new DefaultHttpClient();
HttpPut put = new HttpPut(getString(R.string.feedBack));
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("userId", "8"));
pairs.add(new BasicNameValuePair("feedback",feedbackMessage
.getText().toString()));
put.addHeader("Content-Type", "application/json");
put.addHeader("Accept", "application/json");
put.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = client.execute(put);
Log.d(tag, "Result" + response.getStatusLine());
You are not initializing a URL object and passing in simple String.
You should do this instead:
URL url = new URL(getString(R.string.feedBack));
HttpClient client = new DefaultHttpClient();
HttpPut put= new HttpPut(url);
And it should work.

HttpPost returns only part of response body

I'm performing login on server using HttpPost method.
CommonsConnection.initHttpConnection();
HttpResponse response;
String contentOfMyInputStream;
HttpGet initPostMethod = new HttpGet("https://stat.volia.com:8443/ktvinet/index.jsp");
response = CommonsConnection.hc.execute(initPostMethod);
HttpPost Statistics = new HttpPost("https://stat.volia.com:8443/ktvinet/j_security_check");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("j_username", "username"));
nameValuePairs.add(new BasicNameValuePair("j_password", "password"));
nameValuePairs.add(new BasicNameValuePair("submit", "%C2%EE%E9%F2%E8"));
Statistics.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = CommonsConnection.hc.execute(Statistics);
HttpGet mainPage = new HttpGet("https://stat.volia.com:8443/ktvinet/main.do");
response = CommonsConnection.hc.execute(mainPage);
contentOfMyInputStream = EntityUtils.toString(response.getEntity());
The response I get - body of webpage - is like 1/5 part of webpage, which ends with "...".
Am I missing some parameter for httpclient ?
How are you examining the response? if you're looking int he debugger or something similar, it may well truncate long strings, try dumping it in a file.
Logcat displays only part of long string response.

Loop HttpPost requests

I need to access data from a webpage using several different post requests. For now I use:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://myurl");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("action", "search"));
nameValuePairs.add(new BasicNameValuePair("ndc", ndc));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
I need to sent this request using different values for the variable ndc. Would looping this lines be a good idea? If so, how to reuse the HttpClient and HttpPost variables?
If the URL needs to stay the same, then you should only change the values that need to sent.
for (int i=0; i<ndcArray.length;i++)
{
if(i==theNumberWhenURLhasToBeChanged) //adjust this condition based on your knowledge when the url has to be changed, lets say if i > theNumberWhenURLhasToBeChanged, then change the url...
{
httppost = new HttpPost(URLs[theNumberWhenURLhasToBeChanged]);
}
nameValuePairs.add(new BasicNameValuePair("ndc", ndcArray[i]));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
}
Note that: response will change each time, so bear in mind that you should save the response somewhere. And ndcArray[] can be replaced with any structure you want.

Why is HttpPost using a GET method? [Android]

I am executing the following post in Android:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.example.com");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("account", "login"));
nameValuePairs.add(new BasicNameValuePair("email", "email#email.com));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
In the server side I return the cgi.request_method variable and it is GET.
Shouldn't it be POST? Am I missing something?
That code should initiate a POST, not a GET. I would guess that something on the server side is misconfigured.
My problem was send http instead of https

Categories

Resources