原java发送请求
public static void main(String[] args) throws IOException {
URL url = new URL("http://www.baidu.com");
HttpURLConnection urlconn = (HttpURLConnection) url.openConnection();
urlconn.setRequestMethod("GET");
urlconn.setConnectTimeout(5000);
urlconn.setReadTimeout(5000);
urlconn.setUseCaches(false);
urlconn.connect();
InputStream inputStream = urlconn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
BufferedReader buff = new BufferedReader(inputStreamReader);
String line = null;
while ((line = buff.readLine()) != null){
System.out.println(line);
}
inputStream.close();
inputStreamReader.close();
buff.close();
}
httpclient发送请求
public static void main(String[] args) throws IOException, ParseException {
CloseableHttpClient aDefault = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(30, TimeUnit.SECONDS)
.setConnectionRequestTimeout(10,TimeUnit.SECONDS)
.setResponseTimeout(10,TimeUnit.SECONDS)
.build();
------------------执行结果
响应码:200
实体Wrapper [[Content-Type: text/plain;charset=UTF-8,Content-Encoding: null,Content-Length: 8,Chunked: false]]
hello!!!
HttpPost httpPost = new HttpPost("http://localhost:8080/hello");
httpPost.setConfig(requestConfig);
httpPost.setHeader("Content-Type","application/json");
CloseableHttpResponse response = aDefault.execute(httpPost);
System.out.println("响应码:"+response.getCode());
HttpEntity entity = response.getEntity();
System.out.println("实体"+ entity);
String s = EntityUtils.toString(entity);
System.out.println(s);
------------------执行结果
响应码:200
实体Wrapper [[Content-Type: text/plain;charset=UTF-8,Content-Encoding: null,Content-Length: 8,Chunked: false]]
hello!!!
}
httpclient
springboot编写接口
public class Person implements Serializable {
private String name;
private String sex;
public Person() {
}
public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
@Controller
public class TestController {
@RequestMapping(value = "/test/getpGet",method = RequestMethod.GET)
@ResponseBody
public String getPersonGet1(){
return "hello!!!--get";
}
@RequestMapping(value = "/test/getpGetParam",method = RequestMethod.GET)
@ResponseBody
public Person getPersonGet2(String name,String sex){
Person p = new Person(name,sex);
System.out.println(p);
return p;
}
@RequestMapping(value = "/test/getpPost",method = RequestMethod.POST)
@ResponseBody
public String getPersonPost1(){
return "hello!!!--post";
}
@RequestMapping(value = "/test/getpPostParam",method = RequestMethod.POST)
@ResponseBody
public Person getPersonPost2(Person person){
System.out.println(person);
return person;
}
@RequestMapping(value = "/test/getpPostParamJson",method = RequestMethod.POST)
@ResponseBody
public Person getPersonPost3(@RequestBody Person person){
System.out.println(person);
return person;
}
}
get请求
public class HttpclientGet {
public static void main(String[] args) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://localhost:8080/test/getpGet");
CloseableHttpResponse response = null;
response = httpClient.execute(httpGet);
if (response.getCode() == 200){
String content = EntityUtils.toString(response.getEntity(),"utf8");
System.out.println(content);
}
response.close();
httpClient.close();
}
}
----------结果
hello!!!--get
get请求传参
public class HttpclientGetParam {
public static void main(String[] args) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
URIBuilder uriBuilder = new URIBuilder("http://localhost:8080/test/getpGetParam");
uriBuilder.setParameter("name","xiaoai")
.setParameter("sex","nan");
HttpGet httpGet = new HttpGet(uriBuilder.build());
CloseableHttpResponse response = null;
response = httpClient.execute(httpGet);
if (response.getCode() == 200){
String content = EntityUtils.toString(response.getEntity(),"utf8");
System.out.println(content);
}
response.close();
httpClient.close();
}
}
----------结果
{"name":"xiaoai","sex":"nan"}
post请求
public class HttpclientPost {
public static void main(String[] args) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://localhost:8080/test/getpPost");
CloseableHttpResponse response = null;
response = httpClient.execute(httpPost);
if (response.getCode() == 200){
String content = EntityUtils.toString(response.getEntity(),"utf8");
System.out.println(content);
}
response.close();
httpClient.close();
}
}
----------结果
hello!!!--post
post请求传参
1-表单传参
public class HttpclientPostParamForm {
public static void main(String[] args) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://localhost:8080/test/getpPostParam");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("name","xiaoai"));
params.add(new BasicNameValuePair("sex","nan"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, Charset.defaultCharset());
httpPost.setEntity(formEntity);
CloseableHttpResponse response = null;
response = httpClient.execute(httpPost);
System.out.println(response.getCode()+"=========");
if (response.getCode() == 200){
String content = EntityUtils.toString(response.getEntity(),"utf8");
System.out.println(content);
}
response.close();
httpClient.close();
}
}
----------结果
{"name":"xiaoai","sex":"nan"}
2-json格式传参
public class HttpclientPostParamJson {
public static void main(String[] args) throws IOException, ParseException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://localhost:8080/test/getpPostParamJson");
httpPost.setHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
JSONObject param = new JSONObject();
param.put("name","xiaoai");
param.put("sex","nan");
StringEntity StringEntity = new StringEntity(param.toString());
httpPost.setEntity(StringEntity);
CloseableHttpResponse response = null;
response = httpClient.execute(httpPost);
if (response.getCode() == 200){
String content = EntityUtils.toString(response.getEntity(),"utf8");
System.out.println(content);
}
response.close();
httpClient.close();
}
}
----------结果
{"name":"xiaoai","sex":"nan"}
连接池和请求配置java
public class HttpClientPoolT {
public static void main(String[] args) throws Exception {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(100);
cm.setDefaultMaxPerRoute(10);
doGet(cm);
doPost(cm);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(30, TimeUnit.SECONDS)
.setConnectionRequestTimeout(10,TimeUnit.SECONDS)
.setResponseTimeout(10,TimeUnit.SECONDS)
.build();
doGet(cm,requestConfig);
doPost(cm,requestConfig);
}
private static void doGet(PoolingHttpClientConnectionManager cm) throws Exception {
HttpClientBuilder httpClientBuilder = HttpClients.custom().setConnectionManager(cm);
CloseableHttpClient httpClient = httpClientBuilder.build();
HttpGet httpGet = new HttpGet("http://localhost:8080/test/getpGet");
CloseableHttpResponse response = httpClient.execute(httpGet);
if (response.getCode() == 200){
String content = EntityUtils.toString(response.getEntity(), Charset.defaultCharset());
System.out.println(content);
}
response.close();
}
private static void doPost(PoolingHttpClientConnectionManager cm) throws Exception {
HttpClientBuilder httpClientBuilder = HttpClients.custom().setConnectionManager(cm);
CloseableHttpClient httpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost("http://localhost:8080/test/getpPost");
CloseableHttpResponse response = httpClient.execute(httpPost);
if (response.getCode() == 200){
String content = EntityUtils.toString(response.getEntity(), Charset.defaultCharset());
System.out.println(content);
}
response.close();
}
private static void doGet(PoolingHttpClientConnectionManager cm, RequestConfig requestConfig) throws Exception {
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(cm)
.setDefaultRequestConfig(requestConfig)
.build();
HttpGet httpGet = new HttpGet("http://localhost:8080/test/getpGet");
CloseableHttpResponse response = httpClient.execute(httpGet);
if (response.getCode() == 200){
String content = EntityUtils.toString(response.getEntity(), Charset.defaultCharset());
System.out.println(content);
}
response.close();
}
private static void doPost(PoolingHttpClientConnectionManager cm, RequestConfig requestConfig)throws Exception {
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(cm)
.build();
HttpPost httpPost = new HttpPost("http://localhost:8080/test/getpPost");
httpPost.setConfig(requestConfig);
CloseableHttpResponse response = httpClient.execute(httpPost);
if (response.getCode() == 200){
String content = EntityUtils.toString(response.getEntity(), Charset.defaultCharset());
System.out.println(content);
}
response.close();
}
}