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、示例代码 (来源于网络)

  1. package test;
  2.  
  3. import org.apache.http.HttpResponse;
  4. import org.apache.http.HttpStatus;
  5. import org.apache.http.client.methods.HttpGet;
  6. import org.apache.http.client.methods.HttpPost;
  7. import org.apache.http.entity.StringEntity;
  8. import org.apache.http.impl.client.DefaultHttpClient;
  9. import org.apache.http.util.EntityUtils;
  10.  
  11. import net.sf.json.JSONObject;
  12.  
  13. public class Ttest02
  14. {
  15. public static void main(String[] args) throws Exception
  16. {
  17. main_post(
  18. "http://ajax.mianbao99.com/vod-showlist-id-8-order-time-c-3719-p-2.html",
  19. null,
  20. false);
  21. }
  22.  
  23. @SuppressWarnings("deprecation")
  24. public static void main_post(String _strUrl, JSONObject _jsonParam, boolean _bNoNeedResponse) throws Exception
  25. {
  26. //String strUrl = "http://ajax.mianbao99.com/vod-showlist-id-8-order-time-c-3719-p-2.html";
  27.  
  28. //post请求返回结果
  29. DefaultHttpClient httpClient = new DefaultHttpClient();
  30. JSONObject jsonResult = null;
  31. HttpPost method = new HttpPost(_strUrl);
  32. try {
  33. if (null != _jsonParam) {
  34. //解决中文乱码问题
  35. StringEntity entity = new StringEntity(_jsonParam.toString(), "utf-8");
  36. entity.setContentEncoding("UTF-8");
  37. entity.setContentType("application/json");
  38. method.setEntity(entity);
  39. }
  40. HttpResponse result = httpClient.execute(method);
  41. //url = URLDecoder.decode(url, "UTF-8");
  42. /**请求发送成功,并得到响应**/
  43. if (result.getStatusLine().getStatusCode() == 200)
  44. {
  45. String str = "";
  46. try
  47. {
  48. /**读取服务器返回过来的json字符串数据**/
  49. str = EntityUtils.toString(result.getEntity());
  50. if (_bNoNeedResponse)
  51. return;
  52. System.out.println(str);
  53.  
  54. /**把json字符串转换成json对象**/
  55. jsonResult = JSONObject.fromObject(str);
  56. } catch (Exception e) {
  57. System.out.println("post请求提交失败:" + _strUrl+"\n\t"+e.getMessage());
  58. }
  59. }
  60. } catch (Exception e) {
  61. System.out.println("post请求提交失败:" + _strUrl+"\n\t"+e.getMessage());
  62. }
  63. }
  64.  
  65. @SuppressWarnings("deprecation")
  66. public static void main_get() throws Exception
  67. {
  68. String strUrl = "http://ajax.mianbao99.com/vod-showlist-id-8-order-time-c-3719-p-2.html";
  69. DefaultHttpClient client = new DefaultHttpClient();
  70. //发送get请求
  71. HttpGet request = new HttpGet(strUrl);
  72. HttpResponse response = client.execute(request);
  73.  
  74. /**请求发送成功,并得到响应**/
  75. if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  76. /**读取服务器返回过来的json字符串数据**/
  77. String strResult = EntityUtils.toString(response.getEntity());
  78. System.out.println(strResult);
  79. /**把json字符串转换成json对象**/
  80. JSONObject jsonResult = JSONObject.fromObject(strResult);
  81. //url = URLDecoder.decode(url, "UTF-8");
  82. } else {
  83. System.out.println("get请求提交失败:" + strUrl);
  84. }
  85. }
  86.  
  87. }

3、

4、

5、

