一.HttpClient介绍
1.简介:
HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源。虽然在 JDK 的 java net包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。HttpClient 已经应用在很多的项目中,比如 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTMLUnit 都使用了 HttpClient。
2.功能:
(1)实现了所有 HTTP 的方法(GET,POST,PUT,HEAD 等)
(2)支持自动转向
(3)支持 HTTPS 协议
(4)支持代理服务器等
二.HttpClient实现数据拷贝
1.依赖添加
这次新的依赖是HttpClient的
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
</dependencies>
2.HtttpClientUtils.java工具类
package nz.study.util;
import org.apache.http.HttpConnection;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpClientUtils {
/**
* 获取指定baseURL链接所返回的字符串数据
* @param baseURL
* @return
*/
public static String getDataFromURL(String baseURL){
try {
//根据URL创建一个URL对象
URL url = new URL(baseURL);
//通过url对象的openConnection()方法并强转的带一个HttpURLConnection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置请求超时时间
conn.setConnectTimeout(5000);
//设置请求方式
conn.setRequestMethod("GET");
//设置请求方式为输入请求
conn.setDoInput(true);
//得到请求返回的状态码
int code = conn.getResponseCode();
//如果状态码为HTTP_OK:200
if (code == HttpURLConnection.HTTP_OK){
//通过链接对象的getInputStream()方法得到一个输入流对象
InputStream is = conn.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte [] b = new byte[1024];
int len;
while ((len = is.read(b))!= -1){
baos.write(b,0,len);
}
return new String(baos.toByteArray());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 根据指定的baseURL来获取url对应的字节数组的值
* @param baseURL 要获取数据的URL
* @param type 请求类型 GET或者POST
* @return 指定URL所对应的字节数组值
*/
public static byte[] getByteDataByURL(String baseURL,String type){
try {
URL url = new URL(baseURL);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5000);
if("GET".equalsIgnoreCase(type)){
conn.setRequestMethod("GET");
}else{
conn.setRequestMethod("POST");
}
conn.setDoInput(true);
int code = conn.getResponseCode();
if (code == HttpURLConnection.HTTP_OK){
InputStream is = conn.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len;
byte[] b = new byte[1024];
while((len = is.read(b))!= -1){
baos.write(b,0,len);
}
return baos.toByteArray();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
3.TestHttpClientUtils.java测试类
package nz.study.test;
import nz.study.util.HttpClientUtils;
import org.junit.Test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestHttpClientUtils {
@Test
public void testGetData(){
String data = HttpClientUtils.getDataFromURL("https://www.baidu.com");
System.out.println(data);
}
@Test
public void testGetByte(){
byte[] datas = HttpClientUtils.getByteDataByURL("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4","GET");
FileOutputStream fos = null;
try {
fos = new FileOutputStream("rabbit.mp4");
fos.write(datas);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try{
if(fos != null){
fos.close();
fos = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("download success");
}
}
今天是我在千锋线上学习的第59天,加油!!!