This question already has answers here:
Android HttpPost: how to get the result
(5 answers)
Closed 9 years ago.
I'm using
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
in order to send data to my webservice. How could I get the http response code (like 200, 404, 500, etc) ?
I'm not able to use getResponseCode() from HttpURLConnection
More of your code would be helpful here but the way I do it is this:
HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
HttpResponse response = client.execute(httppost);
int responseCode = response.getStatusLine().getStatusCode()
by
HttpResponse response = httpclient.execute(httppost);
StatusLine statusLine = response.getStatusLine();
int code = statusline.getStatusCode();
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
The app worked fine before adding a certificate from startSSL.com to my server.
The POST array is empty on the server. And I don't get any error, I get response from the server.
This is the code I use to make my server calls in AsyncTasks.
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
//String paramString = URLEncodedUtils.format(params, "utf-8");
// url += "?" + paramString;
for (NameValuePair nameValuePair : params) { // modificado para usar URIS
url += "/" + nameValuePair.getValue();
}
Log.d("Intentando conectar con el servidor: ", url);
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
Any idea?
It was a server isue. Removing the 'www' part from the url solved it. I configured my server with no www but I didn't imagine it was going to make me sweat this way.
Here is how I use POST and it works:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
And I didnt know how to use PUT, so I got this code and I changed every "Post" to "Put"
but I dont think it works:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPut httpPut = new HttpPut(url);
httpPut.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPut);
HttpEntity httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
When I try updating a user's account using this implementation ot PUT I get errors from the server. - "No session. Unauthorized."
When I use Chrome's postman with the same parameters, I get no problem, so I think my PUT implementation doesnt work.
You need to use the same DefaultHttpClient to make all the calls, because your session information is stored in the instance object.
If you need to use different instances, you may be able to do that getting the cookies from the login request and add them in the next requests using getCookieStore/setCookieStore.
This question already has an answer here:
How to set content-length in android?
(1 answer)
Closed 9 years ago.
I've set almost all my data :
String capcha = editText.getText().toString();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("myurl.asp");
// Add your data
String str = "param1=x¶m2=y";
StringEntity strEntity = new StringEntity(str);
httppost.setEntity(strEntity);
httppost.setHeader("Set-Cookie", sessionCookie);
httppost.setHeader("Accept", "text/html");
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
// httppost.setHeader("Content-Length", ); HOW TO GET CONTENT LENGTH ?
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
Thanks for your help.
Perhaps this may help to solve your problem:
httppost.setHeader("Content-Length", String.valueOf(strEntity.getContentLength()));
The problem is as following :
I use httpclient to post login data to server.
While using firefox on my desktop with live headers the server redirects me to url2 witk 302, then to url3 with 302 and then I get 200 ok. In android I got the same login page and 200 OK.
Then I disabled automatic redirect handling got the response and saw 302 redirect, BUT
"String location = response.getHeaders("Location")[0].toString();" gave me the same login page url and not the one from firefox.
I have no idea why is that.
Code:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(uri[0]);
HttpResponse response;
String responseString = null;
HttpParams params = httpclient.getParams();
HttpClientParams.setRedirecting(params, false);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// Adding namvalue pairs here - adding correct i'm sure
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else{
if (statusLine.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY)
{
String location = response.getHeaders("Location")[0].toString();
}
response.getEntity().getContent().close();
}
Firefox headers:
What i get is : location= /login/
Hi I am developing an android application, I like to create a class to get the HTTP status before send the data to the server with HTTP Post.
Have any form to get the HTTP status of this server?
I read to get the 200 code is the server is running and another code no
Thanks.
Resolved the timeout is very long, My solution is:
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 10000);
HttpClient httpclient = new DefaultHttpClient(params);
and then:
HttpGet httpRequest = new HttpGet(server);
HttpResponse response = httpclient.execute(httpRequest);
You could do the following
HttpGet httpRequest = new HttpGet(myUri);
HttpEntity httpEntity = null;
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httpRequest);
response.getStatusLine().getStatusCode()
This is how you get Response code if you are using HttpUrlConnection :
when server is not running
int status = ((HttpURLConnection) connection).getResponseCode();
Log.i("", "Status : " + status);
And here is if you are using HttpClient :
HttpResponse response = httpclient.execute(httppost);
Log.w("Response ","Status line : "+ response.getStatusLine().toString());