package com.teorange.magic.bottle.admin.modules.job.utils;
import cn.teorange.framework.core.exception.RRException;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
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 org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
/**
* Created by admin on 2016/1/23.
*/
public class HttpRequestUtil {
private static Logger logger = LoggerFactory.getLogger(HttpRequestUtil.class);
private HttpRequestUtil() {
}
/**
* 连接时间
*/
private static int connectTimeout = 30000;
/**
* 响应时间
*/
private static int readTimeout = 60000;
protected static HttpClient mClient;
public static String transPost(String url, File files, String fileName) throws IOException {
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter("http.socket.timeout", Integer.valueOf(5000));
HttpPost httpPost = new HttpPost(url);
String message = "This is a multipart post";
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("upfile", files, ContentType.MULTIPART_FORM_DATA, fileName);
builder.addTextBody("text", message, ContentType.TEXT_PLAIN);
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
HttpResponse response = client.execute(httpPost);
return EntityUtils.toString(response.getEntity(), "utf-8");
}
public static String post(String url, Map<String, String> header, Map<String, Object> body, File in, String fileName)
throws IOException {
mClient = new DefaultHttpClient();
mClient.getParams().setParameter("http.socket.timeout", 5 * 1000);
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("accept", "*/*");
httpPost.setHeader("connection", "Keep-Alive");
httpPost.setHeader("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
if (null != header) {
for (Map.Entry<String, String> entry : header.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
}
if (null != header && (false == header.containsKey("Content-Type")
|| header.get("Content-Type").equals("multipart/form-data"))) {
MultipartEntity multipartEntity = new MultipartEntity();
if (body != null) {
for (Map.Entry<String, Object> entry : body.entrySet()) {
multipartEntity.addPart(entry.getKey(), new StringBody(entry.getValue().toString(), ContentType.MULTIPART_FORM_DATA));
}
}
if (in != null) {
ContentBody contentBody = new FileBody(in, ContentType.MULTIPART_FORM_DATA);
multipartEntity.addPart(fileName, contentBody);
}
httpPost.setEntity(multipartEntity);
} else {
if (in != null) {
String strBody = new String(in.toString());
StringEntity stringEntity = new StringEntity(strBody);
httpPost.setEntity(stringEntity);
}
}
HttpResponse httpResponse = mClient.execute(httpPost);
return EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
}
/**
* MAP数据转换成HTTP参数格式的字符串
*
* @param requestMap
* @return String
*/
public static String toHttpParamString(Map<String, Object> requestMap) {
StringBuilder result = new StringBuilder();
if (requestMap != null && !requestMap.isEmpty()) {
for (Map.Entry<String, Object> entry : requestMap.entrySet()) {
if (!StringUtils.isEmpty(entry.getKey())) {
result.append(entry.getKey() + "=" + entry.getValue() + "&");
}
}
result = new StringBuilder(result.substring(0, result.length() - 1));
}
return result.toString();
}
/**
* 向指定 URL 发送POST方法的请求
*
* @param url 发送请求的 URL
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public static String sendGet(String url, String param) {
return sendGet(url, param, "application/x-www-form-urlencoded");
}
/**
* 向指定 URL 发送POST方法的请求
*
* @param url 发送请求的 URL
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public static String sendJsonGet(String url, String param) {
return sendGet(url, param, "application/json");
}
/**
* 向指定URL发送GET方法的请求
*
* @param url 发送请求的URL
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @param contentType 媒体类型
* @return URL 所代表远程资源的响应结果
*/
public static String sendGet(String url, String param, String contentType) {
return sendGet(url, param, contentType, connectTimeout, readTimeout);
}
/**
* 向指定URL发送GET方法的请求
*
* @param url 发送请求的URL
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @param contentType 媒体类型
* @param connectTimeout 连接时间
* @param readTimeout 响应时间
* @return URL 所代表远程资源的响应结果
*/
public static String sendGet(String url, String param, String contentType, int connectTimeout, int readTimeout) {
String result = "";
BufferedReader in = null;
try {
if (StringUtils.isNotEmpty(param)) {
if (url.indexOf("?") > 0) {
url = url + "&" + param;
} else {
url = url + "?" + param;
}
}
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
magic-bottle项目是一套匿名社交系统,包括IOS、Andriod、WEB管理端以及服务端,采用DDD+CQRS架构,是DDD真正落地的开源项目 软件开发设计:PHP、QT、应用软件开发、系统软件开发、移动应用开发、网站开发C++、Java、python、web、C#等语言的项目开发与学习资料 硬件与设备:单片机、EDA、proteus、RTOS、包括计算机硬件、服务器、网络设备、存储设备、移动设备等 操作系统:LInux、IOS、树莓派、安卓开发、微机操作系统、网络操作系统、分布式操作系统等。此外,还有嵌入式操作系统、智能操作系统等。 网络与通信:数据传输、信号处理、网络协议、网络与通信硬件、网络安全网络与通信是一个非常广泛的领域,它涉及到计算机科学、电子工程、数学等多个学科的知识。 云计算与大数据:数据集、包括云计算平台、大数据分析、人工智能、机器学习等,云计算是一种基于互联网的计算方式,通过这种方式,共享的软硬件资源和信息可以按需提供给计算机和其他设备。
资源推荐
资源详情
资源评论
收起资源包目录
magic-bottle项目是一套匿名社交系统,包括IOS、Andriod、WEB管理端以及服务端.zip (1321个子文件)
.babelrc 337B
index.css 192KB
index.css 192KB
index.css 192KB
index.css 192KB
index.css 192KB
index.css 192KB
index.css 192KB
index.css 192KB
index.css 192KB
index.css 192KB
index.css 192KB
index.css 192KB
app.css 192KB
app.css 192KB
ueditor.css 43KB
ueditor.css 43KB
ueditor.css 43KB
ueditor.min.css 34KB
ueditor.min.css 34KB
ueditor.min.css 34KB
video-js.css 21KB
video-js.css 21KB
video-js.css 21KB
image.css 18KB
image.css 18KB
image.css 18KB
video.css 15KB
video.css 15KB
video.css 15KB
attachment.css 14KB
attachment.css 14KB
attachment.css 14KB
video-js.min.css 11KB
video-js.min.css 11KB
video-js.min.css 11KB
shCoreDefault.css 7KB
shCoreDefault.css 7KB
shCoreDefault.css 7KB
scrawl.css 4KB
scrawl.css 4KB
scrawl.css 4KB
codemirror.css 3KB
codemirror.css 3KB
codemirror.css 3KB
charts.css 3KB
charts.css 3KB
charts.css 3KB
background.css 2KB
background.css 2KB
background.css 2KB
emotion.css 2KB
emotion.css 2KB
emotion.css 2KB
dialogbase.css 2KB
dialogbase.css 2KB
dialogbase.css 2KB
music.css 2KB
music.css 2KB
music.css 2KB
edittable.css 1KB
edittable.css 1KB
edittable.css 1KB
template.css 1KB
template.css 1KB
template.css 1KB
webuploader.css 515B
webuploader.css 515B
webuploader.css 515B
help.css 389B
help.css 389B
help.css 389B
iframe.css 41B
iframe.css 41B
iframe.css 41B
.editorconfig 147B
vjs.eot 3KB
vjs.eot 3KB
vjs.eot 3KB
.eslintignore 74B
.eslintrc 58B
wface.gif 49KB
wface.gif 49KB
wface.gif 49KB
jxface2.gif 40KB
jxface2.gif 40KB
jxface2.gif 40KB
yface.gif 28KB
yface.gif 28KB
yface.gif 28KB
bface.gif 27KB
bface.gif 27KB
bface.gif 27KB
icons.gif 20KB
icons.gif 20KB
icons.gif 20KB
file-icons.gif 20KB
file-icons.gif 20KB
file-icons.gif 20KB
file-icons.gif 20KB
共 1321 条
- 1
- 2
- 3
- 4
- 5
- 6
- 14
资源评论
妄北y
- 粉丝: 9606
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功