问题描述
我发送一些信息到服务器进行我的一个大学项目。我遇到的问题是,断绝只会acpect POST请求,也不会解析GET这是很公平的要求。
I am sending some information to a sever for a university project of mine. The problem i am having is that the sever will only acpect POST request, it will not parse GET requests which is fair enough.
我遇到的问题是,我发送一个请求,httpPost我检查这个使用内置在Android中的方法(见下文),但是,当它到达服务器时,它认为这是一个GET请求。
The issue i am having is that i am sending a httpPost request i check this using the built in Android method (see below) but when it arrives at the server it sees it as a GET request.
邮政code:
JSONObject auth = new JSONObject();
auth.put("TEST", "TESTING");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://xxxxxxxxxxxxx/upload.php");
String meth = httpPost.getMethod();
Toast checker = Toast.makeText(this, meth, Toast.LENGTH_LONG);
checker.show();
String json = "";
json = auth.toString();
StringEntity se = new StringEntity(json);
httpPost.setHeader("User-Agent", "android app");
httpPost.setEntity(se);
httpclient.execute(httpPost);
支票敬酒,显示值POST,这是正确的。
The check Toast, displays the value POST, which is correct.
断绝日志显示这是一个GET请求。
The sever log shows this as a GET request.
xxxxxxxxxxx MY IP - - [09/Dec/2013:00:20:57 +0000] "GET /upload.php HTTP/1.1" 405 352 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0"
编辑severname / IP的出日志和code的。
Edited severname / Ip's out of the log and code.
什么想法?
推荐答案
使用这种方法我的朋友张贴数据到服务器
Use this method my friend for posting data to server
public Boolean postDataWithURL(String url, String fileUrl,
ArrayList<NameValuePair> listParamsWithValues) {
boolean result = false;
try {
int TIMEOUT_MILLISEC = 100000; // = 10 seconds
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(httpParams);
HttpPost request = new HttpPost(url);
if (fileUrl.equalsIgnoreCase("")) {
request.setEntity(new UrlEncodedFormEntity(listParamsWithValues));
} else {
// System.out.println("file path "+fileUrl+" with actual path "+file);
}
// request.setEntity(new
// ByteArrayEntity(listParamsWithValues.toString().getBytes("UTF8")));
HttpResponse response = client.execute(request);
String responseString = request(response);
// System.out.println(responseString);
if (responseString.toLowerCase().contains("1")) {
result = true;
} else {
result = false;
}
} catch (Exception e) {
// Some things goes Wrong
e.printStackTrace();
}
return result;
}
这篇关于从应用程序,但服务器&QUOT Android版发送HTTP POST请求,认为&QUOT;它作为一个GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!