整合營銷服務(wù)商

          電腦端+手機(jī)端+微信端=數(shù)據(jù)同步管理

          免費(fèi)咨詢熱線:

          《極簡Java新手編程之道》15.2.2 HttpURLConnection類

          AWURLConnection類。

          下面來介紹使用RAWURLConnection類。由于URL類只能發(fā)送HTTP/HTTPS的GET方法請求,如果要想發(fā)送其他的情況或者對網(wǎng)絡(luò)請求有更深入的控制時,可以使用DN2URLConnection類。

          由于URL類只能發(fā)送HTTP/HTTPS的GET方法請求,如果要想發(fā)送其他的情況或者對網(wǎng)絡(luò)請求有更深入的控制時,可以使用DN2URLConnection類。

          有的時候為了保證數(shù)據(jù)的安全,使用ALPTV類請求進(jìn)行發(fā)送數(shù)據(jù),使用VGA這個類就不適合了。這種情況怎么辦?可以使用ALPTV類。

          使用GET請求返回HTML數(shù)據(jù)的示例。這個示例就使用了。剛才講到了ABWebWeb這個類,來看配套代碼,在這個地方使用GET請求返回HTML數(shù)據(jù)的示例。

          這個類獲得一個ALPOK連接對象。該如何去獲得呢?通過UTHO對象有QUESBSB插入WebWebWeb。

          這個方法之前提到過,給它打開一個連接,打開連接的類型強(qiáng)制性轉(zhuǎn)換成為ABWebWeb。有它之后,有了它ALPMA連接對象之后可以設(shè)置,要發(fā)送的請求的方法就是Esc請求方法,可以暫時進(jìn)行設(shè)置。

          這里面有一個QUEQUEE BOO方法,這個方法它里面參數(shù)給它一個GET,發(fā)送就是Esc,請求。然后通過HN有Java對象的MP4給它打開,打開這個流,然后打開這個流,又給它轉(zhuǎn)換成為字符流,將字節(jié)流轉(zhuǎn)換成為字符流,然后去讀數(shù)據(jù),讀數(shù)據(jù)數(shù)據(jù)的讀取的方式跟上面介紹的例子,沒有區(qū)別后面就不墜數(shù)了。

          這里需要注意的是,現(xiàn)在使用的是ALP打開,請求方法設(shè)置請求方法,來運(yùn)行一下這個例子。這就是介紹的實用DN2SBPE類。

          課程就介紹到這里,大家有什么問題可以加入我的服群,詳細(xì)請看課件最后一頁。

          先讓我們回顧下前端入門——html 超鏈接的用法 <a href="url"> , 超鏈接是一個非常偉大的發(fā)明,它鏈接了整個互聯(lián)網(wǎng),沒有它就沒有互聯(lián)網(wǎng)。

          超鏈接在發(fā)明之初就給它設(shè)計了一個默認(rèn)的樣式,就是藍(lán)色帶下劃線的樣式,如下圖:

          默認(rèn)樣式

          關(guān)于為什么超鏈接是藍(lán)色帶下劃線的歷史,可以參考這里:https://baijiahao.baidu.com/s?id=1710334071632114373&wfr=spider&for=pc

          當(dāng)網(wǎng)頁變得越來越豐富,用戶的需求越來越高的時候,這樣的樣式已經(jīng)不符合大眾的審美,所以通過css來美化超鏈接非常有用和有趣。

          設(shè)置鏈接樣式

          鏈接可以使用任何 CSS 屬性(例如 colorfont-familybackground 等)來設(shè)置樣式。

          如下圖:

          除此之外,可以根據(jù)鏈接狀態(tài)來設(shè)置不同樣式,鏈接狀態(tài)分別有:

          • a:link - 正常的,未訪問的鏈接
          • a:visited - 用戶訪問過的鏈接
          • a:hover - 用戶將鼠標(biāo)懸停在鏈接上時
          • a:active - 鏈接被點(diǎn)擊時

          如下示例:

          /* 未被訪問的鏈接 */
          a:link {
            color: red;
          }
          
          /* 已被訪問的鏈接 */
          a:visited {
            color: green;
          }
          
          /* 將鼠標(biāo)懸停在鏈接上 */
          a:hover {
            color: hotpink;
          }
          
          /* 被選擇的鏈接 */
          a:active {
            color: blue;
          }

          未被訪問的鏈接

          已被訪問的鏈接

          將鼠標(biāo)懸停在鏈接上

          被選擇的鏈接

          如果為多個鏈接狀態(tài)設(shè)置樣式,請遵循如下順序規(guī)則:

          • a:hover 必須 a:link 和 a:visited 之后
          • a:active 必須在 a:hover 之后

          以上是鏈接的各種偽類,描述了鏈接的不同狀態(tài),你可以試試在不同狀態(tài)下給設(shè)置不同的屬性,比如背景色,字體或者文本修飾等等。

          鏈接按鈕

          鏈接通常用來當(dāng)做按鈕使用,點(diǎn)擊它跳轉(zhuǎn)頁面或執(zhí)行一些事件或js函數(shù)。如下示例:

          a:link, a:visited {
            background-color: #f44336;
            color: white;
            padding: 14px 25px;
            text-align: center; 
            text-decoration: none;
            display: inline-block;
          }
          
          a:hover, a:active {
            background-color: red;
          }

          定義了一個背景色為紅色的按鈕,如下圖:

          注意:如果想在點(diǎn)擊這個按鈕后禁止跳轉(zhuǎn)頁面,可以給href屬性設(shè)置javascript:void();,在以后會講到JavaScript在網(wǎng)頁中的使用,這里你已經(jīng)看到,在href屬性中通過Javascript:的形式可以執(zhí)行一段js語句或函數(shù),這里void()就是阻止鏈接跳轉(zhuǎn)。

          上面只是一個簡單的例子,當(dāng)然你也可以制作更漂亮的按鈕,比如加上圓角、陰影、或漸變背景等。

          改變光標(biāo)

          當(dāng)鼠標(biāo)移動到鏈接上時,你會看到鼠標(biāo)變成一個手形,通過css cursor 屬性可以改變鼠標(biāo)指針的形狀,如下圖:

          可以嘗試按照上面的屬性依次練習(xí)一遍,鏈接的樣式到此就介紹完了,感謝關(guān)注。

          上篇:前端入門——css字體和文本

          天內(nèi)容好長,翻譯起來有點(diǎn)累。

          Preparations

          With the basics of HTTP out of the way, let's get acquainted with the tools we'll use in this book to demonstrate how HTTP works. This section goes through a few tools you'll need to follow along. Note that you only need one of the tools listed. It is important to note that you will be able to follow this book regardless of what tool you use, so pick one that you're comfortable with and get started!

          翻譯:

          準(zhǔn)備工作,我們先學(xué)習(xí)下工具來演練 HTTP怎么工作的。本章節(jié)介紹下一些工具,可以選擇一個你覺得比較合適的來上手。

          HTTP GUI Tools

          We'll make heavy use of Graphical HTTP tools throughout this book. There are lots of options available in this category of HTTP tools, and we'll be using Paw 3. Although it's a paid App in the Mac App Store, there is a limited Trial version as well which can get you through the book.

          That said, there are many other alternatives out there. Some other capable options are Insomnia and Postman; they're both free and they work on OS X, Windows, and Ubuntu.

          HTTP 的 圖形用戶界面工具

          我們這里用的是Paw3, 雖然他需要在蘋果商店買,但是也有短時間免費(fèi)版本。

          你也可以選擇 Insomnia 和 Postman (譯者之前裝過postman,這里就用Postman了)

          HTTP Command line Tools

          curl is a free command line tool that is used to issue HTTP requests.

          Mac OS X/Linux :

          It is shipped with OS X and most GNU/Linux distributions, and you can simply invoke it on the command line by issuing the command below.

          $ curl www.google.com

          If you have version 1803 or later of Windows 10, cURL should be installed by default. If you have an earlier version of windows or cannot find cURL on your version, we recommend installing a GUI version of cURL. You won't be able to execute the command line commands, but you should still be able to perform the appropriate actions in the GUI.

          curl 是一個免費(fèi)的工具,用來發(fā)送HTTP 請求,和MAC 及絕大多數(shù) Linux 系統(tǒng)原生綁定。

          你可以很簡單的啟動curl ,用下面命令行:

          $ curl www.google.com

          如果你有windows10的 1803 及以后版本,curl 也是默認(rèn)安裝好了,如果你有更早的版本,你可以考慮安裝一個,圖形界面的curl. 你雖然沒法執(zhí)行命令,但是可以在GUI 中執(zhí)行相關(guān)操作。

          Making HTTP Requests

          HTTP Request with a Browser

          Making an HTTP request is easy. Say you want to visit Reddit in your browser. All you need to do is launch your browser and enter the address https://www.reddit.com and

          this is a snapshot of what you might see:

          翻譯:

          做HTTP的申請

          通過一個瀏覽器做HTTP請求,做HTTP請求很容易,你可以在瀏覽器中訪問reddit ,直接鍵入地址https://www.reddit.com, 然后看到快照。



          The server that hosts the main Reddit website handles your request and issues a response back to your browser. Your browser is smart enough to process the response that is sent back and display the site you see in the screenshot, with all its colors, images, text and presentation.

          翻譯:

          承載著Reddit web主站的服務(wù)器負(fù)責(zé)處理請求,并返回一個response 到瀏覽器,瀏覽器足夠“聰明”可以處理返回的響應(yīng)文件,在屏幕上把文件展示出來,包含的是顏色、圖片、文字和展示。

          HTTP Request with an HTTP Tool

          Because browsers show us the processed version of the response, we don't get to see the raw response the server sent back. How do we see the raw HTTP response data?

          For that, we can use an HTTP tool and just like the browser did when we entered a URL in the address bar, we can have our HTTP tool issue a request to https://www.reddit.com.

          Our HTTP tool, Paw, doesn't process the response and lets us see the raw response data, which looks something like this:

          通過工具來做HTTP請求

          因為瀏覽器顯示的是服務(wù)端回復(fù)(response)被 處理后的版本,我們沒拿到服務(wù)器發(fā)回來原始的回復(fù),我們怎么查看 服務(wù)器返回的HTTP 裸回復(fù)呢?

          我們可以用HTTP工具 來實現(xiàn),我們在地址欄輸入URL, 我們的HTTP工具會發(fā)送一個請求到 https://www.reddit.com.我們的HTTP 工具,Paw,不會處理服務(wù)端的回復(fù)(response),那原始回復(fù)的response 是什么樣呢?如下圖



          What a huge difference this raw response is from the display in your browser! If you've never seen raw HTTP response data before, this may be quite shocking. What you see here is, in fact, what your browser also receives, except it parses and processes that huge blob of data into a user-friendly format.

          If you're learning about HTTP in order to become a web developer, you'll need to learn to read and process raw HTTP response data just by scanning it. Of course, you won't be able to convert it into a high-resolution picture in your head, but you should have a general idea of what the response is about. With enough experience, you can dig into the raw data and do some debugging and see exactly what's in the response.

          這個和在瀏覽器上顯示的有很大的區(qū)別! 你的瀏覽器也會受到一樣的裸回復(fù)(response)數(shù)據(jù),瀏覽器可以對回復(fù)數(shù)據(jù)進(jìn)行解析,并以一個友好的方式向用戶呈現(xiàn)出來。

          如果你在學(xué)習(xí)HTTP ,目的是做一個web 開發(fā)者,你需要學(xué)習(xí)和讀懂如何處理這些裸數(shù)據(jù)(response).當(dāng)然,你無法把response轉(zhuǎn)換為頭腦中一個高精度的照片,但是你應(yīng)該大概了解這個回復(fù)是什么含義。只要有足夠的經(jīng)驗,你就可以閱讀這些裸數(shù)據(jù),并做一些調(diào)試工作,看看回復(fù)(response)是什么。

          我在Postman 里面輸入地址后,打開console,也可以看到下面返回的裸回復(fù)。



          Using the Inspector

          Every modern browser has a way to view HTTP requests and responses, and it's usually called the inspector. We're going to use the Chrome Inspector to demonstrate how to analyze your browser's HTTP communication.

          Launch Chrome browser and open the Inspector by navigating to the Chrome Menu on the top right-hand corner of your browser. Select Tools > More Tools > Developer Tools. There are other ways of accessing the inspector, such as right-clicking and selecting the 'Inspector' option, or using a keyboard shortcut Ctrl+Shift+I (or Option+Command+I on a Mac).

          Send a new request to Reddit by entering the address https://www.reddit.com into your browser.

          With the inspector still open click on the Network tab:

          翻譯:

          使用檢查員(inspector)

          每一個現(xiàn)代的瀏覽器都有辦法可以讀HTTP 請求和回復(fù)請求,這個方式就是檢查員。我們將使用google 瀏覽器Chrome 的檢查員(inspector)來分析瀏覽器的HTTP通信。

          1. 啟動google 瀏覽器,打開inspector, 通過 select tools->More Tools>developer tools
          2. 發(fā)送一個請求到reddit, 在瀏覽器中鍵入地址 https://www.reddit.com
          3. 在inspector 中打開network 表。

          國內(nèi)訪問,果然時間要長很多。

          4. The first thing you should notice is that there are a lot of entries there. Each entry is a separate request, which means just by visiting the URL,

          your browser is making multiple requests, one for every resource (image, file, etc.). Click on the first request for the main page, www.reddit.com entry:

          翻譯:

          首先需要知道的是,這里很多的條目,每個條目是一個單獨(dú)的請求。這意味著(敲黑板),即使只是訪問url,你的瀏覽器已經(jīng)做了多個request,針對每個資源會申請一次(資源包括圖片、文件等等)。鼠標(biāo)點(diǎn)擊主頁條目看一下,www.reddit.com 條目:



          From here, you'll be able to see the specific request headers, cookies as well as the raw response data:



          翻譯:

          這里可以看到針對這個主頁的請求,cookie 以及裸回復(fù)(response)。

          The response data should look similar to what we saw earlier using our HTTP tool, except Chrome displays the data in a single line.

          Another thing to note when using the inspector's Network tab is, other than the first request, there are a ton of other requests returned:

          翻譯:

          回復(fù)的數(shù)據(jù)應(yīng)該和之前我們用HTTP 工具 看到的內(nèi)容是相似的,區(qū)別是google 瀏覽器在單行中顯示數(shù)據(jù)。(這里在新版本google 瀏覽器已經(jīng)改過來了)

          另外一個需要關(guān)注的是,使用inspector network 這個表,除了看到第一個請求,還有非常多的其它請求返回。(如下圖)



          Why are these additional responses sent back, who initiated the requests? What's happening is that the resource we requested, the initial www.reddit.com entry, returned some HTML. And in that HTML body are references to other resources like images, css stylesheets, javascript files and more. Your browser, being smart and helpful, understands that in order to produce a visually appealing presentation, it has to go and grab all these referenced resources. Hence, the browser will make separate requests for each resource referenced in the initial response. When you scroll down the Network tab, you'll be able to see all the referenced resources. These other requests are to make sure the page displays properly on your screen, among other things. Overall, you see that the browser's inspector gives you a good feel for these referenced resources. A pure HTTP tool, on the other hand, returns one huge response chunk without any concern for automatically pulling in referenced resources. A curl request will demonstrate this:

          翻譯:

          為什么會有額外的回復(fù)被發(fā)回來? 第一個請求條目,www.reddit.com 返回來一些HTML 的文件。這個文件指向了其它的資源,比如圖片、CSS、JS 文件。而你的瀏覽器足夠聰明和幫助,理解了這個HTML,并去抓去了所有相關(guān)的資源。

          這樣,在最開始的回復(fù)后,瀏覽器會針對每個資源做單獨(dú)的申請。當(dāng)你在network 表往下查看所有的表,你能夠看到所有被引用的資源。這些其它的請求可以幫助你把頁面在你的屏幕顯示的更加清晰,總的來看,瀏覽器inspector 給你一個好的感覺,針對所有的資源。

          一個純HTTP 工具,只會返回一個巨大的response 片段,不會考慮自動的去把關(guān)聯(lián)到的資源拉群進(jìn)來。一個curl 工具一般顯示的內(nèi)容如下:

          Reddit now requires that we add in a User-Agent to our HTTP requests. Otherwise, it will deny our request, assuming that the request originates from a bot.

          Make sure to append the following to any curl commands where reddit is the site you wish to send a request to.

          -A 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36'

          The -A option is used to specify a User-Agent for an HTTP request when using curl. Since this is another option for our command,

          don't forget to add in a space between -v and -A.

          For the sake of simplicity, we specify the User-Agent that is listed at the end of this page. You may use your own User-Agent as well.

          $ curl -X GET "https://www.reddit.com/" -m 30 -v

          What you should see is just one request and the response containing the HTML, but no additional requests being automatically issued, like you see in a browser.

          翻譯:

          下圖,curl 百度的效果圖:



          Request Methods

          Let's revisit the diagram from Step 3 above, when we looked at the responses in the Network tab. You might have noticed two columns named Method and Status.

          If you don't see the Methodcolumn, it may be hidden by default. To display the Method column, right click on Status and select Method.

          The Method column should now be visible next to the Status column.

          翻譯:

          請求方法

          讓我們重新調(diào)到第三步(在google瀏覽器inspector那一步),我們在network 里面查看responses的時候,你可以看到兩列:method 和 status.在status 狀態(tài)上右鍵,選擇展示method,如下圖。



          We'll spend this section looking at what the information shown in these columns mean.

          Information displayed in the Method column is known as the HTTP Request Method. You can think of this as the verb that tells the server what action to perform on a resource. The two most common HTTP request methods you'll see are GET and POST. When you think about retrieving information, think GET, which is the most used HTTP request method. In the above diagram, you'll notice almost all of the requests use GET to retrieve the resources needed to display the web page.

          The Status column shows the response status for each request. We'll talk about responses in detail later in the book. The important thing to understand is that every request gets a response, even if the response is an error -- that's still a response. (That's not 100% technically true as some requests can time out, but we'll set those rare cases aside for now.)

          我們將花一點(diǎn)時間介紹下這些列里包含信息的含義。

          • 1. 在method列里顯示的是HTTP Get method, 你可以理解為 告訴服務(wù)器來針對一個資源能做一個操作。
          • 2. 見的最多的request 方法是GET 和POST.

          當(dāng)你想獲取信息,用GET 方法,這是最常用的HTTP 請求方法,在上面的請求中,你可以看到幾乎所有的請求都是GET .狀態(tài)列顯示 每個請求的返回狀態(tài),我們在本書稍后會介紹狀態(tài)的含義。需要了解的是,每一個請求都會有一個回復(fù)(response),即使回復(fù)還是錯誤的,這仍然是一個回復(fù)。(當(dāng)然有些超時的除外)

          GET Requests

          GET requests are initiated by clicking a link or via the address bar of a browser. When you type an address like https://www.reddit.com into the address bar of your browser, you're making a GETrequest. You're asking the web browser to go retrieve the resource at that address, which means we've been making GET requests throughout this book. The same goes for interacting with links on web applications. The default behavior of a link is to issue a GET request to a URL. Let's make a simple GET request to https://www.reddit.com with an HTTP tool. Make sure to select GET and enter the address:



          You can view the raw HTTP response and other information sent back from the web server on the right panel.curl users can enter the following command on their terminal:

          $ curl -X GET "https://www.reddit.com/" -m 30 -v

          We can also send query strings using an HTTP tool. Let's look at another quick example by sending a request to search for all things Michael Jackson at https://itunes.apple.com/ with query strings. The final URL will look like this:

          https://itunes.apple.com/search?term=Michael%20Jackson

          before submitting a request, make sure to select GET.



          Here we are simply sending an HTTP GET request to the server at https://itunes.apple.com/ with parameter term=Michael%20Jackson where %20 is a URL-encoded character for SPACE.

          The curl command for this example is:

          $ curl -X GET "https://itunes.apple.com/search?term=Michael%20Jackson" -m 30 -v

          That's all you need to know about issuing HTTP GET requests for now. The primary concepts are:

          • GET requests are used to retrieve a resource, and most links are GETs.
          • The response from a GET request can be anything, but if it's HTML and that HTML references other resources, your browser will automatically request those referenced resources.
          • A pure HTTP tool will not.

          翻譯

          你需要了解的是:

          • Get 請求主要目的是獲取資源的,絕大多數(shù)鏈接就是GET
          • 從GET 請求里面拿到的反饋可以是任何東西,如果是HTML,而且HTML 包含了其它資源,瀏覽器會自動的請求這些被引用的資源。
          • 而一個純的HTTP 工具不會這樣做(自動抓取html 中的引用resource)

          POST Requests

          We've seen how to retrieve or ask for information from a server with GET, but what if you need to send or submit data to the server? That's where another essential HTTP request method comes in: POST. POST is used when you want to initiate some action on the server, or send data to a server. Let's see an example with our HTTP tool:

          翻譯:

          如果需要提交信息到服務(wù)器,這個時候需要使用HTTP 請求的另外一個方法,POST。 當(dāng)你需要對服務(wù)器發(fā)起某些操作,或者發(fā)送數(shù)據(jù)到服務(wù)器,需要POST。



          Here is the curl command:

          $ curl -X POST "https://echo.epa.gov" -m 30 -v

          The above screenshot shows a POST request to https://echo.epa.gov and the response from the server. Typically from within a browser, you use POST when submitting a form. POST requests allow us to send much larger and sensitive data to the server, such as images or videos. For example, say we need to send our username and password to the server for authentication. We could use a GET request and send it through query strings. The flaw with this approach is obvious: our credentials become exposed instantly in the URL; that isn't what we want. Using a POST request in a form fixes this problem. POST requests also help sidestep the query string size limitation that you have with GET requests. With POST requests, we can send significantly larger forms of information to the server.

          Let's see another example of making a POST request by filling out a web form. Our sample form looks like this in the browser:

          翻譯:

          curl 命令行如下:

          $ curl -X POST "https://echo.epa.gov" -m 30 -v

          • Post 請求允許我們發(fā)送更大的、更敏感的信息給服務(wù)器,比如照片 或者視頻。
          • 如果發(fā)送用戶名和密碼到服務(wù)器,使用GET 方法,這個時候我們的信息會被暴露,這不是我們希望做的。
          • 使用POST 方法,也不會在收到get 請求的大小限制,你可以發(fā)送大的多的信息給服務(wù)器。



          After filling out the form, you'll be redirected to a page that looks like this:

          Now let's switch over to our HTTP tool and simulate what we just did in the browser. Instead of filling out a form in the browser, we will send a POST request to http://al-blackjack.herokuapp.com/new_player. This is the URL that the first form (the one where we input a name) submits to:

          翻譯:

          我們換到HTTP 工具上來模擬我們在瀏覽器上的操作,代替我們在瀏覽器上填入信息,我們會發(fā)送Post 請求到 http://al-blackjack.herokuapp.com/new_player

          我們第一個表格(我們輸入名字的)提交到這個url:



          Note: You'll want to ensure that your Content-Type header is set to application/x-www-form-urlencoded. If it isn't, then your POST request won't be interpreted by the application correctly.

          If you're using Paw 3, select the Form URL-Encoded tab instead of the Text tab.

          If you're using Insomnia, make sure you click "Form URL Encoded" in the Body dropdown menu. And if you're using Postman, make sure the radio button for x-www-form-urlencodedis selected under the Body tab.

          翻譯:

          你會希望保證你的 content-type header 為 application/x-www-form-urlencoded.如果不這樣設(shè)置,服務(wù)器將無法識別請求。

          如果選擇postman,記得選擇了 x-www-form-urlencode 按鈕,在body表中。



          譯者在postman中的操作截圖:



          303 see other的含義:(from 譯者)



          Or you can use curl:

          $ curl -X POST "http://al-blackjack.herokuapp.com/new_player" -d "player_name=Albert" -m 30 -v

          Notice that in the screenshot and curl command we're supplying the additional parameter of player_name=albert. It has the same effect as inputting the name into the first "What's your name?" form and submitting it.

          We can verify the contents using the inspector (right click and select Inspect). You'll see that the player_name parameter we're sending as part of the POST request is embedded in the form via the nameattribute of the input element:

          翻譯:

          或者你可以用curl 命令實現(xiàn) :

          $ curl -X POST "http://al-blackjack.herokuapp.com/new_player" -d "player_name=Albert" -m 30 -v

          這里 -d "player_name=Albert" 和在瀏覽器里面填入 “what's your name?”表格效果是一樣的,可以在瀏覽器里面看到, player_name 參數(shù)是保存在表格里的。




          But the mystery is, how is the data we're sending being submitted to the server since it's no

          t being sent through the URL? The answer to that is the HTTP body.

          The body contains the data that is being transmitted in an HTTP message and is optional. In other words, an HTTP message can be sent with an empty body.

          When used, the body can contain HTML, images, audio and so on. You can think of the body as the letter enclosed in an envelope, to be posted.

          The POST request generated by the HTTP tool or curl is the same as you filling out the form in the browser, submitting that form, and then being redirected to the next page.

          Look carefully at the raw response in the HTTP tool screenshot. The key piece of information that redirects us to the next page is specified in the field Location:

          http://al-blackjack.herokuapp.com/bet. Location and its associated data is part of what is known as an HTTP response header (yes, requests have headers too, but in this case,

          it's a response header). Don't worry too much about this yet as we'll discuss headers in a later section. Your browser sees the response header and automatically issues a brand

          new request to the URL specified in the Location header, thereby initiating a new, unrelated request. The "Make a bet" form you see is the response from that second request.

          翻譯:

          不過疑問就在,這個數(shù)據(jù)是怎么提交到服務(wù)器上的,因為他沒有通過URL 提交。

          答案是:HTTP body (HTTP 身體)(敲黑板),HTTP body包含了傳遞給HTTP的數(shù)據(jù),這個是可選的。

          • 另一句話說,HTTP 消息可以通過空body來傳送。當(dāng)使用HTTP body 傳遞,body里面可以包含HTML , 圖片,聲音 還有其它。
          • 你可以把body 理解為保存在信封里面的信,隨時可以郵寄。
          • HTTP 工具發(fā)出的請求 或者 curl 發(fā)出的請求,和你在瀏覽器中填入表格是一樣的,提交那個表格,然后被重定向到另外一個頁面。
          • 可以 仔細(xì)的查看下HTTP 工具返回的原始response數(shù)據(jù),關(guān)鍵信息讓用戶重定向到下一頁是在 Location 這里定義:

          http://al-blackjack.herokuapp.com/bet. ( request 請求有Header ,這里是response 的header信息)你的瀏覽器看到了response header, 自動發(fā)起了一個新的請求到url (location 字段里指定的),這樣就看到了第二個response 表格。



          Note: If you're using some other HTTP tool, like Insomnia or Postman, you may have to uncheck "automatically follow redirects" in order to see the Location response header.



          If you're fuzzy on the previous paragraph, read it again. It's critical to understand that when using a browser, the browser hides a lot of the underlying HTTP request/response cycle from you. Your browser issued the initial POST request, got a response with a Location header, then issued another request without any action from you, then displayed the response from that second request. Once again, if you were using a pure HTTP tool, you'd see the Location response header from the first POST request, but the tool would not automatically issue a second request for you. (Some HTTP tools have this ability, if you check the "automatically follow redirects" option.)

          翻譯:

          這里就是對上文的重復(fù)解釋,不復(fù)述了。

          HTTP Headers

          HTTP headers allow the client and the server to send additional information during the request/response HTTP cycle. Headers are colon-separated name-value pairs that are sent in plain text. By using the Inspector, we can see these Headers. Below, you can see both the request as well as the response headers:

          The above shows the various headers being transmitted during a request/response cycle. Further, we can see that the request and response contain a different set of headers under Request Headers:

          The above shows the various headers being transmitted during a request/response cycle.

          Further, we can see that the request and response contain a different set of headers under Request Headers:



          Request Headers

          Request headers give more information about the client and the resource to be fetched. Some useful request headers are:



          Don't bother memorizing any of the request headers, but just know that it's part of the request being sent to the server. We'll talk about response headers in the next chapter.

          翻譯:

          這里講了request header 的一些信息,request header 和 response header 信息是不一樣的。

          上圖里request header 里面包含有:主機(jī)名、接受的語言、用戶端agent 信息、連接信息。

          Summary

          This was a brief introduction on making HTTP requests. After going through this section, you should be comfortable with:

          • using the inspector to view HTTP requests
          • making GET/POST requests with an HTTP tool
          • The most important components to understand about an HTTP request are:
          • HTTP method
          • path
          • headers
          • message body (for POST requests)

          In the next chapter, we'll continue learning about HTTP by looking at HTTP responses.

          翻譯:

          本章節(jié)介紹了下HTTP requests 信息,

          經(jīng)過本章節(jié),你可以:

          使用inspector 來 查看HTTP requests 請求

          使用HTTP 工具來發(fā)起GET /POST 請求

          最需要了解的組件包括:

          HTTP 方法

          路徑

          headers 頭部

          消息體(針對post 請求)


          主站蜘蛛池模板: 亚洲一区二区三区在线播放| 国产成人久久精品一区二区三区| 视频一区二区精品的福利| 国产精品成人免费一区二区| 乱码人妻一区二区三区| 国产熟女一区二区三区五月婷| 一区二区三区在线看| 国精品无码一区二区三区左线| 久久久久人妻一区精品色| 久久久久久人妻一区二区三区 | 国产精品无码不卡一区二区三区| 国产成人无码AV一区二区在线观看| 久久精品道一区二区三区| 亚洲熟妇无码一区二区三区| 亚洲国产综合精品一区在线播放| 日韩电影一区二区三区| 中文字幕一区二区三区人妻少妇| 国产一区二区三区亚洲综合| 无码精品人妻一区二区三区中 | 日韩精品一区二区午夜成人版| 伦理一区二区三区| 精品一区二区ww| 国产亚洲一区二区三区在线观看 | 区三区激情福利综合中文字幕在线一区| 中文字幕在线精品视频入口一区 | 日韩有码一区二区| 99精品国产高清一区二区| 一区二区三区免费精品视频| tom影院亚洲国产一区二区| 亚洲国模精品一区| 国产精品一区二区毛卡片| 亚洲无人区一区二区三区| 国产精品免费视频一区| 日本免费一区二区三区四区五六区 | 精品深夜AV无码一区二区| 亚洲国产一区视频| 国产综合无码一区二区色蜜蜜| 亚洲制服中文字幕第一区| 99精品高清视频一区二区| 久久一区二区三区精华液使用方法 | 日本韩国黄色一区二区三区|