国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

簡(jiǎn)單介紹Java網(wǎng)絡(luò)編程中的HTTP請(qǐng)求

2019-11-26 14:58:39
字體:
供稿:網(wǎng)友

HTTP請(qǐng)求的細(xì)節(jié)――請(qǐng)求行
 
  請(qǐng)求行中的GET稱之為請(qǐng)求方式,請(qǐng)求方式有:POST、GET、HEAD、OPTIONS、DELETE、TRACE、PUT,常用的有: GET、 POST
  用戶如果沒有設(shè)置,默認(rèn)情況下瀏覽器向服務(wù)器發(fā)送的都是get請(qǐng)求,例如在瀏覽器直接輸?shù)刂吩L問,點(diǎn)超鏈接訪問等都是get,用戶如想把請(qǐng)求方式改為post,可通過更改表單的提交方式實(shí)現(xiàn)。
  不管POST或GET,都用于向服務(wù)器請(qǐng)求某個(gè)WEB資源,這兩種方式的區(qū)別主要表現(xiàn)在數(shù)據(jù)傳遞上:如果請(qǐng)求方式為GET方式,則可以在請(qǐng)求的URL地址后以?的形式帶上交給服務(wù)器的數(shù)據(jù),多個(gè)數(shù)據(jù)之間以&進(jìn)行分隔,例如:GET /mail/1.html?name=abc&password=xyz HTTP/1.1
  GET方式的特點(diǎn):在URL地址后附帶的參數(shù)是有限制的,其數(shù)據(jù)容量通常不能超過1K。
  如果請(qǐng)求方式為POST方式,則可以在請(qǐng)求的實(shí)體內(nèi)容中向服務(wù)器發(fā)送數(shù)據(jù),Post方式的特點(diǎn):傳送的數(shù)據(jù)量無限制。
 
HTTP請(qǐng)求的細(xì)節(jié)――消息頭

 
  HTTP請(qǐng)求中的常用消息頭
 
  accept:瀏覽器通過這個(gè)頭告訴服務(wù)器,它所支持的數(shù)據(jù)類型
  Accept-Charset: 瀏覽器通過這個(gè)頭告訴服務(wù)器,它支持哪種字符集
  Accept-Encoding:瀏覽器通過這個(gè)頭告訴服務(wù)器,支持的壓縮格式
  Accept-Language:瀏覽器通過這個(gè)頭告訴服務(wù)器,它的語(yǔ)言環(huán)境
  Host:瀏覽器通過這個(gè)頭告訴服務(wù)器,想訪問哪臺(tái)主機(jī)
  If-Modified-Since: 瀏覽器通過這個(gè)頭告訴服務(wù)器,緩存數(shù)據(jù)的時(shí)間
  Referer:瀏覽器通過這個(gè)頭告訴服務(wù)器,客戶機(jī)是哪個(gè)頁(yè)面來的  防盜鏈
  Connection:瀏覽器通過這個(gè)頭告訴服務(wù)器,請(qǐng)求完后是斷開鏈接還是何持鏈接

例:

http_get

