将url传递给HttpPost和HttpGet

时间:2014-05-03 16:15:14

标签: android http http-post http-get

我在Android中使用DefaultHttpClient的http请求有以下代码,我注意到处理POST和GET请求之间的区别。例如,对于POST请求,首先将URL直接传递给HttpPost,然后完成编码。但是,使用GET请求,首先完成编码,然后传递带参数的url。这是必须遵循的某种规则,还是我可以为POST和GET进行类似的编码和url传递?提前谢谢。

        // check for request method
        if(method == "POST"){
            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            //encode the post data 
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        }else if(method == "GET"){
            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        } 

1 个答案:

答案 0 :(得分:0)

nope Sir先生不能这样做,按照你一直这样做的方式,我已经按照一些教程,所有这些都遵循标准,点击此链接将帮助你 What is the difference between a HTTP-Get and HTTP-POST and why is HTTP-POST weaker in terms of security查看Mike Weller的回答