好了,所以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#代码正在工作的同时工作?我错过了上述要求中的任何内容?
定义不工作?你会得到什么异常或意外的服务器响应? – Perception
返回的状态码是什么(通过response.getStatusLine()方法获得,如果这是您的示例中使用的Apache HttpResponse类)。 – Neeko
@Perception我没有收到任何异常,只是返回的字符串是空白的,而不是“OK”,因为它应该是。 – idish