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+"\n\t"+e.getMessage());
}
}
} catch (Exception e) {
System.out.println("post请求提交失败:" + _strUrl+"\n\t"+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、

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. pip安装Scrapy框架报错

    安装: pip3 install scrapy==1.1.0rc3 一..解决scrapy安装错误: 二.具体操作: 1.在http://landinghub.visualstudio.com/vis ...

  2. 密码验证连续多位相同或者顺序字符引发的思考.md

    目录 密码验证连续多位相同或者顺序字符引发的思考 需求 思考 实现 总结 参考 密码验证连续多位相同或者顺序字符引发的思考 需求 虽然用户对于这种复杂的密码验证恨之入骨,但是有时出于安全的考虑,我们系 ...

  3. SQSERVER--函数、开窗函数,-特殊的内容 (for xml path )

    1.STUFF SQL Server之深入理解STUFF sql stuff函数用于删除指定长度的字符,并可以在制定的起点处插入另一组字符.sql stuff函数中如果开始位置或长度值是负数,或者如果 ...

  4. phpstrrchr()函数的问题

    strrchr — 查找指定字符在字符串中的最后一次出现 说明 string strrchr ( string $haystack , mixed $needle ) 该函数返回 haystack 字 ...

  5. ArcPy开发IDE

    最近时不时的使用Arcpy的脚本,这就不可避免的面临Python IDE的选择.可以用来Python开发的IDE非常的多,像Eclipse等等.尝试了一些IDE后,逐渐的固定使用下面两款. 一.Pyt ...

  6. Visualizing mathematical functions by generating custom meshes using FireMonkey(很美)

    Abstract: This article discusses how you can generate your own 3-dimensional mesh for visualizing ma ...

  7. HTML里引号的输出与显示

    关于输入框中显示双引号和单引号 前台显示解决办法: 方法一: 单引号<input type="text" value="'"> 双引号<inp ...

  8. 访问HDFS报错:org.apache.hadoop.security.AccessControlException: Permission denied

    import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apac ...

  9. PHP获取与操作php.ini文件的几个函数示例

    php有一套设置和获取配置信息的函数,用于设置与修改相关参数信息.   1.ini_get()获取配置参数,ini_set()设置配置参数 <?php 2.ini_get_all()获取所有配置 ...

  10. nginx灰度环境

    1.nginx.conf split_clients "${remote_addr}AAA" $request_type { 25% "abtest"; * & ...