0

好的,所以c#代码的http帖子有效(函数返回TRUE,表示响应字符串是“ OK”,这里是:

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

这是不工作的 JAVA 代码(函数返回FALSE与上面代码相​​同的参数,响应字符串是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;
    }

为什么 C# 代码工作时 JAVA 代码不工作?我在上述请求中遗漏了什么吗?

4

1 回答 1

1

返回的 HTTP 状态码是什么?你可以通过response.getStatusLine().getStatusCode()方法获得这个。这将帮助您确定可能的问题,例如请求甚至发送到服务器。

否则,您在 Java 代码中创建和发送 HTTP 请求的方式看起来正确且有效。

于 2012-11-20T00:32:10.170 回答