import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL;   public class Http_Get {    private static String URL_PATH = "http://192.168.1.125:8080/myhttp/pro1.png";    public Http_Get() {     // TODO Auto-generated constructor stub   }    public static void saveImageToDisk() {     InputStream inputStream = getInputStream();     byte[] data = new byte[1024];     int len = 0;     FileOutputStream fileOutputStream = null;     try {       fileOutputStream = new FileOutputStream("C://test.png");       while ((len = inputStream.read(data)) != -1) {         fileOutputStream.write(data, 0, len);       }     } catch (IOException e) {       // TODO Auto-generated catch block       e.printStackTrace();     } finally {       if (inputStream != null) {         try {           inputStream.close();         } catch (IOException e) {           // TODO Auto-generated catch block           e.printStackTrace();         }       }       if (fileOutputStream != null) {         try {           fileOutputStream.close();         } catch (IOException e) {           // TODO Auto-generated catch block           e.printStackTrace();         }       }     }   }    /**    * 獲得服務(wù)器端的數(shù)據(jù),以InputStream形式返回    * @return    */   public static InputStream getInputStream() {     InputStream inputStream = null;     HttpURLConnection httpURLConnection = null;     try {       URL url = new URL(URL_PATH);       if (url != null) {         httpURLConnection = (HttpURLConnection) url.openConnection();         // 設(shè)置連接網(wǎng)絡(luò)的超時(shí)時(shí)間         httpURLConnection.setConnectTimeout(3000);         httpURLConnection.setDoInput(true);         // 表示設(shè)置本次http請(qǐng)求使用GET方式請(qǐng)求         httpURLConnection.setRequestMethod("GET");         int responseCode = httpURLConnection.getResponseCode();         if (responseCode == 200) {           // 從服務(wù)器獲得一個(gè)輸入流           inputStream = httpURLConnection.getInputStream();         }       }     } catch (MalformedURLException e) {       // TODO Auto-generated catch block       e.printStackTrace();     } catch (IOException e) {       // TODO Auto-generated catch block       e.printStackTrace();     }     return inputStream;   }    public static void main(String[] args) {     // 從服務(wù)器獲得圖片保存到本地     saveImageToDisk();   } } 

Http_Post

import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map;  public class Http_Post {    // 請(qǐng)求服務(wù)器端的url   private static String PATH = "http://192.168.1.125:8080/myhttp/servlet/LoginAction";   private static URL url;    public Http_Post() {     // TODO Auto-generated constructor stub   }    static {     try {       url = new URL(PATH);     } catch (MalformedURLException e) {       // TODO Auto-generated catch block       e.printStackTrace();     }   }    /**    * @param params    *      填寫的url的參數(shù)    * @param encode    *      字節(jié)編碼    * @return    */   public static String sendPostMessage(Map<String, String> params,       String encode) {     // 作為StringBuffer初始化的字符串     StringBuffer buffer = new StringBuffer();     try {       if (params != null && !params.isEmpty()) {          for (Map.Entry<String, String> entry : params.entrySet()) {             // 完成轉(zhuǎn)碼操作             buffer.append(entry.getKey()).append("=").append(                 URLEncoder.encode(entry.getValue(), encode))                 .append("&");           }         buffer.deleteCharAt(buffer.length() - 1);       }       // System.out.println(buffer.toString());       // 刪除掉最有一個(gè)&              System.out.println("-->>"+buffer.toString());       HttpURLConnection urlConnection = (HttpURLConnection) url           .openConnection();       urlConnection.setConnectTimeout(3000);       urlConnection.setRequestMethod("POST");       urlConnection.setDoInput(true);// 表示從服務(wù)器獲取數(shù)據(jù)       urlConnection.setDoOutput(true);// 表示向服務(wù)器寫數(shù)據(jù)       // 獲得上傳信息的字節(jié)大小以及長(zhǎng)度       byte[] mydata = buffer.toString().getBytes();       // 表示設(shè)置請(qǐng)求體的類型是文本類型       urlConnection.setRequestProperty("Content-Type",           "application/x-www-form-urlencoded");       urlConnection.setRequestProperty("Content-Length",           String.valueOf(mydata.length));       // 獲得輸出流,向服務(wù)器輸出數(shù)據(jù)       OutputStream outputStream = urlConnection.getOutputStream();       outputStream.write(mydata,0,mydata.length);       outputStream.close();       // 獲得服務(wù)器響應(yīng)的結(jié)果和狀態(tài)碼       int responseCode = urlConnection.getResponseCode();       if (responseCode == 200) {         return changeInputStream(urlConnection.getInputStream(), encode);       }     } catch (UnsupportedEncodingException e) {       // TODO Auto-generated catch block       e.printStackTrace();     } catch (IOException e) {       // TODO Auto-generated catch block       e.printStackTrace();     }     return "";   }    /**    * 將一個(gè)輸入流轉(zhuǎn)換成指定編碼的字符串    *    * @param inputStream    * @param encode    * @return    */   private static String changeInputStream(InputStream inputStream,       String encode) {     // TODO Auto-generated method stub     ByteArrayOutputStream outputStream = new ByteArrayOutputStream();     byte[] data = new byte[1024];     int len = 0;     String result = "";     if (inputStream != null) {       try {         while ((len = inputStream.read(data)) != -1) {           outputStream.write(data, 0, len);         }         result = new String(outputStream.toByteArray(), encode);       } catch (IOException e) {         // TODO Auto-generated catch block         e.printStackTrace();       }     }     return result;   }    /**    * @param args    */   public static void main(String[] args) {     // TODO Auto-generated method stub     Map<String, String> params = new HashMap<String, String>();     params.put("username", "admin");     params.put("password", "123");     String result = Http_Post.sendPostMessage(params, "utf-8");     System.out.println("--result->>" + result);   }  } 


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 横峰县| 玛曲县| 方城县| 双城市| 阳新县| 灵寿县| 黄冈市| 太谷县| 都昌县| 贵港市| 琼海市| 德格县| 杂多县| 无极县| 沙坪坝区| 青铜峡市| 黎川县| 白城市| 揭西县| 泗洪县| 政和县| 阿克| 南阳市| 万盛区| 南华县| 孝义市| 张家口市| 阿拉善盟| 翼城县| 高尔夫| 北碚区| 新沂市| 石楼县| 富顺县| 望都县| 吉首市| 宜川县| 怀远县| 长泰县| 北京市| 边坝县|