0

Alright, so the http post of the c# code works(the function returns TRUE, means the response string is "OK", here it is:

public bool Rank(int rank)
    {
                    System.Text.UTF8Encoding encoding=new System.Text.UTF8Encoding();
        string postData = "";
        InvokeOnMainThread(delegate(){
        postData="pass=somePass&request=someRequest&access_key="+((FBTabBarController)TabBarController).AAMAccessKey+"&pid="+place_id+"&rank="+rank.ToString();
        });
byte[]  data = encoding.GetBytes(postData);

                HttpWebRequest myRequest =
            (HttpWebRequest)WebRequest.Create("someURL");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();
        HttpWebResponse hwr =(HttpWebResponse) myRequest.GetResponse();
        StreamReader reader = new StreamReader(hwr.GetResponseStream());
        string res = reader.ReadToEnd();
        if(res=="OK") 
            return true;}
        else if(res == "FAILED") return false;

        return false;
    }

And here's the JAVA code that isn't working(the function returns FALSE for the same parameters as the code above, the response string is: NULL :

   public boolean SubmitRank(String URL) 
    {
        HttpClient httpclient = new DefaultHttpClient();   
        HttpPost httppost = new HttpPost(URL); 
            // Add your data   

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5); 
            Log.d("MyTag","id: " + place_id + "rank: " + rank);
            nameValuePairs.add(new BasicNameValuePair("pass","somePass"));
            nameValuePairs.add(new BasicNameValuePair("request","someRequest"));
            nameValuePairs.add(new BasicNameValuePair("accesskey",shareAppPreferences.getAccessKey()));
            nameValuePairs.add(new BasicNameValuePair("pid",place_id));
            nameValuePairs.add(new BasicNameValuePair("rank",rank));

            try {
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));
                    try {
                       HttpResponse response = httpclient.execute(httppost);
                        String resString = EntityUtils.toString(response.getEntity());

                        if(resString.equals("OK")){
                            return true;
                        }
                        else if(resString.equals("FAILED")){
                            return false;
                        }
                        return false;
                    } catch (ClientProtocolException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            return false;
    }

Why the JAVA code isn't working while the C# code is working? Am I missing anything in the above request?

idish
  • 3,190
  • 12
  • 53
  • 85
  • Define not working? What exception or unexpected server response are you getting? – Perception Nov 18 '12 at 15:37
  • What's the status code being returned (obtained through response.getStatusLine() method if this is the Apache HttpResponse class being used in your example). – Display name Nov 18 '12 at 15:37
  • @Perception I'm not receiving any exception, just the returned String is blank and not "OK" as it should be. – idish Nov 18 '12 at 15:38
  • @Neeko I cannot run the code ATM, I will be able to do so in some hours. As you see, the http requests are the same? – idish Nov 18 '12 at 15:40
  • What could be the problem here? – idish Nov 18 '12 at 15:43
  • @idish From what I can see, it looks okay. Knowing what the HTTP status code is will help pinpoint what the possible issue is (like did the request even make it to the server, or not). – Display name Nov 18 '12 at 15:50
  • @Neeko Yes, the status code was 417, after checking it, I fixed the issue setting the header 100continue to false, it works now. – idish Nov 19 '12 at 20:25
  • @Neeko You can post it as an answer so I can accept it, anyways, thank you very much for your help. – idish Nov 19 '12 at 20:26
  • @idish That would definitely do it, glad you got it figured out. I'll post an answer. – Display name Nov 20 '12 at 00:33

1 Answers1

1

What's the HTTP status code being returned? You can this obtained via response.getStatusLine().getStatusCode() method. This will help you pin point the possible issue, like is the request even making it to the server.

Otherwise, the way you're creating and sending your HTTP request in the Java code looks correct and valid.

Display name
  • 1,109
  • 1
  • 15
  • 31
  • Hmm, Hi Neeko, I'm sorry but even after that fix, it's not working. I gain now status code: 200 but the response string is blank. and in the C# code it works.. Can you please try helping me? – idish Nov 23 '12 at 23:20
  • What does response.getEntity().getContentLength(); return, after executing the POST call? – Display name Nov 24 '12 at 03:36
  • Then the server isn't returning anything so you'll need to debug through the server to find out why. Again, the Java code you posted looks good but verify you're including the proper header parameters in your request, as expected by the server. Also, and this may be fruitless since your server isn't returning anything, but try obtaining the entity body as described here http://stackoverflow.com/a/10684201/708969 – Display name Nov 24 '12 at 14:29
  • But that's too weird, why would the C# code work while the JAVA code doesn't, If I am sending exactly same parameters in both of the requests. I'm still trying fixing it. – idish Nov 24 '12 at 15:39
  • The only thing I notice about your C# code is that you include the content-length and content-type in your request, yet omit it in your Java code. However, I'd imagine you would get back a 411 status code from the server if it was required, but it may be worth trying to include it in your Java request. – Display name Nov 24 '12 at 15:43
  • Ahh what the hell.. this code works: http://pastebin.com/vpwGhAby Finally...You thing it was because of the headers I haven't specified? I still want to use httpclient from apache, how can I set the headers there? – idish Nov 24 '12 at 16:29
  • For Apache HttpPost, try `httppost.setHeader("Content-type", "application/x-www-form-urlencoded");` and `httppost.setHeader("Content-Length", Integer.toString(urlParameters.getBytes().length));`, etc. I'm taking an educated guess that this is for an Android application? If so, read http://android-developers.blogspot.com/2011/09/androids-http-clients.html and choose the HTTP client that's recommended for the Android version you're targeting. – Display name Nov 24 '12 at 16:41
  • the urlParameters in the httpclient are basically the namevaluepairs, how can I convert them into bytes? – idish Nov 24 '12 at 17:12
  • If I set the header of content length, I'm getting an exception saying that Content-Length header already present. Anyways, that's really weird. I really wonder what's the issue here. But, I guess I will just use the other way(not httpclient). Thank you so much for your help! – idish Nov 24 '12 at 17:37
  • Ah yes, I forgot that `HttpPost.setEntity()` will implicitly set the content-length for you, so setting it again will throw that exception. Out of curiosity, did you attempt to obtain the response string via the method shown at http://stackoverflow.com/a/10684201/708969 from `HttpEntity`? – Display name Nov 24 '12 at 17:42
  • Actually, probably not worth the bother. That's what `EntityUtils.toString(HttpEntity)` essentially does, and you've tried that method already. – Display name Nov 24 '12 at 17:52
  • Hahaha that's pretty funny and weird. – idish Nov 24 '12 at 18:49