2012-11-18 98 views
0

好了,所以C#代碼的HTTP POST工程(該函數返回TRUE,意味着響應串「OK」,那就是:HTTP POST不工作(C#代碼的工作和Java代碼是不是)

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; 
    } 

而這裏的不工作(該函數返回FALSE爲相同的參數上面的代碼Java代碼,響應串是: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; 
    } 

爲什麼JAVA代碼不在C#代碼正在工作的同時工作?我錯過了上述要求中的任何內容?

+0

定義不工作?你會得到什麼異常或意外的服務器響應? – Perception

+0

返回的狀態碼是什麼(通過response.getStatusLine()方法獲得,如果這是您的示例中使用的Apache HttpResponse類)。 – Neeko

+0

@Perception我沒有收到任何異常,只是返回的字符串是空白的,而不是「OK」,因爲它應該是。 – idish

回答

1

什麼是返回的HTTP狀態碼?你可以通過response.getStatusLine().getStatusCode()方法獲得這個。這將幫助您指出可能的問題,就像請求甚至將其發送到服務器一樣。

否則,您在Java代碼中創建和發送HTTP請求的方式看起來正確且有效。

+0

再次感謝你,保持偉大的工作! – idish

+0

嗯,嗨Neeko,我很抱歉,但即使在修復後,它不工作。我現在獲得狀態碼:200,但響應字符串是空白的。並在C#代碼的作品..你可以請嘗試幫助我嗎? – idish

+0

response.getEntity()。getContentLength();在執行POST調用後返回? – Neeko