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+"\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的更多相关文章
- springmvc 项目完整示例01 需求与数据库表设计 简单的springmvc应用实例 web项目
一个简单的用户登录系统 用户有账号密码,登录ip,登录时间 打开登录页面,输入用户名密码 登录日志,可以记录登陆的时间,登陆的ip 成功登陆了的话,就更新用户的最后登入时间和ip,同时记录一条登录记录 ...
- HttpClient示例
<%@page import="com.sun.xml.ws.client.BindingProviderProperties"%> <%@page conten ...
- AI - TensorFlow - 示例01:基本分类
基本分类 基本分类(Basic classification):https://www.tensorflow.org/tutorials/keras/basic_classification Fash ...
- python网页爬虫开发之七-多线程爬虫示例01
from urllib.request import quote import urllib.request from bs4 import BeautifulSoup import re impor ...
- Linux shell 函数应用示例01
函数Function的使用 定义函数 (1) 函数名称() { ... ... } (2) function 函数名称{ ... ... } 调用函数 ...
- mysql——单表查询——其它整理示例01
create database see; use database see; drop database sww; ========================================== ...
- 关于 Task.Run 简单的示例
1. 关于 Task.Run 简单的示例01 直接贴代码了: public static class TaskDemo01 { public static void Run() { Console.W ...
- C# Httpclient编程
今天研究了一天C#如何添加cookie到httpcient里面,从而发请求时,能把cookie作为头部发出,最后发现根本加不进去. Httpclient的cookie是来自上一个请求的响应,httpc ...
- Tyrion中文文档(含示例源码)
Tyrion是一个基于Python实现的支持多个WEB框架的Form表单验证组件,其完美的支持Tornado.Django.Flask.Bottle Web框架.Tyrion主要有两大重要动能: 表单 ...
随机推荐
- HDU 3388 Coprime(容斥原理+二分)
Coprime Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- docker运行oracle11g
image docker pull registry.cn-hangzhou.aliyuncs.com/qida/oracle-xe-11g 或者自己自动添加表 create role test_ro ...
- Squid 缓存代理服务器的完整配置
Squid 缓存代理服务器 Squid 的作用 1.通过缓存的方式为用户提供web访问加速 2.对用户的web访问进行过滤控制 缓存代理服务器又分为普通代理服务器,透明代理服务器,和反向代理服务器. ...
- 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 ...
- PHP的几种排序方法
<?php /** * PHP最常用的四个排序方法及二种查找方法 * 下面的排序方法全部都通过测试 * auther : soulence * date : 2015/06/20 */ //PH ...
- Django生成CSV文件
1.生成CSV文件 有时候我们做的网站,需要将一些数据,生成有一个CSV文件给浏览器,并且是作为附件的形式下载下来.以下将讲解如何生成CSV文件. 2.生成小的CSV文件 这里将用一个生成小的CSV文 ...
- 我的Android进阶之旅------>解决:Failed to create 'build\outputs\apk\watch-debug-unaligned.apks': 拒绝访问。
1. 错误描述 今天用Android Studio进行项目编译的时候,报错如下所示: FAILURE: Build failed with an exception. * What went wron ...
- MySQL具体解释(20)-----------数据库备份和还原
数据备份: 使用mysqldump命令备份 mysqldump命令能够讲数据库中的数据备份成一个文本文件. 表结果和表中的数据将存储在生成的文本中.mysqldump的工作原理非常easy. 他先查出 ...
- weal woe
He is worth no weal that can bide no woe. 禁不起吃苦的人不配得到幸福 有句谚语叫No weal without woe 福兮祸所伏 ; 祸兮福所倚 weal和 ...
- python3条件表达式和字符串
1.布尔表达式 布尔表达式的值只有两个:真和假.在python中,真值为1,假值为0 2.逻辑操作符 三种逻辑操作:and.or.not 3.条件语句 if. if...else.if...elif. ...