0

I want to send my id & password to server and get the response from server. Here is my code. It is not working for the first time. But iam getting the response from server if i execute my application on second time. It is throwing "Post method failed: -1 null" on first time. Where iam wrong?? Why if() block is executing on first time?? could you please tell me.

HttpsURLConnection con = null;
String httpsURL = "https://www.abc.com/login";
String query = "id=xyz&password=pqr";
URL url = new URL(httpsURL); 
con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-length", String.valueOf(query.length()));
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
con.setRequestProperty("User-Agent","Mozilla/4.0(compatible; MSIE 5.0; Windows 98; DigExt)"); 
con.setDoInput(true);
con.setDoOutput(true);  
DataOutputStream output = new DataOutputStream(con.getOutputStream());
output.writeBytes(query);
output.close();  
int respCode = con.getResponseCode();

if (respCode != HttpsURLConnection.HTTP_OK) 
{
  throw new Exception("POST method failed: " + con.getResponseCode()+ "\t" + con.getResponseMessage()); }
 else {
//read the content from server
}
Santhosh
  • 4,956
  • 12
  • 62
  • 90
  • from java doc ...getResponseCode Returns -1 if no code can be discerned from the response (i.e., the response is not valid HTTP). Did you try your server site with other technology and why not with a classic java program ? – Emmanuel Devaux Aug 18 '11 at 15:41

3 Answers3

1

1/ It is recommanded to use apache HttpClient rather than URLConnection (see http://developer.android.com/reference/org/apache/http/impl/client/DefaultHttpClient.html)

2/ for login and password, why not use Http Authentication ? both basic and digest are supported by android.

3/ as for you problem, you don't close the underlying outputStream.

you should do:

OutputStream os = con.getOutputStream();
DataOutputStream output = new DataOutputStream(os);
output.writeBytes(query);
output.close();
os.close();
njzk2
  • 38,969
  • 7
  • 69
  • 107
  • it may indeed. It seems that a FilterOutputStream does close the underlying stream after all. – njzk2 Aug 18 '11 at 16:05
0

Check Server service validity with other technology and/or classic java. You didn say in your question if you succeed to discriminate the server from the issue.

from java doc ...getResponseCode returns -1 if no code can be discerned from the response (i.e., the response is not valid HTTP).

Java https post request example : http://www.java-samples.com/java/POST-toHTTPS-url-free-java-sample-program.htm

try to close your outputstream after querying the status and not before...that may help

Emmanuel Devaux
  • 3,327
  • 4
  • 25
  • 30
0

Here is how you should send POST requests in Android

HttpPost httpGet = new HttpPost(server + "/login?email="+username+"&password="+password);
DefaultHttpClient httpClient = new DefaultHttpClient();         
HttpResponse response = httpClient.execute(httpGet);

You can read response using:

response.getEntity().getContent()
bitle
  • 415
  • 2
  • 5
  • 2
    not really. you are not posting anything, here, and are sending the request in the url. which would be get. – njzk2 Aug 18 '11 at 16:05
  • May be I'm wrong but code like "HttpPost httpGet = new HttpPost" is quite a strange coding ... a "Get" is a "Post" ? May be a too fast copy/Paste :) – Emmanuel Devaux Aug 18 '11 at 16:16
  • Yes, httpGet is a typo, it used to be a GET request in my code long ago. – bitle Aug 18 '11 at 16:47
  • Here is where I got it from originally: [stackoverflow](http://stackoverflow.com/questions/4272905/httppost-not-working/4273540#4273540) – bitle Aug 18 '11 at 16:48