zoukankan      html  css  js  c++  java
  • 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、

  • 相关阅读:
    初识软件工程
    00.JS前言
    01.JS语法规范、变量与常量
    02.JS数据类型与数据类型转换
    03.JS运算符
    04.JS逻辑结构
    05.JS函数
    06.JS对象-1
    08.JS单词整理
    00.ES6简介
  • 原文地址:https://www.cnblogs.com/javaskill/p/5977291.html
Copyright © 2011-2022 走看看