|
|
@@ -1,19 +1,10 @@
|
|
|
package me.zhengjie.base.util;
|
|
|
|
|
|
-import java.io.BufferedReader;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.FileNotFoundException;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.io.InputStreamReader;
|
|
|
-
|
|
|
-import java.io.PrintWriter;
|
|
|
-import java.net.URL;
|
|
|
-import java.net.URLConnection;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.io.*;
|
|
|
+
|
|
|
+import java.net.*;
|
|
|
+import java.text.MessageFormat;
|
|
|
+import java.util.*;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
|
@@ -27,15 +18,11 @@ public class HttpUtil {
|
|
|
*/
|
|
|
private static int DEFAULT_TIME_OUT = 1800000000;
|
|
|
|
|
|
- //
|
|
|
-
|
|
|
/**
|
|
|
* jdk 原生 http post 请求,默认超时时间为30秒
|
|
|
*
|
|
|
* @param url 请求地址
|
|
|
- * @param body 入参String
|
|
|
- * @param contentType 内容类型
|
|
|
- * @param charSet 编码格式
|
|
|
+ * @param json 入参String
|
|
|
* @return respone的body
|
|
|
*/
|
|
|
public static String sendPost(String url, String json) {
|
|
|
@@ -47,8 +34,6 @@ public class HttpUtil {
|
|
|
*
|
|
|
* @param url 访问地址
|
|
|
* @param body request请求体
|
|
|
- * @param contentType 请求类型
|
|
|
- * @param charSet 编码格式
|
|
|
* @param timeOut 超时时间,单位毫秒
|
|
|
* @return 响应体string
|
|
|
*/
|
|
|
@@ -66,8 +51,6 @@ public class HttpUtil {
|
|
|
conn.setRequestProperty("charsert", "utf-8");
|
|
|
conn.setConnectTimeout(timeOut);
|
|
|
conn.setReadTimeout(timeOut);
|
|
|
- //
|
|
|
-
|
|
|
// 发送POST请求必须设置如下两行
|
|
|
conn.setDoOutput(true);
|
|
|
conn.setDoInput(true);
|
|
|
@@ -121,6 +104,215 @@ public class HttpUtil {
|
|
|
return stringBuilder.toString();
|
|
|
}
|
|
|
|
|
|
+ /***
|
|
|
+ * 向指定URL发送POST方法的请求
|
|
|
+ *
|
|
|
+ * @param apiUrl
|
|
|
+ * @param data
|
|
|
+ * @param headers
|
|
|
+ * @param encoding
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static String sendPOST(String apiUrl, String data, LinkedHashMap<String, String> headers, String encoding) throws Exception {
|
|
|
+ // 获得响应内容
|
|
|
+ String http_RespContent = null;
|
|
|
+ HttpURLConnection httpURLConnection = null;
|
|
|
+ int http_StatusCode = 0;
|
|
|
+ String http_RespMessage = null;
|
|
|
+ try {
|
|
|
+ // 建立连接
|
|
|
+ URL url = new URL(apiUrl);
|
|
|
+ httpURLConnection = (HttpURLConnection) url.openConnection();
|
|
|
+ // 需要输出
|
|
|
+ httpURLConnection.setDoOutput(true);
|
|
|
+ // 需要输入
|
|
|
+ httpURLConnection.setDoInput(true);
|
|
|
+ // 不允许缓存
|
|
|
+ httpURLConnection.setUseCaches(false);
|
|
|
+ // HTTP请求方式
|
|
|
+ httpURLConnection.setRequestMethod("POST");
|
|
|
+ // 设置Headers
|
|
|
+ if (null != headers) {
|
|
|
+ for (String key : headers.keySet()) {
|
|
|
+ httpURLConnection.setRequestProperty(key, headers.get(key));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 连接会话
|
|
|
+ httpURLConnection.connect();
|
|
|
+ if (data != null) {
|
|
|
+ // 建立输入流,向指向的URL传入参数
|
|
|
+ DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream());
|
|
|
+ // 设置请求参数
|
|
|
+ dos.write(data.getBytes(encoding));
|
|
|
+ dos.flush();
|
|
|
+ dos.close();
|
|
|
+ }
|
|
|
+ // 获得响应状态(HTTP状态码)
|
|
|
+ http_StatusCode = httpURLConnection.getResponseCode();
|
|
|
+ // 获得响应消息(HTTP状态码描述)
|
|
|
+ http_RespMessage = httpURLConnection.getResponseMessage();
|
|
|
+ // 获得响应内容
|
|
|
+ if (HttpURLConnection.HTTP_OK == http_StatusCode) {
|
|
|
+ // 返回响应结果
|
|
|
+ http_RespContent = getResponseContent(httpURLConnection);
|
|
|
+ } else {
|
|
|
+ // 返回非200状态时响应结果
|
|
|
+ http_RespContent = getErrorResponseContent(httpURLConnection);
|
|
|
+ String msg =
|
|
|
+ MessageFormat.format("请求失败: Http状态码 = {0} , {1}", http_StatusCode, http_RespMessage);
|
|
|
+ System.out.println(msg);
|
|
|
+ }
|
|
|
+ } catch (UnknownHostException e) {
|
|
|
+ String message = MessageFormat.format("网络请求时发生异常: {0}", e.getMessage());
|
|
|
+ Exception ex = new Exception(message);
|
|
|
+ ex.initCause(e);
|
|
|
+ throw ex;
|
|
|
+ } catch (MalformedURLException e) {
|
|
|
+ String message = MessageFormat.format("格式错误的URL: {0}", e.getMessage());
|
|
|
+ Exception ex = new Exception(message);
|
|
|
+ ex.initCause(e);
|
|
|
+ throw ex;
|
|
|
+ } catch (IOException e) {
|
|
|
+ String message = MessageFormat.format("网络请求时发生异常: {0}", e.getMessage());
|
|
|
+ Exception ex = new Exception(message);
|
|
|
+ ex.initCause(e);
|
|
|
+ throw ex;
|
|
|
+ } catch (Exception e) {
|
|
|
+ String message = MessageFormat.format("网络请求时发生异常: {0}", e.getMessage());
|
|
|
+ Exception ex = new Exception(message);
|
|
|
+ ex.initCause(e);
|
|
|
+ throw ex;
|
|
|
+ } finally {
|
|
|
+ if (null != httpURLConnection) {
|
|
|
+ httpURLConnection.disconnect();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return http_RespContent;
|
|
|
+ }
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 读取HttpResponse响应内容
|
|
|
+ *
|
|
|
+ * @param httpURLConnection
|
|
|
+ * @return
|
|
|
+ * @throws UnsupportedEncodingException
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ private static String getErrorResponseContent(HttpURLConnection httpURLConnection)
|
|
|
+ throws UnsupportedEncodingException, IOException {
|
|
|
+ StringBuffer contentBuffer = null;
|
|
|
+ BufferedReader responseReader = null;
|
|
|
+ try {
|
|
|
+ contentBuffer = new StringBuffer();
|
|
|
+ String readLine = null;
|
|
|
+ responseReader =
|
|
|
+ new BufferedReader(new InputStreamReader(httpURLConnection.getErrorStream(), "UTF-8"));
|
|
|
+ while ((readLine = responseReader.readLine()) != null) {
|
|
|
+ contentBuffer.append(readLine);
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ if (null != responseReader) {
|
|
|
+ responseReader.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return contentBuffer.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 读取HttpResponse响应内容
|
|
|
+ *
|
|
|
+ * @param httpURLConnection
|
|
|
+ * @return
|
|
|
+ * @throws UnsupportedEncodingException
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ private static String getResponseContent(HttpURLConnection httpURLConnection)
|
|
|
+ throws UnsupportedEncodingException, IOException {
|
|
|
+ StringBuffer contentBuffer = null;
|
|
|
+ BufferedReader responseReader = null;
|
|
|
+ try {
|
|
|
+ contentBuffer = new StringBuffer();
|
|
|
+ String readLine = null;
|
|
|
+ responseReader =
|
|
|
+ new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "UTF-8"));
|
|
|
+ while ((readLine = responseReader.readLine()) != null) {
|
|
|
+ contentBuffer.append(readLine);
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ if (null != responseReader) {
|
|
|
+ responseReader.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return contentBuffer.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传文件
|
|
|
+ *
|
|
|
+ * @param url
|
|
|
+ * @param header
|
|
|
+ * @param filePath
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String postFile(String url, Map<String, String> header, String filePath) {
|
|
|
+ HttpURLConnection conn = null;
|
|
|
+ OutputStream out = null;
|
|
|
+ String respContent = "";
|
|
|
+ try {
|
|
|
+ URL realUrl = new URL(url);
|
|
|
+ // 打开和URL之间的连接
|
|
|
+ conn = (HttpURLConnection) realUrl.openConnection();
|
|
|
+ // 设置通用的请求头属性
|
|
|
+ conn.setRequestProperty("Content-Type", header.get("contentType"));
|
|
|
+ conn.setRequestProperty("Content-MD5", header.get("contentMD5"));
|
|
|
+ // 发送POST请求必须设置如下两行
|
|
|
+ conn.setDoOutput(true);
|
|
|
+ conn.setDoInput(true);
|
|
|
+ conn.setRequestMethod("PUT");
|
|
|
+ conn.connect();
|
|
|
+ // 上传文件内容
|
|
|
+ out = new DataOutputStream(conn.getOutputStream());
|
|
|
+ File file = new File(filePath);
|
|
|
+ DataInputStream fileIS = new DataInputStream(new FileInputStream(file));
|
|
|
+ int bytes = 0;
|
|
|
+ byte[] bufferOut = new byte[1024];
|
|
|
+ while ((bytes = fileIS.read(bufferOut)) != -1) {
|
|
|
+ out.write(bufferOut, 0, bytes);
|
|
|
+ }
|
|
|
+ fileIS.close();
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+ // 获取返回信息
|
|
|
+ int statusCode = conn.getResponseCode();
|
|
|
+ String respMessage = conn.getResponseMessage();
|
|
|
+ // 获得响应内容
|
|
|
+ if (HttpURLConnection.HTTP_OK == statusCode) {
|
|
|
+ // 返回响应结果
|
|
|
+ respContent = getResponseContent(conn);
|
|
|
+ } else {
|
|
|
+ // 返回非200状态时响应结果
|
|
|
+ respContent = getErrorResponseContent(conn);
|
|
|
+ String msg = MessageFormat.format("请求失败: Http状态码 = {0} , {1}", statusCode, respMessage);
|
|
|
+ System.out.println(msg);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (out != null) {
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+ if (conn != null) {
|
|
|
+ conn.disconnect();
|
|
|
+ }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return respContent;
|
|
|
+ }
|
|
|
+
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
|
int i = 0;
|