話很少說 , 直接上代碼 (沒有依賴jar) java
1 import java.io.ByteArrayOutputStream; 2 import java.io.IOException; 3 import java.io.InputStream; 4 import java.io.OutputStream; 5 import java.net.HttpURLConnection; 6 import java.net.URL; 7 8 9 public class HttpTest { 10 11 12 public static void main(String[] args) { 13 String path = "http://127.0.0.1:8080/demo/test.servlet" ; 14 // String path = "https://www.baidu.com/" ;//只能支持get請求 15 16 HttpURLConnection httpURLConnection = null ; 17 ByteArrayOutputStream byteArrayOutputStream = null ; 18 OutputStream outputStream = null ; 19 InputStream inputStream = null ; 20 URL url = null ; 21 22 //從輸出流中獲取讀取到數據(服務端返回的) 23 String message = null ; 24 try { 25 //請求的url 26 url = new URL(path); 27 28 //建立http連接 29 httpURLConnection = (HttpURLConnection) url.openConnection(); 30 31 //設置超時時間 32 httpURLConnection.setConnectTimeout(5000);// 5 秒 33 httpURLConnection.setReadTimeout(5000); 34 35 //設置請求的方法類型 36 httpURLConnection.setRequestMethod("POST"); 37 38 //設置通用的請求屬性 設置請求格式 39 httpURLConnection.setRequestProperty("contentType", "utf-8"); 40 httpURLConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded"); 41 42 //設置發送數據 43 httpURLConnection.setDoOutput(true); 44 //設置接受數據 45 httpURLConnection.setDoInput(true); 46 47 //發送數據,使用輸出流 48 outputStream = httpURLConnection.getOutputStream(); 49 outputStream.write(new String("name=zhangshang&pwd=123").getBytes()); 50 outputStream.flush() ; 51 // //發送數據 52 if (httpURLConnection.getResponseCode() == 200) {//成功 53 //接收數據 54 inputStream = httpURLConnection.getInputStream(); 55 //定義字節數組 56 byte[] b = new byte[1024]; 57 //定義一個輸出流存儲接收到的數據 58 byteArrayOutputStream = new ByteArrayOutputStream(); 59 //開始接收數據 60 int len = 0; 61 while (true) { 62 len = inputStream.read(b); 63 if (len == -1) { 64 //數據讀完 65 break; 66 } 67 byteArrayOutputStream.write(b, 0, len); 68 } 69 }else{//失敗 70 System.err.println("異常HTTP狀態碼 : "+httpURLConnection.getResponseCode()); 71 //接收數據 72 inputStream = httpURLConnection.getInputStream(); 73 //定義字節數組 74 byte[] b = new byte[1024]; 75 //定義一個輸出流存儲接收到的數據 76 byteArrayOutputStream = new ByteArrayOutputStream(); 77 //開始接收數據 78 int len = 0; 79 while (true) { 80 len = inputStream.read(b); 81 if (len == -1) { 82 //數據讀完 83 break; 84 } 85 byteArrayOutputStream.write(b, 0, len); 86 } 87 88 } 89 message = byteArrayOutputStream.toString("utf-8"); 90 } catch (Exception e) { 91 e.printStackTrace(); 92 } finally{ 93 if(null != byteArrayOutputStream){ 94 try { 95 byteArrayOutputStream.close() ; 96 } catch (IOException e) { 97 e.printStackTrace(); 98 } 99 } 100 if(null != inputStream){ 101 try { 102 inputStream.close() ; 103 } catch (IOException e) { 104 e.printStackTrace(); 105 } 106 } 107 if(null != outputStream){ 108 try { 109 outputStream.close() ; 110 } catch (IOException e) { 111 e.printStackTrace(); 112 } 113 } 114 if(null != httpURLConnection){ 115 httpURLConnection.disconnect() ; 116 } 117 } 118 System.out.println(message); 119 } 120 }
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");數組
設置是否向httpUrlConnection輸出,設置是否從httpUrlConnection讀入,此外發送post請求必須設置這兩個最經常使用的Http請求無非是get和post,get請求能夠獲取靜態頁面,也能夠把參數放在URL字串後面,傳遞給servlet,post與get的 不一樣之處在於post的參數不是放在URL字串裏面,而是放在http請求的正文內。多線程
conn.setDoOutput(true);
conn.setDoInput(true);app
發送http請求而後返回的方法還有不少種 , 我知道有一種叫 httpclient 的jar包 , 封裝很完善 , 很好使 ...socket