package bhz.utils;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
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 org.apache.http.util.EntityUtils;
public class HttpProxy {
private static class SingletonHolder {
static final HttpProxy instance = new HttpProxy();
}
public static HttpProxy getInstance() {
return SingletonHolder.instance;
}
private static CloseableHttpClient httpClient;
private static final String CONTENT_TYPE_JSON = "application/json";
static {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(HttpProxyConfig.MAX_TOTAL_CONNECTIONS);
cm.setDefaultMaxPerRoute(HttpProxyConfig.MAX_ROUTE_CONNECTIONS);
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(HttpProxyConfig.CONNECT_TIMEOUT)
.setConnectTimeout(HttpProxyConfig.CONNECT_TIMEOUT)
.build();
//缓存
/*
CacheConfig cacheConfig = CacheConfig.custom()
.setMaxCacheEntries(1000)
.setMaxObjectSize(8192)
.build();
*/
httpClient = HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.setConnectionManager(cm)
.build();
}
public static HttpClient getHttpClient() {
return httpClient;
}
/**
* <B>方法名称:</B>普通请求<BR>
* <B>概要说明:</B>普通请求<BR>
* @param requestUrl 请求路径
* @param requestContent 请求内容
* @return 返回响应结果
* @throws IOException
*/
public static String post(String requestUrl, String requestContent) throws IOException {
StringEntity requestEntity = new StringEntity(requestContent, Consts.UTF_8);
return execute(requestUrl,requestEntity);
}
/**
* <B>方法名称:</B>json请求<BR>
* <B>概要说明:</B>json请求<BR>
* @param requestUrl 请求路径
* @param jsonContent json内容
* @return 返回响应结果
* @throws IOException
*/
public static String postJson(String requestUrl, String jsonContent) throws IOException {
StringEntity requestEntity = new StringEntity(jsonContent, Consts.UTF_8);
requestEntity.setContentEncoding("UTF-8");
requestEntity.setContentType(CONTENT_TYPE_JSON);
return execute(requestUrl,requestEntity);
}
/**
* <B>方法名称:</B>模拟表单上传<BR>
* <B>概要说明:</B>模拟表单上传<BR>
* @param requestUrl 请求路径
* @param params 属性参数
* @return 返回响应结果
* @throws IOException
*/
public static String post(String requestUrl, Map<String, String> params) throws IOException {
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
if (params != null) {
for (Map.Entry<String, String> entry : params.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
EntityBuilder builder = EntityBuilder.create();
builder.setParameters(nvps);
HttpEntity httpEntity = builder.build();
return execute(requestUrl,httpEntity);
}
/**
* <B>方法名称:</B>上传文件<BR>
* <B>概要说明:</B>上传文件<BR>
* @param requestUrl 请求路径
* @param localFile 文件位置
* @param username 用户名
* @param password 密码
* @return 响应信息
* @throws IOException
*/
public static String upload(String requestUrl, String localFile, String username, String password) throws IOException {
HttpPost httpPost = new HttpPost(requestUrl);
// 把文件转换成流对象FileBody
FileBody fileBody = new FileBody(new File(localFile));
StringBody usernameInp = new StringBody(username, ContentType.create("text/plain", Consts.UTF_8));
StringBody passwordInp = new StringBody(password, ContentType.create("text/plain", Consts.UTF_8));
HttpEntity httpEntity = MultipartEntityBuilder.create()
// 相当于<input type="file" name="file"/>
.addPart("file", fileBody)
// 相当于<input type="text" name="userName" value=userName>
.addPart("username", usernameInp)
.addPart("password", passwordInp)
.build();
return execute(requestUrl,httpEntity);
}
/**
* <B>方法名称:</B>执行请求方法<BR>
* <B>概要说明:</B>执行请求方法<BR>
* @param requestUrl 请求路径
* @param httpEntity 请求实体对象
* @return 返回响应结果
* @throws IOException
*/
private static String execute(String requestUrl, HttpEntity httpEntity) throws IOException {
String result = null;
HttpPost httpPost = new HttpPost(requestUrl);
httpPost.setEntity(httpEntity);
try {
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
try {
HttpEntity entity = httpResponse.getEntity();
//System.out.println(httpResponse.getStatusLine().getStatusCode());
if (httpResponse.getStatusLine().getReasonPhrase().equals("OK") && httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
result = EntityUtils.toString(entity, "UTF-8");
}
//进行销毁
EntityUtils.consume(entity);
} finally {
if (null != httpResponse) {
httpResponse.close();
}
}
} finally {
if (null != httpPost) {
httpPost.releaseConnection();
}
}
return result;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
dubbox生产者和消费者示例代码(含依赖包).zip
共246个文件
jar:184个
java:30个
xml:8个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 122 浏览量
2023-12-22
20:04:48
上传
评论
收藏 49.61MB ZIP 举报
温馨提示
dubbox生产者和消费者示例代码(含依赖包).
资源推荐
资源详情
资源评论
收起资源包目录
dubbox生产者和消费者示例代码(含依赖包).zip (246个子文件)
.classpath 1KB
.classpath 1KB
org.eclipse.wst.common.component 582B
org.eclipse.wst.common.component 582B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
tomcat-embed-core-8.0.11.jar 2.69MB
tomcat-embed-core-8.0.11.jar 2.69MB
guava-16.0.1.jar 2.12MB
guava-16.0.1.jar 2.12MB
dubbo-2.8.4.jar 1.5MB
dubbo-2.8.4.jar 1.5MB
netty-3.7.0.Final.jar 1.15MB
netty-3.7.0.Final.jar 1.15MB
cxf-api-2.6.1.jar 961KB
cxf-api-2.6.1.jar 961KB
jaxb-impl-2.2.7.jar 898KB
jaxb-impl-2.2.7.jar 898KB
spring-core-3.2.9.RELEASE.jar 864KB
spring-core-3.2.9.RELEASE.jar 864KB
spring-context-3.2.9.RELEASE.jar 846KB
spring-context-3.2.9.RELEASE.jar 846KB
resteasy-jaxrs-3.0.7.Final.jar 802KB
resteasy-jaxrs-3.0.7.Final.jar 802KB
zookeeper-3.4.6.jar 774KB
zookeeper-3.4.6.jar 774KB
jackson-mapper-asl-1.9.12.jar 762KB
jackson-mapper-asl-1.9.12.jar 762KB
grizzly-core-2.1.4.jar 692KB
grizzly-core-2.1.4.jar 692KB
grizzly-framework-2.1.4.jar 676KB
grizzly-framework-2.1.4.jar 676KB
javassist-3.15.0-GA.jar 633KB
javassist-3.15.0-GA.jar 633KB
spring-web-3.2.9.RELEASE.jar 618KB
spring-web-3.2.9.RELEASE.jar 618KB
spring-beans-3.2.9.RELEASE.jar 599KB
spring-beans-3.2.9.RELEASE.jar 599KB
httpclient-4.3.1.jar 572KB
httpclient-4.3.1.jar 572KB
jetty-6.1.26.jar 527KB
jetty-6.1.26.jar 527KB
xstream-1.4.1.jar 484KB
xstream-1.4.1.jar 484KB
log4j-1.2.16.jar 470KB
log4j-1.2.16.jar 470KB
woodstox-core-asl-4.1.2.jar 468KB
woodstox-core-asl-4.1.2.jar 468KB
xmemcached-1.3.6.jar 412KB
xmemcached-1.3.6.jar 412KB
hessian-4.0.7.jar 375KB
hessian-4.0.7.jar 375KB
hibernate-validator-4.2.0.Final.jar 358KB
hibernate-validator-4.2.0.Final.jar 358KB
fastjson-1.1.39.jar 349KB
fastjson-1.1.39.jar 349KB
cxf-rt-core-2.6.1.jar 344KB
cxf-rt-core-2.6.1.jar 344KB
kryo-2.24.0.jar 331KB
kryo-2.24.0.jar 331KB
spring-aop-3.2.9.RELEASE.jar 330KB
spring-aop-3.2.9.RELEASE.jar 330KB
libthrift-0.8.0.jar 329KB
libthrift-0.8.0.jar 329KB
mina-core-1.1.7.jar 308KB
mina-core-1.1.7.jar 308KB
FastInfoset-1.2.12.jar 287KB
FastInfoset-1.2.12.jar 287KB
httpcore-4.3.1.jar 276KB
httpcore-4.3.1.jar 276KB
commons-lang-2.5.jar 273KB
commons-lang-2.5.jar 273KB
fst-1.55.jar 243KB
fst-1.55.jar 243KB
commons-codec-1.6.jar 227KB
commons-codec-1.6.jar 227KB
jackson-core-asl-1.9.12.jar 227KB
jackson-core-asl-1.9.12.jar 227KB
geronimo-javamail_1.4_spec-1.7.1.jar 218KB
geronimo-javamail_1.4_spec-1.7.1.jar 218KB
jaxb-core-2.2.7.jar 217KB
jaxb-core-2.2.7.jar 217KB
cxf-rt-transports-http-2.6.1.jar 209KB
cxf-rt-transports-http-2.6.1.jar 209KB
spring-expression-3.2.9.RELEASE.jar 192KB
spring-expression-3.2.9.RELEASE.jar 192KB
cxf-rt-bindings-soap-2.6.1.jar 184KB
cxf-rt-bindings-soap-2.6.1.jar 184KB
stax2-api-3.1.1.jar 178KB
stax2-api-3.1.1.jar 178KB
curator-framework-2.5.0.jar 177KB
curator-framework-2.5.0.jar 177KB
jetty-util-6.1.26.jar 173KB
jetty-util-6.1.26.jar 173KB
commons-io-2.1.jar 159KB
commons-io-2.1.jar 159KB
xmlschema-core-2.0.2.jar 159KB
xmlschema-core-2.0.2.jar 159KB
wsdl4j-1.6.2.jar 145KB
wsdl4j-1.6.2.jar 145KB
共 246 条
- 1
- 2
- 3
资源评论
小小哭包
- 粉丝: 1899
- 资源: 3854
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功