| | |
| | | import net.sf.json.JSONException; |
| | | import net.sf.json.JSONObject; |
| | | import org.apache.commons.io.IOUtils; |
| | | import org.bouncycastle.util.encoders.UTF8; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.activation.MimetypesFileTypeMap; |
| | |
| | | import java.io.*; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | import java.nio.charset.Charset; |
| | | import java.nio.charset.CharsetEncoder; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | } |
| | | in.close(); |
| | | System.err.println("result:" + result); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 带header的post请求 |
| | | * @param generalUrl |
| | | * @param header |
| | | * @param params |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static String post(String generalUrl, Map<String, String> header, String params) |
| | | throws Exception { |
| | | URL url = new URL(generalUrl); |
| | | // 打开和URL之间的连接 |
| | | HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| | | connection.setRequestMethod("POST"); |
| | | // 设置通用的请求属性 |
| | | connection.setRequestProperty("Connection", "Keep-Alive"); |
| | | if(header != null) |
| | | { |
| | | for(String key : header.keySet()) |
| | | { |
| | | connection.setRequestProperty(key, header.get(key)); |
| | | } |
| | | } |
| | | |
| | | connection.setUseCaches(false); |
| | | connection.setDoOutput(true); |
| | | connection.setDoInput(true); |
| | | |
| | | // 得到请求的输出流对象 |
| | | DataOutputStream out = new DataOutputStream(connection.getOutputStream()); |
| | | out.write(params.getBytes("UTF-8")); |
| | | out.flush(); |
| | | out.close(); |
| | | |
| | | // 建立实际的连接 |
| | | connection.connect(); |
| | | // 定义 BufferedReader输入流来读取URL的响应 |
| | | BufferedReader in = null; |
| | | in = new BufferedReader( |
| | | new InputStreamReader(connection.getInputStream(), "UTF-8")); |
| | | String result = ""; |
| | | String getLine; |
| | | while ((getLine = in.readLine()) != null) { |
| | | result += getLine; |
| | | } |
| | | in.close(); |
| | | |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 带header的get请求 |
| | | * @param generalUrl |
| | | * @param header |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static String get(String generalUrl, Map<String, String> header) |
| | | throws Exception { |
| | | URL url = new URL(generalUrl); |
| | | // 打开和URL之间的连接 |
| | | HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| | | connection.setRequestMethod("GET"); |
| | | // 设置通用的请求属性 |
| | | connection.setRequestProperty("Connection", "Keep-Alive"); |
| | | if(header != null) |
| | | { |
| | | for(String key : header.keySet()) |
| | | { |
| | | connection.setRequestProperty(key, header.get(key)); |
| | | } |
| | | } |
| | | |
| | | connection.setUseCaches(false); |
| | | connection.setDoOutput(true); |
| | | connection.setDoInput(true); |
| | | |
| | | // 建立实际的连接 |
| | | connection.connect(); |
| | | // 定义 BufferedReader输入流来读取URL的响应 |
| | | BufferedReader in = null; |
| | | in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); |
| | | String result = ""; |
| | | String getLine; |
| | | while ((getLine = in.readLine()) != null) { |
| | | result += getLine; |
| | | } |
| | | in.close(); |
| | | |
| | | return result; |
| | | } |
| | | |
| | |
| | | } |
| | | return res; |
| | | } |
| | | |
| | | } |