GET
package com.tungkong.httpclient;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
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 java.io.IOException;
public class HttpClientGet {
//https://blog.csdn.net/yangsf_/article/details/124527687
public static void main(String[] args) throws IOException {
// 1. 创建HttpClient实例
CloseableHttpClient httpClient = HttpClients.createDefault();
// 2. 创建GET请求方法实例
HttpGet httpGet = new HttpGet("http://172.16.196.187:8666/sns/oauth2/access_token");
// 3. 调用HttpClient实例来执行GET请求方法,得到response
CloseableHttpResponse response = httpClient.execute(httpGet);
// 4. 读response,判断是否访问成功 2xx表示 成功。
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
// 对得到后的实例可以进行处理,例如读取回复体,读取html
HttpEntity entity = response.getEntity();
System.out.println(response);
System.out.println("=======================");
String html = EntityUtils.toString(entity);
System.out.println(html);
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
// 6. 释放连接
response.close();
httpClient.close();
}
}
POST
package com.tungkong.httpclient;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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 java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class HttpClientPost {
public static void main(String... args) throws IOException {
// 1. 创建HttpClient实例
CloseableHttpClient httpclient = HttpClients.createDefault();
// 2. 设置表单参数
List<NameValuePair> kv = new ArrayList<>();
kv.add(new BasicNameValuePair("name", "root"));
kv.add(new BasicNameValuePair("password", "123456"));
// 3. 创建HttpPost实例
HttpPost httpPost = new HttpPost("http://172.16.196.187:8666/sns/oauth2/access_token");
httpPost.setEntity(new StringEntity("this is Post"));
// 4. 让Post携带表单参数
httpPost.setEntity(new UrlEncodedFormEntity(kv, Consts.UTF_8));
// 5. 调用HttpClient实例来执行HttpPost实例
CloseableHttpResponse response = httpclient.execute(httpPost);
// 6. 读 response
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
System.out.println(response);
System.out.println("===================");
String html = EntityUtils.toString(entity);
System.out.println("html="+html);
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
// 5. 释放连接
response.close();
httpclient.close();
}
}