从应用程序发送的Android HTTP POST请求,但服务器将其视为GET请求

时间:2013-12-09 12:31:44

标签: android json post http-post

我正在向我的大学项目的服务器发送一些信息。我遇到的问题是服务器只会检测POST请求,它不会解析GET请求,这是公平的。

我遇到的问题是我发送了一个httpPost请求,我使用内置的Android方法检查这个(见下文),但当它到达服务器时,它将其视为GET请求。

邮政编码:

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

检查Toast,显示值POST,这是正确的。

服务器日志将此显示为GET请求。

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的日志和代码。

任何想法?

1 个答案:

答案 0 :(得分:1)

使用此方法我的朋友将数据发布到服务器

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