通过httpClient提交POST数据

转载自:传智播客教程

//SSL HTTPS Cookie
    public static boolean sendRequestFromHttpClient(String path, Map<String, String> params, String enc) throws Exception{
        List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
        if(params!=null && !params.isEmpty()){
            for(Map.Entry<String, String> entry : params.entrySet()){
                paramPairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
        }
        UrlEncodedFormEntity entitydata = new UrlEncodedFormEntity(paramPairs, enc);//得到经过编码过后的实体数据
        HttpPost post = new HttpPost(path); //form
        post.setEntity(entitydata);
        DefaultHttpClient client = new DefaultHttpClient(); //浏览器
        HttpResponse response = client.execute(post);//执行请求
        if(response.getStatusLine().getStatusCode()==200){
            return true;
        }
        return false;
    }

 调用示例:

public void testSendRequestFromHttpClient() throws Throwable{
        Map<String, String> params = new HashMap<String, String>();
        params.put("method", "save");
        params.put("title", "传智播客");
        params.put("timelength", "80");
        
        boolean result = HttpRequest.sendRequestFromHttpClient("http://10.10.97.51:8180/videoweb/video/manage.do", params, "UTF-8");
        Log.i("HttRequestTest", ""+ result);
    }

结果:打印值为"true"

 

posted @ 2016-04-14 11:08  进进  阅读(204)  评论(0)    收藏  举报