簡(jiǎn)單介紹Java網(wǎng)絡(luò)編程中的HTTP請(qǐ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問(wèn),點(diǎn)超鏈接訪問(wèn)等都是get,用戶如想把請(qǐng)求方式改為post,可通過(guò)更改表單的提交方式實(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ù)容量通常不能超過(guò)1K。
如果請(qǐng)求方式為POST方式,則可以在請(qǐng)求的實(shí)體內(nèi)容中向服務(wù)器發(fā)送數(shù)據(jù),Post方式的特點(diǎn):傳送的數(shù)據(jù)量無(wú)限制。
HTTP請(qǐng)求的細(xì)節(jié)——消息頭
HTTP請(qǐng)求中的常用消息頭
accept:瀏覽器通過(guò)這個(gè)頭告訴服務(wù)器,它所支持的數(shù)據(jù)類型
Accept-Charset: 瀏覽器通過(guò)這個(gè)頭告訴服務(wù)器,它支持哪種字符集
Accept-Encoding:瀏覽器通過(guò)這個(gè)頭告訴服務(wù)器,支持的壓縮格式
Accept-Language:瀏覽器通過(guò)這個(gè)頭告訴服務(wù)器,它的語(yǔ)言環(huán)境
Host:瀏覽器通過(guò)這個(gè)頭告訴服務(wù)器,想訪問(wèn)哪臺(tái)主機(jī)
If-Modified-Since: 瀏覽器通過(guò)這個(gè)頭告訴服務(wù)器,緩存數(shù)據(jù)的時(shí)間
Referer:瀏覽器通過(guò)這個(gè)頭告訴服務(wù)器,客戶機(jī)是哪個(gè)頁(yè)面來(lái)的 防盜鏈
Connection:瀏覽器通過(guò)這個(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); } }
相關(guān)文章
java的jdk基礎(chǔ)知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于java的jdk基礎(chǔ)知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。2021-01-01Java初學(xué)者問(wèn)題圖解(動(dòng)力節(jié)點(diǎn)Java學(xué)院整理)
本文通過(guò)圖文并茂的形式給大家介紹了java初學(xué)者問(wèn)題,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-04-04Java安全框架——Shiro的使用詳解(附springboot整合Shiro的demo)
這篇文章主要介紹了Java安全框架——Shiro的使用詳解,幫助大家更好的理解和學(xué)習(xí)使用Shiro,感興趣的朋友可以了解下2021-04-04Java中實(shí)體類為什么要實(shí)現(xiàn)Serializable序列化的作用
這篇文章主要介紹了Java中實(shí)體類為什么要實(shí)現(xiàn)Serializable序列化的作用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Java結(jié)構(gòu)型設(shè)計(jì)模式之裝飾模式詳解
裝飾模式(Decorator Pattern)允許向一個(gè)現(xiàn)有的對(duì)象添加新的功能,同時(shí)又不改變其結(jié)構(gòu)。這種類型的設(shè)計(jì)模式屬于結(jié)構(gòu)型模式,它是作為現(xiàn)有類的一個(gè)包裝。這種模式創(chuàng)建了一個(gè)裝飾類,用來(lái)包裝原有的類,并在保持類方法簽名完整性的前提下,提供了額外的功能2023-03-03SpringMVC中Model和ModelAndView的EL表達(dá)式取值方法
下面小編就為大家分享一篇SpringMVC中Model和ModelAndView的EL表達(dá)式取值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03一個(gè)簡(jiǎn)單的java學(xué)生寢室查詢系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了一個(gè)簡(jiǎn)單的java學(xué)生寢室查詢系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10