HttpClient的get和post请求处理
程序员文章站
2022-05-09 09:57:48
...
package cn.test1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
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.HttpGet;
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.message.BasicNameValuePair;
/**
* 1.创建HttpClient对象
* 2.创建请求方法的实例,并指定请求URL,如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象
* 3.如果需要发送请求参数,可调用HttpGET或HttpPost共同的setParams(HttpParams params)方法来添加请求参数;
* 对于HttpPost对象而言,也可以调用SetEntity(HttpEntity entity)方法来设置请求参数。
* 4.调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。
* 5.调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;
* 调用HttpResponse的getEntity()方法可获取httpEntity()对象,该对象包装了服务器的响应内容。
* 程序可通过该对象获取服务器的响应内容。
* 6.释放连接,无论是否执行方法成功,都必须释放连接
*
*/
public class Test1 {
public static void main(String[] args) {
get();
post();
}
private static void post() {
//1.创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
//创建HttpGet对象
//此处路径不对哦,瞎写的
HttpPost httpPost = new HttpPost("http://www.baidu.com/login.action");
//创建参数队列
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", "admin"));
params.add(new BasicNameValuePair("password", "123456"));
UrlEncodedFormEntity formEntity;
formEntity = new UrlEncodedFormEntity(params,"utf-8");
//将参数设置到HttpPost对象中
httpPost.setEntity(formEntity);
System.out.println("执行请求路径:"+httpPost.getURI());
//执行get请求
CloseableHttpResponse response = httpClient.execute(httpPost);
System.out.println("响应转台"+response.getStatusLine());
//获取响应实体
HttpEntity entity = response.getEntity();
if(entity != null){
InputStream is = entity.getContent();
String str = convertStreamToString(is);
System.out.println("响应内容是:"+str);
httpPost.abort();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void get() {
//1.创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
//创建HttpGet对象
HttpGet httpGet = new HttpGet("http://www.baidu.com/");
System.out.println("执行请求路径:"+httpGet.getURI());
//执行get请求
CloseableHttpResponse response = httpClient.execute(httpGet);
System.out.println("响应转台"+response.getStatusLine());
//获取响应实体
HttpEntity entity = response.getEntity();
if(entity != null){
InputStream is = entity.getContent();
String str = convertStreamToString(is);
System.out.println("响应内容是:"+str);
httpGet.abort();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while((line = reader.readLine()) != null){
sb.append(line+"\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
转载于:https://blog.51cto.com/11841428/1876229