HttpClient请求
HttpClient
HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,
并且它支持 HTTP 协议最新的版本和建议。
使用httpClient执行get请求<url不带参的>
import
org.apache.http.HttpEntity;
import
org.apache.http.client.methods.CloseableHttpResponse;
import
org.apache.http.client.methods.HttpGet;
import
org.apache.http.impl.client.CloseableHttpClient;
import
org.apache.http.impl.client.HttpClients;
import
org.apache.http.util.EntityUtils;
import org.junit.Test;
public class HttpClient {
@Test
public void doGet()throws Exception{
//创建一个HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建一个GET对象
HttpGet get = new HttpGet("http://www.sogou.com");
//执行请求
CloseableHttpResponse response = httpClient.execute(get);
//获取相应结果
int statusCode = response.getStatusLine().getStatusCode();//响应的状态码
System.out.println(statusCode);//如果结果为200 即为正常
HttpEntity entity = response.getEntity();//响应内容
String string = EntityUtils.toString(entity);
System.out.println(string);
//关闭HttpClient
response.close();
httpClient.close();
}
}
使用httpClient执行get请求<url带参的>
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
public class HttpClient {
@Test
public void doGetWithParam()throws Exception{
//创建一个httpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建一个uri对象
URIBuilder uriBuilder = new URIBuilder("http://www.sogou.com/web");
uriBuilder.addParameter("query", "杀破狼"); //www.sogou.com/web?query=杀破狼
HttpGet get = new HttpGet(uriBuilder.build());
//执行结果
CloseableHttpResponse response = httpClient.execute(get);
//获取响应结果
int statusCode = response.getStatusLine().getStatusCode();
System.out.println(statusCode);
HttpEntity entity = response.getEntity();
String string = EntityUtils.toString(entity,"UTF-8");
System.out.println(string);
//关闭HttpClient
response.close();
httpClient.close();
}
}
HttpClient的post请求方式<不带参数的post请求>
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
public class HttpClient {
@Test
public void doPost()throws Exception{
//创建一个httpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建一个post对象
HttpPost post = new HttpPost("http://localhost:8080/httpclient/post.html");
//执行结果
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
//关闭HttpClient
response.close();
httpClient.close();
}
}
HttpClient使用post请求<带参数请求>
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
public class HttpClient {
@Test
public void doPostWithParam()throws Exception{
//创建一个httpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建一个post对象
HttpPost post = new HttpPost("http://localhost:8080/httpclient/post.html");
//创建一个Entity.模拟一个表单
List<NameValuePair> kvList = new ArrayList<>();
kvList.add(new BasicNameValuePair("username","abc"));
kvList.add(new BasicNameValuePair("password","123"));
//包装成一个Entity对象
StringEntity entity = new UrlEncodedFormEntity(kvList);
//设置请求内容
post.setEntity(entity);
//执行结果
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
//关闭HttpClient
response.close();
httpClient.close();
}
}
HttpClient请求的更多相关文章
- httpclient请求方法
/** * httpclient请求方法 * @param url 请求地址 * @param paramMap 请求参数 * @param ent 编码格式 gbk.utf-8 * @return ...
- HttpClient请求服务器代码优化版
HttpClient请求服务器代码优化版 首先,我在前面的两篇博文中介绍了在 Android中,除了使用java.net包下HttpUrlConnection的API访问HTTP服务之外,我们还可以换 ...
- 通过HttpClient请求webService
通过HttpClient请求webService 由于服务端是用webService开发的,android要调用webService服务获取数据,这里采用的是通过HttpClient发送post请求, ...
- C# HttpClient 请求认证、数据传输笔记
目录 一,授权认证 二,请求类型 三,数据传输 C# HttpClient 请求认证.数据传输笔记 一,授权认证 客户端请求服务器时,需要通过授权认证许可,方能获取服务器资源,目前比较常见的认证方式有 ...
- SpringMVC获取HttpClient 请求的数据
package com.nnk.upstream.controller;import org.springframework.util.StreamUtils;import javax.servlet ...
- .NetCore简单封装基于IHttpClientFactory的HttpClient请求
IHttpClientFactory是什么?为什么出现了IHttpClientFactory 一.IHttpClientFactory是什么? IHttpClientFactory是.netcore2 ...
- .NET Core HttpClient请求异常详细情况分析
前言 最近项目上每天间断性捕获到HttpClient请求异常,感觉有点奇怪,于是乎观察了两三天,通过日志以及对接方沟通确认等等,查看对应版本源码,尝试添加部分配置发布后,观察十几小时暂无异常情况出现, ...
- Httpclient请求数据(post)
public static String loginCheck_POST_HttpClient(String name,String pass,String url){ String result = ...
- HttpClient请求返回JSON、图片
/** * Created by RongGuang on 2015/9/19. */ public class RongHttp { /** * Http Post请求 * @param url * ...
随机推荐
- string的实现
面试常常用到string类的实现,自己总结了一下: #pragma once #include <iostream> #include <cassert> #include & ...
- Add Two Numbers (c#)
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 不懂指针就不要说自己学过C语言!
不懂指针就不要说自己学过C语言! 1.掌握了指针,就掌握了C语言的精髓!计算机中绝大部分数据都放到内存中的,不同的数据放到不同的内存区域中. 内存角度没有数据类型,只有二进制:数据以字节(8位二进制) ...
- jquery.get()
1.获取当前jquery对象匹配到的dom元素 2.语法: jqueryObject.get([index]) //jQueryObject[index]等价于jQueryObject.get(ind ...
- Android sdk环境配置
1.环境搭建 1.1.JDK安装 1.2.Eclipse安装 1.3.Android SDK安装 1.4.ADT安装 1.5.创建AVD 详细细节 http://www.cnblogs.com/s ...
- C++的STL
今天,看一段代码的时候发现只一句话就做了个排序,是这样的: sort(rotateArray.begin(),rotateArray.end()); 很震惊,后来查了一下sort的用法, sort函数 ...
- web前端教程之javascript创建对象的方法
今天给大家讲讲javascript基础教程中的javascript面向对象的技术,这一次我们深入的学习一下JavaScrip基于t面向对象之创建对象,关于面向对象的一些术语这里就不给大家介绍了,不了解 ...
- 内存对齐 和 sizeof小结
数据对齐(内存对齐)指该数据所在的地址必须是该数据长度的整数倍.X86CPU能直接访问对齐的数据,当它试图访问未对齐的数据时,会在内部进行一系列的调整,降低运行速度.数据对齐一般出现在结构体和类中,在 ...
- Django实现表单验证、CSRF、cookie和session、缓存、数据库多表操作(双下划綫)
通常验证用户输入是否合法的话,是前端js和后端共同验证的,这是因为前端js是可以被禁用的,假如被禁用了,那就没法用js实现验证合法与否了,也就是即使用户输入的不合法,但是也没提示,用户也不知道怎么输入 ...
- MySQL语句中的转义字符----引号
MySQL语言中的转义字符和各种编程语言基本相同,见下表 形式 含义 \0 0(NUL)字符 \n 换行 \r 回车符 \t 制表符 \b 退格 \' 单引号 \" 双引号 \\ 反斜线 \ ...