HttpClient 是Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的用户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。
· HttpClient不是浏览器,它是一个用户端 http协议传输类库。
· HttpClient被用来发送和接受Http消息。
· HttpClient会解决 http消息的内容,不会进行 javascript解析,不会关心content type,假如没有明确设置,httpclient也不会对请求进行格式化、重定向 url,或者者 其余任何和
· http 消息传输相关的功能。
· 实现了所有 HTTP 的方法(GET,POST,PUT,HEAD 等)
· 支持自动转向
· 支持 HTTPS 协议
· 支持代理商服务器等
引入依赖
org.apache.httpcomponents
httpcore
4.4.6
org.apache.httpcomponents
httpclient
4.5.3
简单的get和post请求
public static void main(String[] args) throws Exception{
try {
//此为百度一篇文章
String url = "https://mbd.baidu.com/newspage/data/landingsuper?context=%7B%22nid%22%3A%22news_15984470154821203735%22%7D&n_type=0&p_from=1";
// 使用默认配置创立httpclient的实例
CloseableHttpClient client = HttpClients.createDefault();
// HttpPost post = new HttpPost(url);
HttpGet get = new HttpGet(url);
// CloseableHttpResponse response = client.execute(post);
CloseableHttpResponse response = client.execute(get);
// 服务器返回码
System.out.println(response.getStatusLine().toString());
System.out.println(response.getStatusLine().getStatusCode());
System.out.println(response.getStatusLine().getReasonPhrase());
// 服务器返回内容
String respStr = null;
HttpEntity entity = response.getEntity();
if(entity != null) {
respStr = EntityUtils.toString(entity, "UTF-8");
}
System.out.println("respStr = " + respStr);
// 释放资源
EntityUtils.consume(entity);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
String url = "http://localhost:9090"; // 使用默认配置创立httpclient的实例 CloseableHttpClient client = HttpClients.createDefault(); HttpPost post = new HttpPost(url); /** * 设置参数,常用的有StringEntity,UrlEncodedFormEntity,MultipartEntity * 具体看org.apache.http.entity包 */
Listparams = new ArrayList<>();
params.add(new BasicNameValuePair("username", "张三"));
params.add(new BasicNameValuePair("password", "123456"));
UrlEncodedFormEntity e = new UrlEncodedFormEntity(params, "UTF-8");
post.setEntity(e);
CloseableHttpResponse response = client.execute(post); // 服务器返回码
int status_code = response.getStatusLine().getStatusCode(); System.out.println(status_code); // 服务器返回内容
String respStr = null;
HttpEntity entity = response.getEntity();
if(entity != null) {
respStr = EntityUtils.toString(entity, "UTF-8");
}
System.out.println("respStr = " + respStr); // 释放资源
EntityUtils.consume(entity);
} catch (Exception e) {
e.printStackTrace();
}
}
int StatusCode = httpResponse.getStatusLine().getStatusCode();
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
System.out.println("长度:\t" + httpEntity.getContentLength());
System.out.println("内容:\t" + EntityUtils.toString(httpEntity,"UTF-8"));
}
String uploadUrl = "http://localhost:9102/upload.do";
HttpPost httpPost = new HttpPost(uploadUrl);
FileBody fileBody = new FileBody(new File("C:/Users/Administrator/Desktop/timg.jpg")); MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); multipartEntityBuilder.addPart("file",fileBody);
// 设置其余参数
Listnvps = new ArrayList ();
nvps.add(new NameValuePair("Accept","application/json, text/plain, */*")); nvps.add(new NameValuePair("Accept-Encoding","gzip, deflate, br"));
nvps.add(new NameValuePair("Accept-Language","zh-CN,zh;q=0.9"));
nvps.add(new NameValuePair("Connection","keep-alive"));
nvps.add(new NameValuePair("Content-Length","28700"));
nvps.add(new NameValuePair("Content-Type","multipart/form-data; boundary=----WebKitFormBoundarypaEfQmIQBbUrkI0c"));
nvps.add(new NameValuePair("Host","localhost:9102"));
nvps.add(new NameValuePair("Origin","http://localhost:9102"));
nvps.add(new NameValuePair("Referer","http://localhost:9102/admin/goods_edit.html")); nvps.add(new NameValuePair("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36")); HttpEntity reqEntity = multipartEntityBuilder.build();
httpPost.setEntity(reqEntity);
CloseableHttpResponse response = httpClient.execute(httpPost);
来自:《Java网络编程面试题》
出版单位:北京尚学堂优效学院
优效学院由清华大学著名的IT教育领导者马士兵老师创办,是一家线上线下相互融合的互联网+培训机构。公司均由海外留学生和国内行业精英人士担任授课讲师,主要成员均硕士且拥有十多年的行业经验。毕业学生就职于国内BAT以及海外著名公司。优效学院,名师执教,高效学习,成就未来。
著:张洋
优效学院_张洋老师
11年工作经验 曾就职联众游戏(程序员)、众信旅游(Team Leader)、精智教育(联合创始人)、中国石化(大数据高级顾问) 精通javaEE体系、互联网产品架构,熟习Sap Bw/HANA、多个大数据项目经验。
20180926版