Android HTTP POST request sent from application but server “sees” it as a GET request

蹲街弑〆低调 提交于 2019-12-25 02:44:19

问题


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.

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.

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

The check Toast, displays the value POST, which is correct.

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"

Edited severname / Ip's out of the log and code.

ANy ideas?


回答1:


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


来源:https://stackoverflow.com/questions/20470775/android-http-post-request-sent-from-application-but-server-sees-it-as-a-get-re

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!