Http编程(二)使用Apache 的API实现
要下载jar包 import java.io.FileOutputStream;
import java.io.IOException; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils; /*
* 模拟下载
* 使用到的类:
* 1.HttpClient:
* 2.HttpResponse
* 3.HttpEntity
* 4.EntityUtils
*
*/
public class HttpDemo1 {
public static void main(String[] args) throws IOException {
// HttpClient:创建了客户端。
HttpClient client = new DefaultHttpClient();
// 请求 get:HttpGet
String path = "http://www.baidu.com/img/bdlogo.gif";
HttpGet get = new HttpGet(path);
// 让客户端执行请求。
HttpResponse response = client.execute(get);
// 数据全部在HttpResponse
// 1:响应码。
int code = response.getStatusLine().getStatusCode();
if(code == 200){
// 取出返回的数据。 数据封装到HttpEntity对象。
HttpEntity entity = response.getEntity();
// 如何获得HttpEntity对象中的数据。
byte[] b = EntityUtils.toByteArray(entity);
FileOutputStream fos = new FileOutputStream("e:\\bb.gif");
fos.write(b);
fos.flush();
fos.close();
}
}
}
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.FormBodyPart;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
/*
* 使用的类:
* HttpClient
* HttpPost
* FileBody
* FormBodyPart
* MultipartEntity
* HttpResponse
* EntityUtils
*
*/
public class HttpDemo5 {
public static void main(String[] args) throws ClientProtocolException, IOException {
//1:创建HttpClient 对象(创建客户端)
HttpClient client = new DefaultHttpClient();
//2:创建请求方式(web中的请求方式method)
String uri = "http://localhost:8080/FileUpload/FileUploadServlet";
HttpPost post = new HttpPost(uri);
//3:包装要发送的数据(文件)
//3.1:获取本地的文件
File file = new File("e:\\aa.jpg");
//3.2:创建FileBody对象(文件主体)
FileBody fileBody = new FileBody(file);
//3.3:创建FormBodyPart 对象(表单主体部分)
FormBodyPart part = new FormBodyPart("form", fileBody);
//4:创建MultipartEntity对象。MultipartEntity:多部件实体
MultipartEntity entity = new MultipartEntity();
//5:把表单主体部分添加到多部件实体中(将文件类型的数据添加到entity中)
entity.addPart(part);
//5:将普通文本数据添加到多部件实体中
entity.addPart("username", new StringBody("哈哈", "text/html", Charset.forName("utf-8")));
entity.addPart("password", new StringBody("123"));
//6:设置请求的实体
post.setEntity(entity);
//7:让客户端执行请求(带有数据的请求),得到的是HttpResponse对象(响应对象)
HttpResponse response = client.execute(post);
//8:通过响应对象获取响应码
int code = response.getStatusLine().getStatusCode();
//9:如果响应码为200(成功响应码),则获取服务器返回的数据
if(code == 200){
//9.1:获取HttpEntity对象(通过响应来获取)
HttpEntity entity2 = response.getEntity();
//10:使用EntityUtils工具类,将获取到的数据(实体)转换为字节数组形式,任何文件都可以以字节的形式保存
byte[] b = EntityUtils.toByteArray(entity2);
//输出内容
System.out.println(new String(b, "utf-8"));
}
}
}
Http编程(二)使用Apache 的API实现的更多相关文章
- Linux网络编程二、tcp连接API
一.服务端 1.创建套接字: int socket(int domain, int type, int protocol); domain:指定协议族,通常选用AF_INET. type:指定sock ...
- 【原创】高性能网络编程(二):上一个10年,著名的C10K并发连接问题
1.前言 对于高性能即时通讯技术(或者说互联网编程)比较关注的开发者,对C10K问题(即单机1万个并发连接问题)应该都有所了解."C10K"概念最早由Dan Kegel发布于其个人 ...
- Linux系统编程@多线程编程(二)
线程的操作 线程标识 线程的ID表示数据类型:pthread_t (内核中的实现是unsigned long/unsigned int/指向pthread结构的指针(不可移植)几种类型) 1.对两个线 ...
- java GUI编程二
java基础学习总结--GUI编程(二) 一.事件监听 测试代码一: 1 package cn.javastudy.summary; 2 3 import java.awt.*; 4 import j ...
- 微服务实战(二):使用API Gateway
微服务实战(一):微服务架构的优势与不足 微服务实战(二):使用API Gateway 微服务实战(三):深入微服务架构的进程间通信 微服务实战(四):服务发现的可行方案以及实践案例 微服务实践(五) ...
- 微服务实战(二):使用API Gateway - DockOne.io
原文:微服务实战(二):使用API Gateway - DockOne.io [编者的话]本系列的第一篇介绍了微服务架构模式.它讨论了采用微服务的优点和缺点,除了一些复杂的微服务,这种模式还是复杂应用 ...
- python异步IO编程(二)
python异步IO编程(二) 目录 开门见山 Async IO设计模式 事件循环 asyncio 中的其他顶层函数 开门见山 下面我们用两个简单的例子来让你对异步IO有所了解 import asyn ...
- Linux shell脚本编程(二)
Linux shell脚本编程(二) 练习:求100以内所有偶数之和; 使用至少三种方法实现; 示例1: #!/bin/bash # declare -i sum=0 #声明一个变量求和,初始值为0 ...
- (原创)LAMP搭建之二:apache配置文件详解(中英文对照版)
LAMP搭建之二:apache配置文件详解(中英文对照版) # This is the main Apache server configuration file. It contains the # ...
- Java并发编程二三事
Java并发编程二三事 转自我的Github 近日重新翻了一下<Java Concurrency in Practice>故以此文记之. 我觉得Java的并发可以从下面三个点去理解: * ...
随机推荐
- Tomcat 环境变量配置
1.变量和常量 i 和 0 2.环境变量 cmd >set 查看所有环境变量 %PATH% 系统指定可执行文件的搜索路径,可以是 .exe .bat String path=“C:\WINDOW ...
- SharpMap源代码解析
1. 简介 SharpMap是基于.net2.0的GIS系统.支持多种.NET开发语言(C# C++ 等).使用属性数据作为注记.符合OpenGIS的简单要素规范(OpenGIS Simple Fea ...
- c# richTextBox判断是否为图片文件
//图片 if (richText.Rtf.IndexOf(@"{\pict\") > -1)//条件成立为图片(richText为一个richTextBox的实例名称)
- What if you are involved in an automobile accident in the US
What if you are involved in an automobile accident in the US With increasing Chinese tourists and vi ...
- L-BFGS
L-BFGS算法比较适合在大规模的数值计算中,具备牛顿法收敛速度快的特点,但不需要牛顿法那样存储Hesse矩阵,因此节省了大量的空间以及计算资源.本文主要通过对于无约束最优化问题的一些常用算法总结,一 ...
- SQL Server Extended Events 进阶 2:使用UI创建基本的事件会话
第一阶中我们描述了如何在Profiler中自定义一个Trace,并且让它运行在服务器端来创建一个Trace文件.然后我们通过Jonathan Kehayias的 sp_SQLskills_Conver ...
- NLayerAppV3--基础结构层(Cross-Cutting部分)
回顾:NLayerAppV3是一个使用.net 2.1实现的经典DDD的分层架构的项目. NLayerAppV3是在NLayerAppV2的基础上,使用.net core2.1进行重新构建的:它包含了 ...
- asp.net Ibatis.net 批量插入数据ORACLE
在开发中我们有时会遇到需要批量插入数据,最普通的就是每次 插入一条.但是当数据量大道一定的地步会很影响性能.下面例子示范了ibatis.net批量插入 ibatis.net 的XML文件里面使用ite ...
- 解决EF没有生成字段和表说明
找了很多资料,终于找到一篇真正能解决ef生成字段说明,注释的文章,收藏不了,于是转载 本文章为转载,原文地址 项目中使用了EF框架,使用的是Database-First方式,因为数据库已经存在,所以采 ...
- javascript 对象克隆
浅克隆 先看代码: /** * 浅克隆 克隆传入对象,只克隆一层 * @param {any} source */ function shallowClone(source) { var tiaget ...