Commit 0bbecbd1 authored by yanzg's avatar yanzg

下载视频

parent fb329a78
...@@ -32,6 +32,106 @@ public class HttpHelper { ...@@ -32,6 +32,106 @@ public class HttpHelper {
*/ */
public static final String CHARSET_DEFAULT = "UTF-8"; public static final String CHARSET_DEFAULT = "UTF-8";
/**
* 获取请求参数
*
* @param obj 对象
* @return
*/
public static String getUrlParameter(Object obj) {
return getUrlParameter(obj, "UTF-8");
}
/**
* 获取请求参数
*
* @param obj 对象
* @return
*/
public static String getUrlParameter(Object obj, String charset) {
MapRow to = JsonHelper.to(obj, MapRow.class);
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, Object> kvp : to.entrySet()) {
if (sb.length() > 0) {
sb.append("&");
}
sb.append(UrlHelper.encoding(kvp.getKey(), charset));
sb.append("=");
sb.append(UrlHelper.encoding(StringHelper.toString(kvp.getValue()), charset));
}
return sb.toString();
}
/**
* 获取连接
*
* @param url 请求地址
* @return
* @throws IOException
*/
public static HttpURLConnection getConn(String url) throws IOException {
return getConn(url, null, false);
}
/**
* 获取连接
*
* @param url 请求地址
* @param header 请求头
* @return
* @throws IOException
*/
public static HttpURLConnection getConn(String url, Map<String, String> header) throws IOException {
return getConn(url, header, false);
}
/**
* 获取连接
*
* @param url 请求地址
* @param isApplicationJson 是否请求json
* @return
* @throws IOException
*/
public static HttpURLConnection getConn(String url, boolean isApplicationJson) throws IOException {
return getConn(url, null, isApplicationJson);
}
/**
* 获取连接
*
* @param url 请求地址
* @param header 请求头
* @param isApplicationJson 是否请求json
* @return
* @throws IOException
*/
public static HttpURLConnection getConn(String url, Map<String, String> header, boolean isApplicationJson) throws IOException {
// 创建URL对象
java.net.URL connURL = new java.net.URL(url);
// 打开URL连接
java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) connURL.openConnection();
// 设置通用属性
httpConn.setRequestProperty("Accept", "*/*");
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
if (isApplicationJson) {
httpConn.setRequestProperty("Content-Type", "application/json");
}
// 处理自定义头
if (header != null) {
for (Map.Entry<String, String> item : header.entrySet()) {
httpConn.setRequestProperty(item.getKey(), item.getValue());
}
}
// 设置POST方式
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
httpConn.setConnectTimeout(HTTP_CONNECT_TIMEOUT);
httpConn.setReadTimeout(HTTP_READ_TIMEOUT);
return httpConn;
}
/** /**
* 发送POST请求,当请求失败时,抛出异常或返回空字符串 * 发送POST请求,当请求失败时,抛出异常或返回空字符串
* *
...@@ -86,68 +186,13 @@ public class HttpHelper { ...@@ -86,68 +186,13 @@ public class HttpHelper {
public static String postApplicationJSON(String url, String jsonString) { public static String postApplicationJSON(String url, String jsonString) {
try { try {
// 打开URL连接 // 打开URL连接
java.net.HttpURLConnection httpConn = getConn(url); java.net.HttpURLConnection httpConn = getConn(url, true);
httpConn.setRequestProperty("Content-Type", "application/json");
return post(httpConn, jsonString, "UTF-8"); return post(httpConn, jsonString, "UTF-8");
} catch (Exception ex) { } catch (Exception ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
} }
/**
* 获取请求参数
*
* @param obj 对象
* @return
*/
public static String getUrlParameter(Object obj) {
return getUrlParameter(obj, "UTF-8");
}
/**
* 获取请求参数
*
* @param obj 对象
* @return
*/
public static String getUrlParameter(Object obj, String charset) {
MapRow to = JsonHelper.to(obj, MapRow.class);
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, Object> kvp : to.entrySet()) {
if (sb.length() > 0) {
sb.append("&");
}
sb.append(UrlHelper.encoding(kvp.getKey(), charset));
sb.append("=");
sb.append(UrlHelper.encoding(StringHelper.toString(kvp.getValue()), charset));
}
return sb.toString();
}
/**
* 获取连接
*
* @param url
* @return
* @throws IOException
*/
private static HttpURLConnection getConn(String url) throws IOException {
// 创建URL对象
java.net.URL connURL = new java.net.URL(url);
// 打开URL连接
java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) connURL.openConnection();
// 设置通用属性
httpConn.setRequestProperty("Accept", "*/*");
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
// 设置POST方式
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
httpConn.setConnectTimeout(HTTP_CONNECT_TIMEOUT);
httpConn.setReadTimeout(HTTP_READ_TIMEOUT);
return httpConn;
}
/** /**
* 发送POST请求,当请求失败时,抛出异常或返回空字符串 * 发送POST请求,当请求失败时,抛出异常或返回空字符串
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment