• HttpClient示例01


    1、要使用 HttpClient 需要下载 Apache的相关包

      我这里下载的是 httpcomponents-client-4.5.2-bin.zip、httpcomponents-client-4.5.2-src.zip

      下载地址:http://hc.apache.org/downloads.cgi

      1.1、如果只是 基本的使用的话,只需要这两个包:httpcore-4.4.4.jar、httpclient-4.5.2.jar

    2、示例代码 (来源于网络)

    package test;
    
    import org.apache.http.HttpResponse;
    import org.apache.http.HttpStatus;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;
    
    import net.sf.json.JSONObject;
    
    public class Ttest02
    {
        public static void main(String[] args) throws Exception
        {
            main_post(
                    "http://ajax.mianbao99.com/vod-showlist-id-8-order-time-c-3719-p-2.html",
                    null,
                    false);
        }
        
        @SuppressWarnings("deprecation")
        public static void main_post(String _strUrl, JSONObject _jsonParam, boolean _bNoNeedResponse) throws Exception
        {
            //String strUrl = "http://ajax.mianbao99.com/vod-showlist-id-8-order-time-c-3719-p-2.html";
            
            //post请求返回结果
            DefaultHttpClient httpClient = new DefaultHttpClient();
            JSONObject jsonResult = null;
            HttpPost method = new HttpPost(_strUrl);
            try {
                if (null != _jsonParam) {
                    //解决中文乱码问题
                    StringEntity entity = new StringEntity(_jsonParam.toString(), "utf-8");
                    entity.setContentEncoding("UTF-8");
                    entity.setContentType("application/json");
                    method.setEntity(entity);
                }
                HttpResponse result = httpClient.execute(method);
                //url = URLDecoder.decode(url, "UTF-8");
                /**请求发送成功,并得到响应**/
                if (result.getStatusLine().getStatusCode() == 200)
                {
                    String str = "";
                    try
                    {
                        /**读取服务器返回过来的json字符串数据**/
                        str = EntityUtils.toString(result.getEntity());
                        if (_bNoNeedResponse)
                            return;
                        System.out.println(str);
                        
                        /**把json字符串转换成json对象**/
                        jsonResult = JSONObject.fromObject(str);
                    } catch (Exception e) {
                        System.out.println("post请求提交失败:" + _strUrl+"
    	"+e.getMessage());
                    }
                }
            } catch (Exception e) {
                System.out.println("post请求提交失败:" + _strUrl+"
    	"+e.getMessage());
            }
        }
        
        @SuppressWarnings("deprecation")
        public static void main_get() throws Exception
        {
            String strUrl = "http://ajax.mianbao99.com/vod-showlist-id-8-order-time-c-3719-p-2.html";
            DefaultHttpClient client = new DefaultHttpClient();
            //发送get请求
            HttpGet request = new HttpGet(strUrl);
            HttpResponse response = client.execute(request);
    
            /**请求发送成功,并得到响应**/
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                /**读取服务器返回过来的json字符串数据**/
                String strResult = EntityUtils.toString(response.getEntity());
                System.out.println(strResult);
                /**把json字符串转换成json对象**/
                JSONObject jsonResult = JSONObject.fromObject(strResult);
                //url = URLDecoder.decode(url, "UTF-8");
            } else {
                System.out.println("get请求提交失败:" + strUrl);
            }
        }
    
    }

    3、

    4、

    5、

  • 相关阅读:
    string的erase函数和find、find_first_of函数
    strtok和strtok_r
    Linux添加硬盘 挂载硬盘(附 Linux磁盘挂载和mount共享 带图)
    linux下访问中文目录文件
    用yield写协程实现生产者消费者
    用进程池和线程池实现高并发服务器
    python自带线程池
    python自带进程池
    模拟线程池代码
    面向对象的多次调用线程(含参版)
  • 原文地址:https://www.cnblogs.com/javaskill/p/5977291.html
Copyright © 2020-2023  润新知