Java HTTP请求

注意:java  http请求要放在 try catch里面,该过程是一个阻塞过程,所以需要新建一个线程进行处理

        try
        {
            HttpPost request = new HttpPost(URL);  
            StringEntity se = new StringEntity(param.toString(), HTTP.UTF_8);
            se.setContentType("application/json");
            request.setEntity(se);  
            HttpResponse httpResponse = new DefaultHttpClient().execute(request);  
            // 得到应答的字符串,这也是一个 JSON 格式保存的数据   
            String retSrc = EntityUtils.toString(httpResponse.getEntity());  
            retSrc = URLDecoder.decode(retSrc, "UTF-8");
}
        catch  (Exception e) 
        {
            e.printStackTrace();
        }

 

posted @ 2013-12-04 17:33  haroel  阅读(239)  评论(0编辑  收藏  举报