1 package com.baqingshe.bjs.util; 2 3 import java.io.BufferedReader; 4 5 import java.io.IOException; 6 7 import java.io.InputStream; 8 9 import java.io.InputStreamReader; 10 11 import java.io.PrintWriter; 12 13 import java.net.URL; 14 15 import java.net.URLConnection; 16 17 import java.util.List; 18 19 import java.util.Map; 20 21 import sun.net.www.protocol.http.HttpURLConnection; 22 23 public class HttpRequest { 24 25 public static String doGet(String url) throws Exception { 26 27 URL localURL = new URL(url); 28 29 URLConnection connection = localURL.openConnection(); 30 31 HttpURLConnection httpURLConnection = (HttpURLConnection)connection; 32 33 34 35 httpURLConnection.setRequestProperty("Accept-Charset", "utf-8"); 36 37 httpURLConnection.setRequestProperty("Content-Type", "application/json"); 38 39 40 41 InputStream inputStream = null; 42 43 InputStreamReader inputStreamReader = null; 44 45 BufferedReader reader = null; 46 47 StringBuffer resultBuffer = new StringBuffer(); 48 49 String tempLine = null; 50 51 52 53 if (httpURLConnection.getResponseCode() >= 300) { 54 55 throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode()); 56 57 } 58 59 60 61 try { 62 63 inputStream = httpURLConnection.getInputStream(); 64 65 inputStreamReader = new InputStreamReader(inputStream); 66 67 reader = new BufferedReader(inputStreamReader); 68 69 70 71 while ((tempLine = reader.readLine()) != null) { 72 73 resultBuffer.append(tempLine); 74 75 } 76 77 78 79 } finally { 80 81 82 83 if (reader != null) { 84 85 reader.close(); 86 87 } 88 89 90 91 if (inputStreamReader != null) { 92 93 inputStreamReader.close(); 94 95 } 96 97 98 99 if (inputStream != null) { 100 101 inputStream.close(); 102 103 } 104 105 106 107 } 108 109 110 111 return resultBuffer.toString(); 112 113 } 114 115 116 117 public static String sendPost(String url, String param) { 118 119 PrintWriter out = null; 120 121 BufferedReader in = null; 122 123 String result = ""; 124 125 try { 126 127 URL realUrl = new URL(url); 128 129 // 開啟和URL之間的連線 130 131 URLConnection conn = realUrl.openConnection(); 132 133 // 設定通用的請求屬性 134 135 conn.setRequestProperty("accept", "*/*"); 136 137 conn.setRequestProperty("connection", "Keep-Alive"); 138 139 conn.setRequestProperty("user-agent", 140 141 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); 142 143 // 傳送POST請求必須設定如下兩行 144 145 conn.setDoOutput(true); 146 147 conn.setDoInput(true); 148 149 // 獲取URLConnection物件對應的輸出流 150 151 out = new PrintWriter(conn.getOutputStream()); 152 153 // 傳送請求引數 154 155 out.print(param); 156 157 // flush輸出流的緩衝 158 159 out.flush(); 160 161 // 定義BufferedReader輸入流來讀取URL的響應 162 163 in = new BufferedReader( 164 165 new InputStreamReader(conn.getInputStream())); 166 167 String line; 168 169 while ((line = in.readLine()) != null) { 170 171 result += line; 172 173 } 174 175 } catch (Exception e) { 176 177 System.out.println("傳送 POST 請求出現異常!"+e); 178 179 e.printStackTrace(); 180 181 } 182 183 //使用finally塊來關閉輸出流、輸入流 184 185 finally{ 186 187 try{ 188 189 if(out!=null){ 190 191 out.close(); 192 193 } 194 195 if(in!=null){ 196 197 in.close(); 198 199 } 200 201 } 202 203 catch(IOException ex){ 204 205 ex.printStackTrace(); 206 207 } 208 209 } 210 211 return result; 212 213 } 214 215 }