HttpClient示例01的更多相关文章

  1. springmvc 项目完整示例01 需求与数据库表设计 简单的springmvc应用实例 web项目

    一个简单的用户登录系统 用户有账号密码,登录ip,登录时间 打开登录页面,输入用户名密码 登录日志,可以记录登陆的时间,登陆的ip 成功登陆了的话,就更新用户的最后登入时间和ip,同时记录一条登录记录 ...

  2. HttpClient示例

    <%@page import="com.sun.xml.ws.client.BindingProviderProperties"%> <%@page conten ...

  3. AI - TensorFlow - 示例01:基本分类

    基本分类 基本分类(Basic classification):https://www.tensorflow.org/tutorials/keras/basic_classification Fash ...

  4. python网页爬虫开发之七-多线程爬虫示例01

    from urllib.request import quote import urllib.request from bs4 import BeautifulSoup import re impor ...

  5. Linux shell 函数应用示例01

    函数Function的使用 定义函数 (1) 函数名称() {     ...     ... } (2) function 函数名称{     ...     ... } 调用函数         ...

  6. mysql——单表查询——其它整理示例01

    create database see; use database see; drop database sww; ========================================== ...

  7. 关于 Task.Run 简单的示例

    1. 关于 Task.Run 简单的示例01 直接贴代码了: public static class TaskDemo01 { public static void Run() { Console.W ...

  8. C# Httpclient编程

    今天研究了一天C#如何添加cookie到httpcient里面,从而发请求时,能把cookie作为头部发出,最后发现根本加不进去. Httpclient的cookie是来自上一个请求的响应,httpc ...

  9. Tyrion中文文档(含示例源码)

    Tyrion是一个基于Python实现的支持多个WEB框架的Form表单验证组件,其完美的支持Tornado.Django.Flask.Bottle Web框架.Tyrion主要有两大重要动能: 表单 ...

随机推荐

  1. 巨蟒python全栈开发-第22天 内置常用模块1

    一.今日主要内容 1.简单了解模块 你写的每一个py文件都是一个模块 数据结构(队列,栈(重点)) 还有一些我们一直在使用的模块 buildins 内置模块.print,input random 主要 ...

  2. http://element.eleme.io/#/zh-CN/component/quickstart

    http://element.eleme.io/#/zh-CN/component/quickstart

  3. https://blog.newrelic.com/2014/05/02/25-php-developers-follow-online/

    w https://blog.newrelic.com/2014/05/02/25-php-developers-follow-online/ 1. Rob Allen. Zend Framework ...

  4. Json对象与Json字符串的转化

    1.jQuery插件支持的转换方式: $.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象 2.浏览器支持的转 ...

  5. Leetcode 之 Keys Keyboard

    1. 2 Keys Keyboard 先把dp的最小不走都设置为无穷大(Integer.MAX_VALUE),初始化条件:dp[0] = dp[1] = 0,状态转移方程为dp[i] = Math.m ...

  6. 【Spring MVC】spring mvc中相同的url请求返回不同的结果

    在项目中凡是使用Spring MVC这种控制器的,大多都是返回JSON数据对象,或者JSP页面. 但是相同的URL请求如何让他自动的选择放回的是什么? 在这里有由于鄙人没有亲自测试过,就不敢乱贴代码, ...

  7. C# 调用win api获取chrome浏览器中地址

    //FindWindow 查找窗口 //FindWindowEx查找子窗口 //EnumWindows列举屏幕上的所有顶层窗口,如果回调函数成功则返回非零,失败则返回零 //GetWindowText ...

  8. window7+wamp环境配置Oracle数据库连接

    最近开发需要使用的oracle数据库!翻看了PHP手册,也在网上找了些帖子!功夫不负有心人,花费了四五个小时的时间,终于找到了Oracle的配置方法.下面就讲解下如何配置Oracle数据库连接吧! 附 ...

  9. likely(x)与unlikely(x) __builtin_expect

    本文讲的likely()和unlikely()两个宏,在linux内核代码和一些应用中可常见到它们的身影.实质上,这两个宏是关于GCC编译器内置宏__builtin_expect的使用. 顾名思义,l ...

  10. ImageMagick来处理图片,缩放,调整高度等操作

    单个缩放图片 convert 911.jpg -resize 25% 911.jpg 前面是要处理的图片路径,后面是输出的图片路径,我这么写就把原先图片缩放了 批量缩放图片 mogrify -samp ...