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、

  • 相关阅读:
    投票协议:二进制表示方法
    投票协议:构建和解析协议消息
    多任务处理:多线程
    通信:成帧与解析
    通信:组合输入输出流
    跨越FSO WSH写文件
    MS07004漏洞
    解决ASP(图像)上传漏洞的方法
    让硬件入侵不再是神话
    解决ASP(图像)上传漏洞的方法
  • 原文地址:https://www.cnblogs.com/javaskill/p/5977291.html
Copyright © 2011-2022 走看看