import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.nio.charset.StandardCharsets; import org.apache.commons.io.IOUtils; import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.commons.lang3.StringUtils; import cn.zsmy.constant.BaseConstant; /** * @param path * @param key * @param value * @return */ public static String getHttp(String path, String key, String value){ HttpURLConnection httpURLConnection = null; InputStream bis = null; ByteArrayOutputStream bos = null; try { URL url = new URL(path); //打開連接 httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestProperty("content-Type", "application/json"); httpURLConnection.setRequestProperty("charset", "utf-8"); if(key != null && value != null){ httpURLConnection.setRequestProperty(key, value); } BaseConstant.MY_LOG.info("===getHttp==httpURLConnection.getResponseCode()===" + httpURLConnection.getResponseCode()); if(200 == httpURLConnection.getResponseCode()){ //得到輸入流 bis = httpURLConnection.getInputStream(); bos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while(-1 != (len = bis.read(buffer))){ bos.write(buffer,0,len); bos.flush(); } return bos.toString("utf-8"); } } catch (IOException e) { e.printStackTrace(); } finally { if(httpURLConnection != null){ httpURLConnection.disconnect(); } if(bis != null){ try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if(bos != null){ try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } /** * POST請求獲取數據 */ public static String postHttp(String path, String params){ URL url = null; HttpURLConnection httpURLConnection = null; BufferedInputStream bis = null; ByteArrayOutputStream bos = null; try { url = new URL(path); httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("POST");// 提交模式 httpURLConnection.setRequestProperty("content-Type", "application/json"); //httpURLConnection.setRequestProperty("charset", "utf-8"); // conn.setConnectTimeout(10000);//連接超時 單位毫秒 // conn.setReadTimeout(2000);//讀取超時 單位毫秒 // 發送POST請求必須設置如下兩行 httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); // 獲取URLConnection對象對應的輸出流 PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream()); // 發送請求參數 if(!StringUtils.isEmpty(params)){ printWriter.write(params);//post的參數 xx=xx&yy=yy } // flush輸出流的緩沖 printWriter.flush(); BaseConstant.MY_LOG.info("===postHttp==httpURLConnection.getResponseCode()===" + httpURLConnection.getResponseCode()); //開始獲取數據 bis = new BufferedInputStream(httpURLConnection.getInputStream()); bos = new ByteArrayOutputStream(); int len; byte[] arr = new byte[1024]; while((len=bis.read(arr))!= -1){ bos.write(arr,0,len); bos.flush(); } bos.close(); return bos.toString(BaseConstant.CHARSET_UTF8); } catch (Exception e) { e.printStackTrace(); } finally { if(httpURLConnection != null){ httpURLConnection.disconnect(); } if(bis != null){ try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if(bos != null){ try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }
