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的并发可以从下面三个点去理解: * ...
随机推荐
- Python+Android开发
1 下载Scripting Layer for Android (SL4A) Scripting Layer for Android (SL4A) 是一个开源项目,目标是为android系统提供脚本语 ...
- 一致性哈希算法和Go语言实现
一致性哈希算法,当我第一次听到这个名字的时候,感觉特别高深.而它往往会和分布式系统相关,准确的说,是分布式缓存. 在Web服务中,缓存是介于数据库和服务端程序之间的一个东西.在网站的业务还不是很大的时 ...
- [leetcode] 15. Plus One
这道题其实让我意识到了我的英文水平还有待加强.... 题目如下: Given a non-negative number represented as an array of digits, plus ...
- H2内存数据库支持存储到文件
准备工作 1.下载JDK(本人下载的版本为JDK1.7).设置环境变量JAVA_HOME,设置PATH(%JAVA_HOME%\bin%). 2.下载并解压:h2-2014-07-13.zip 官网下 ...
- 万恶的KPI、新兴的OKR及让人纠结的程序员考核
最近两天在研究研发部门如何进行绩效管理(其实一直都在思考,关注,实践,总感觉无从下手,也想求助咨询公司,无奈囊中羞涩).查了两天的资料,主要的方向是KPI,OKR,谷歌等互联网公司的考核方法.这里做个 ...
- ArgumentException: 已添加了具有相同键的项。
此问题出现在asp.net mvc 5 中,前端向后端请求数据,方法的参数是模型,比如 Add(Student m), 结果浏览器显示的状态是500并返回错误提示ArgumentException,如 ...
- 记一次golang的实践
之前做过一个深交所股票数据的接存储软件,消息的协议是这样. 协议文档在这 https://wenku.baidu.com/view/d102cd0b4a73f242336c1eb91a37f111f ...
- Unity3d ugui 实现image代码换图
核心脚本代码 Image IMGE = transform.Find("IMGE").GetComponent<Image>();Sprite sprite1 = Re ...
- eclipse的使用和断点调试
断点: 快捷键 f5: step into f6: step over 跳过,跳到下一行 f7:step return 从某个方法里跳回 跳出 drop to frame: 跳到当前方法的第一行 re ...
- 这里给大家介绍一下通过 Wi-Fi 连接“慷慨捐赠”你的身份信息的七种方法.
这里给大家介绍一下通过 Wi-Fi 连接“慷慨捐赠”你的身份信息的七种方法和反制措施. 本文作者:黑子小盆友 1.利用免费热点 它们似乎无处不在,而且它们的数量会在接下来四年里增加三倍.但是它们当中很 ...