package com.study.ucenter.utils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.config.RequestConfig.Builder;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.security.GeneralSecurityException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* 依赖的jar包有:commons-lang-2.6.jar、httpclient-4.3.2.jar、httpcore-4.3.1.jar、commons-io-2.4.jar
* @author zhaoyb
*
*/
public class HttpClientUtils {
public static final int connTimeout=10000;
public static final int readTimeout=10000;
public static final String charset="UTF-8";
private static HttpClient client = null;
static {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(128);
cm.setDefaultMaxPerRoute(128);
client = HttpClients.custom().setConnectionManager(cm).build();
}
public static String postParameters(String url, String parameterStr) throws ConnectTimeoutException, SocketTimeoutException, Exception{
return post(url,parameterStr,"application/x-www-form-urlencoded",charset,connTimeout,readTimeout);
}
public static String postParameters(String url, String parameterStr,String charset, Integer connTimeout, Integer readTimeout) throws ConnectTimeoutException, SocketTimeoutException, Exception{
return post(url,parameterStr,"application/x-www-form-urlencoded",charset,connTimeout,readTimeout);
}
public static String postParameters(String url, Map<String, String> params) throws ConnectTimeoutException,
SocketTimeoutException, Exception {
return postForm(url, params, null, connTimeout, readTimeout);
}
public static String postParameters(String url, Map<String, String> params, Integer connTimeout,Integer readTimeout) throws ConnectTimeoutException,
SocketTimeoutException, Exception {
return postForm(url, params, null, connTimeout, readTimeout);
}
public static String get(String url) throws Exception {
return get(url, charset, null, null);
}
public static String get(String url, String charset) throws Exception {
return get(url, charset, connTimeout, readTimeout);
}
/**
* 发送一个 Post 请求, 使用指定的字符集编码.
*
* @param url
* @param body RequestBody
* @param mimeType 例如 application/xml "application/x-www-form-urlencoded" a=1&b=2&c=3
* @param charset 编码
* @param connTimeout 建立链接超时时间,毫秒.
* @param readTimeout 响应超时时间,毫秒.
* @return ResponseBody, 使用指定的字符集编码.
* @throws ConnectTimeoutException 建立链接超时异常
* @throws SocketTimeoutException 响应超时
* @throws Exception
*/
public static String post(String url, String body, String mimeType,String charset, Integer connTimeout, Integer readTimeout)
throws ConnectTimeoutException, SocketTimeoutException, Exception {
HttpClient client = null;
HttpPost post = new HttpPost(url);
String result = "";
try {
if (StringUtils.isNotBlank(body)) {
HttpEntity entity = new StringEntity(body, ContentType.create(mimeType, charset));
post.setEntity(entity);
}
// 设置参数
Builder customReqConf = RequestConfig.custom();
if (connTimeout != null) {
customReqConf.setConnectTimeout(connTimeout);
}
if (readTimeout != null) {
customReqConf.setSocketTimeout(readTimeout);
}
post.setConfig(customReqConf.build());
HttpResponse res;
if (url.startsWith("https")) {
// 执行 Https 请求.
client = createSSLInsecureClient();
res = client.execute(post);
} else {
// 执行 Http 请求.
client = HttpClientUtils.client;
res = client.execute(post);
}
result = IOUtils.toString(res.getEntity().getContent(), charset);
} finally {
post.releaseConnection();
if (url.startsWith("https") && client != null&& client instanceof CloseableHttpClient) {
((CloseableHttpClient) client).close();
}
}
return result;
}
/**
* 提交form表单
*
* @param url
* @param params
* @param connTimeout
* @param readTimeout
* @return
* @throws ConnectTimeoutException
* @throws SocketTimeoutException
* @throws Exception
*/
public static String postForm(String url, Map<String, String> params, Map<String, String> headers, Integer connTimeout,Integer readTimeout) throws ConnectTimeoutException,
SocketTimeoutException, Exception {
HttpClient client = null;
HttpPost post = new HttpPost(url);
try {
if (params != null && !params.isEmpty()) {
List<NameValuePair> formParams = new ArrayList<NameValuePair>();
Set<Entry<String, String>> entrySet = params.entrySet();
for (Entry<String, String> entry : entrySet) {
formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, Consts.UTF_8);
post.setEntity(entity);
}
if (headers != null && !headers.isEmpty()) {
for (Entry<String, String> entry : headers.entrySet()) {
post.addHeader(entry.getKey(), entry.getValue());
}
}
// 设置参数
Builder customReqConf = RequestConfig.custom();
if (connTimeout != null) {
customReqConf.setConnectTimeout(connTimeout);
}
if (readTimeout != null) {
customReqConf.setSocketTimeout(readTimeout);
}
post.setConfig(customReqConf.build());
HttpResponse res = null;
if (url.startsWith("https")) {
// 执行 Https 请求.
client = createSSLInsecureClient();
res = client.execute(post);
} else {
// 执行 Http 请求.
client = HttpClientUtils.client;
res = client.execute(post);
}
return IOUtils.toString(res.getEntity().getContent(), "UTF-8");
} finally {
post.releaseConnection();
if (url.startsWith("https") && client != null
&& client instanceof CloseableHttpClient) {
((CloseableHttpClient) client).close();
}
}
}
/**
* 发送一个 GET 请求
*
* @param url
* @param charset
* @param connTimeout 建立链接超时时间,毫秒.
* @param readTimeout 响应超时时间,毫秒.
* @return
* @throws ConnectTimeoutException 建立链接超时
* @throws SocketTimeoutException 响应超时
* @throws Exception
*/
public static String get(String url, String charset, Integer connTimeout,Integer readTimeout)
throws ConnectTimeoutException,SocketTimeoutException, Exception {
HttpClient client = null;
HttpGet get = new HttpGet(url);
String result = "";
try {
// 设置参数
Builder customReqConf = RequestConfig.custom();
if (connTimeout != null) {
customReqConf.setConnectTimeout(connTimeout);
}
if (readTimeout != null) {
customReqConf.setSocketTimeout(readTimeout);
}
get.setCo
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
该项目是一款基于Java技术的edu_parent后台课程讲师轮播图管理系统源码,包含224个文件,其中包括172个Java源文件、35个XML配置文件、12个属性文件,以及其他必要的支持文件。该系统支持后台讲师轮播图的管理功能,并允许前台用户查询热门课程及讲师的轮播信息。
资源推荐
资源详情
资源评论
收起资源包目录
基于Java的edu_parent后台课程讲师轮播图管理系统源码设计 (227个子文件)
mvnw.cmd 6KB
.gitignore 318B
maven-wrapper.jar 50KB
HttpClientUtils.java 11KB
EduCourseServiceImpl.java 10KB
EduCourseController.java 5KB
AclPermissionServiceImpl.java 5KB
WxApiController.java 5KB
VodServiceImpl.java 5KB
TestVod.java 5KB
MavenWrapperDownloader.java 5KB
EduTeacherController.java 5KB
PayLogServiceImpl.java 5KB
MemberServiceImpl.java 4KB
MsmServiceImpl.java 4KB
EduTeacherServiceImpl.java 4KB
OrderServiceImpl.java 4KB
EduChapterServiceImpl.java 4KB
SubjectExcelListener.java 3KB
TokenLoginFilter.java 3KB
RedisConfig.java 3KB
TokenWebSecurityConfig.java 3KB
CodeGenerator.java 3KB
TokenAuthenticationFilter.java 3KB
CodeGenerator.java 3KB
CodeGenerator.java 3KB
CodeGenerator.java 3KB
CodeGenerator.java 3KB
CodeGenerator.java 3KB
BannerAdminController.java 3KB
JwtUtils.java 3KB
StatisticsServiceImpl.java 3KB
MemberController.java 3KB
EduSubjectServiceImpl.java 3KB
CourseFrontController.java 3KB
EduChapterController.java 3KB
TeacherFrontController.java 2KB
OssServiceImpl.java 2KB
EduCommentServiceImpl.java 2KB
EduVideo.java 2KB
CrmBannerServiceImpl.java 2KB
PayLogController.java 2KB
AclPermissionController.java 2KB
OrderController.java 2KB
EduCourse.java 2KB
MailServiceImpl.java 2KB
Order.java 2KB
EduVideoController.java 2KB
VodController.java 2KB
AclPermission.java 2KB
SecurityUser.java 2KB
Member.java 2KB
EduVideoServiceImpl.java 2KB
ConstantAlipayUtils.java 2KB
CourseWebVo.java 2KB
PayLog.java 2KB
R.java 2KB
EduTeacher.java 2KB
MsmController.java 2KB
IndexFrontController.java 2KB
Statistics.java 2KB
Md5Utils.java 2KB
EduComment.java 2KB
SwaggerConfig.java 1KB
AclUser.java 1KB
EduCommentController.java 1KB
CrmBanner.java 1KB
EduSubjectController.java 1KB
EduSubject.java 1KB
EduChapter.java 1KB
TokenLogoutHandler.java 1KB
AclRole.java 1KB
EduCourseDescription.java 1KB
ScheduledTask.java 1KB
CourseInfoVo.java 1KB
AclUserRole.java 1KB
EduCourseService.java 1KB
ConstantPropertiesUtils.java 1KB
AclRolePermission.java 1KB
StatisticsController.java 1KB
OssController.java 1KB
BannerFrontController.java 1KB
TokenManager.java 1KB
VodClient.java 1KB
GlobalExceptionHandler.java 1KB
EduClient.java 960B
EduConfig.java 901B
EduConfig.java 893B
CourseFrontVo.java 879B
DefaultPasswordEncoder.java 868B
EduTeacherApplication.java 864B
TeacherQuery.java 863B
StatisticsApplication.java 859B
CoursePublishVo.java 857B
ConstantWxUtils.java 855B
UnauthorizedEntryPoint.java 834B
MyMetaObjectHandler.java 811B
RandomUtil.java 793B
CommentApplication.java 743B
ConstantPropertiesUtils.java 738B
共 227 条
- 1
- 2
- 3
资源评论
lsx202406
- 粉丝: 2784
- 资源: 5657
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Cursor Setup 0.43.6 - Build
- 目标检测数据集:鸟类头部图像检测数据【VOC标注格式、包含数据和标签】
- 荒地、水体、农田、湖检测14-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- 2021九月最新视频打赏系统多套模板界面非常漂亮站长亲测
- 超好看倒计时特效单页html模板源码.zip
- 荒地、农田、森林、湖、山姆、住宅检测11-YOLO(v5至v9)、COCO、CreateML、Darknet、Paligemma、TFRecord、VOC数据集合集.rar
- 基于epoll的reactor模型
- 人力资源领域人员简历模板docx文档
- 使用python基于CNN的10种水果识别-含1w张以上的数据集图片
- 基于Delaunay三角化的点云数据三维曲面重建matlab仿真,包括程序,中文注释,仿真操作步骤视频
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功