首页 > 编程语言 > 详细

Java的HTTP通信

时间:2016-06-16 13:21:39      阅读:198      评论:0      收藏:0      [点我收藏+]

在Android中,HTTP通信可以用Volley,在Java中不能使用Volley,只能使用DefaultHttpClient,HttpPost和HttpResponse。

    /* 
     * 向服务器发送数据,并接受返回的数据
     */
    public static String send2Server(String url, List <NameValuePair> params){
        String res = null;
        
        // 建立HTTPPost连接
        HttpPost httpRequest = new HttpPost(url);
        try
        {
            // 发出HTTP Request
            httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
            // 取得HTTP Response
            HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
            res = EntityUtils.toString(httpResponse.getEntity());
        }
        catch (Exception e){
            e.printStackTrace();
        }
        
        return res;
    }

 

Java的HTTP通信

原文:http://www.cnblogs.com/mstk/p/5590643.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!