<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
2、HttpUtil
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
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.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
public class HttpUtil {
private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
public static CloseableHttpClient getHttpClient(String username,String password){
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// 设置BasicAuth
CredentialsProvider provider = new BasicCredentialsProvider();
// Create the authentication scope
AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
// Create credential pair,在此处填写用户名和密码
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username,password);
// Inject the credentials
provider.setCredentials(scope, credentials);
// Set the default credentials provider
httpClientBuilder.setDefaultCredentialsProvider(provider);
// HttpClient
CloseableHttpClient httpClient = httpClientBuilder.build();
return httpClient;
}
public static CloseableHttpClient getHttpClient(){
CloseableHttpClient httpClient = HttpClients.createDefault();
return httpClient;
}
public static String sendPost(CloseableHttpClient httpclient,String url,String jsonParam){
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
// json格式参数
// StringEntity entity = new StringEntity(jsonParam, ContentType.APPLICATION_JSON);
// 普通key-value参数
List<NameValuePair> params = new ArrayList<>();
params.add( new BasicNameValuePair("bookId",String.valueOf(bookId)));
params.add( new BasicNameValuePair("userId",String.valueOf(userId)));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params,"utf-8");
httpPost.setEntity(entity);
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httpPost);
} catch (IOException e) {
logger.error("post请求发送失败",e);
return null;
}
int statusCode = response.getStatusLine().getStatusCode();
logger.info("post请求发送成功,响应码:"+statusCode);
try {
String result = EntityUtils.toString(response.getEntity(), "UTF-8");
return result;
} catch (IOException e) {
logger.error("获取响应体失败:",e);
return null;
} finally {
try {
response.close();
httpclient.close();
} catch (IOException e) {
logger.error("资源关闭失败",e);
}
}
}
public static String sendGet(CloseableHttpClient httpclient,String url){
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httpGet);
} catch (IOException e) {
logger.error("get请求发送失败",e);
return null;
}
int statusCode = response.getStatusLine().getStatusCode();
logger.info("post请求发送成功,响应码:"+statusCode);
try {
String result = EntityUtils.toString(response.getEntity(), "UTF-8");
return result;
} catch (IOException e) {
logger.error("获取响应体失败:",e);
return null;
} finally {
try {
response.close();
httpclient.close();
} catch (IOException e) {
logger.error("资源关闭失败",e);
}
}
}
}