Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 亚洲国产精品综合久久,日韩精品中文字幕视频一区,中文在线第一页

          整合營(yíng)銷服務(wù)商

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

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

          ASP.NET MVC-HTML 幫助器

          TML 幫助器用于修改 HTML 輸出。


          HTML 幫助器

          通過(guò) MVC,HTML 幫助器類似于傳統(tǒng)的 ASP.NET Web Form 控件。

          就像 ASP.NET 中的 Web Form 控件,HTML 幫助器用于修改 HTML。但是 HTML 幫助器是更輕量級(jí)的。與 Web Form 控件不同,HTML 幫助器沒有事件模型和視圖狀態(tài)。

          在大多數(shù)情況下,HTML 幫助器僅僅是一個(gè)返回字符串的方法。

          通過(guò) MVC,您可以創(chuàng)建您自己的幫助器,或者直接使用內(nèi)建的 HTML 幫助器。


          標(biāo)準(zhǔn)的 HTML 幫助器

          MVC 包含了大多數(shù)常用的 HTML 元素類型的標(biāo)準(zhǔn)幫助器,比如 HTML 鏈接和 HTML 表單元素。


          HTML 鏈接

          呈現(xiàn) HTML 鏈接的最簡(jiǎn)單的方法是使用 HTML.ActionLink() 幫助器。

          通過(guò) MVC,Html.ActionLink() 不連接到視圖。它創(chuàng)建一個(gè)連接到控制器操作。

          Razor 語(yǔ)法:

          @Html.ActionLink("About this Website", "About")

          ASP 語(yǔ)法:

          <%=Html.ActionLink("About this Website", "About")%>

          第一個(gè)參數(shù)是鏈接文本,第二個(gè)參數(shù)是控制器操作的名稱。

          上面的 Html.ActionLink() 幫助器,輸出以下的 HTML:

          <a href="/Home/About">About this Website</a>

          Html.ActionLink() 幫助器的一些屬性:

          屬性描述
          .linkTextURL 文本(標(biāo)簽),定位點(diǎn)元素的內(nèi)部文本。
          .actionName操作(action)的名稱。
          .routeValues傳遞給操作(action)的值,是一個(gè)包含路由參數(shù)的對(duì)象。
          .controllerName控制器的名稱。
          .htmlAttributesURL 的屬性設(shè)置,是一個(gè)包含要為該元素設(shè)置的 HTML 特性的對(duì)象。
          .protocolURL 協(xié)議,如 "http" 或 "https"。
          .hostnameURL 的主機(jī)名。
          .fragmentURL 片段名稱(定位點(diǎn)名稱)。

          注釋:您可以向控制器操作傳遞值。例如,您可以向數(shù)據(jù)庫(kù) Edit 操作傳遞數(shù)據(jù)庫(kù)記錄的 id:

          Razor 語(yǔ)法 C#:

          @Html.ActionLink("Edit Record", "Edit", new {Id=3})

          Razor 語(yǔ)法 VB:

          @Html.ActionLink("Edit Record", "Edit", New With{.Id=3})

          上面的 Html.ActionLink() 幫助器,輸出以下的 HTML:

          <a href="/Home/Edit/3">Edit Record</a>


          HTML 表單元素

          以下 HTML 幫助器可用于呈現(xiàn)(修改和輸出)HTML 表單元素:

          • BeginForm()

          • EndForm()

          • TextArea()

          • TextBox()

          • CheckBox()

          • RadioButton()

          • ListBox()

          • DropDownList()

          • Hidden()

          • Password()

          ASP.NET 語(yǔ)法 C#:

          <%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>

          <% using (Html.BeginForm()){%>

          <p>

          <label for="FirstName">First Name:</label>

          <%= Html.TextBox("FirstName") %>

          <%= Html.ValidationMessage("FirstName", "*") %>

          </p>

          <p>

          <label for="LastName">Last Name:</label>

          <%= Html.TextBox("LastName") %>

          <%= Html.ValidationMessage("LastName", "*") %>

          </p>

          <p>

          <label for="Password">Password:</label>

          <%= Html.Password("Password") %>

          <%= Html.ValidationMessage("Password", "*") %>

          </p>

          <p>

          <label for="Password">Confirm Password:</label>

          <%= Html.Password("ConfirmPassword") %>

          <%= Html.ValidationMessage("ConfirmPassword", "*") %>

          </p>

          <p>

          <label for="Profile">Profile:</label>

          <%= Html.TextArea("Profile", new {cols=60, rows=10})%>

          </p>

          <p>

          <%= Html.CheckBox("ReceiveNewsletter") %>

          <label for="ReceiveNewsletter" style="display:inline">Receive Newsletter?</label>

          </p>

          <p>

          <input type="submit" value="Register" />

          </p>

          <%}%>

          大家?guī)?lái)的是Andoird基本UI控件中的RadioButton和Checkbox; 先說(shuō)下本節(jié)要講解的內(nèi)容是:RadioButton和Checkbox的

          1.基本用法

          2.事件處理;

          3.自定義點(diǎn)擊效果;

          4.改變文字與選擇框的相對(duì)位置;

          5.修改文字與選擇框的距離

          其實(shí)這兩個(gè)控件有很多地方都是類似的,除了單選和多選,事件處理,其他的都是類似的! 另外還有一個(gè)ListView上Checkbox的錯(cuò)位的問題,我們會(huì)在ListView那一章對(duì)這個(gè)問題進(jìn)行 解決,好的,開始本節(jié)內(nèi)容~ 本節(jié)官方文檔API:RadioButton;CheckBox;


          1.基本用法與事件處理:


          1)RadioButton(單選按鈕)

          如題單選按鈕,就是只能夠選中一個(gè),所以我們需要把RadioButton放到RadioGroup按鈕組中,從而實(shí)現(xiàn) 單選功能!先熟悉下如何使用RadioButton,一個(gè)簡(jiǎn)單的性別選擇的例子: 另外我們可以為外層RadioGroup設(shè)置orientation屬性然后設(shè)置RadioButton的排列方式,是豎直還是水平~

          效果圖:

          PS:筆者的手機(jī)是Android 5.0.1的,這里的RadioButton相比起舊版本的RadioButton,稍微好看一點(diǎn)~

          布局代碼如下:

          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

          xmlns:tools="http://schemas.android.com/tools"

          android:id="@+id/LinearLayout1"

          android:layout_width="match_parent"

          android:layout_height="match_parent"

          android:orientation="vertical"

          tools:context=".MainActivity" >

          <TextView

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"

          android:text="請(qǐng)選擇性別"

          android:textSize="23dp"

          />

          <RadioGroup

          android:id="@+id/radioGroup"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"

          android:orientation="horizontal">

          <RadioButton

          android:id="@+id/btnMan"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"

          android:text="男"

          android:checked="true"/>

          <RadioButton

          android:id="@+id/btnWoman"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"

          android:text="女"/>

          </RadioGroup>

          <Button

          android:id="@+id/btnpost"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"

          android:text="提交"/>

          </LinearLayout>

          獲得選中的值:

          這里有兩種方法,

          第一種是為RadioButton設(shè)置一個(gè)事件監(jiān)聽器setOnCheckChangeListener

          例子代碼如下:

          RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroup);

          //第一種獲得單選按鈕值的方法

          //為radioGroup設(shè)置一個(gè)監(jiān)聽器:setOnCheckedChanged()

          radgroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

          @Override

          public void onCheckedChanged(RadioGroup group, int checkedId) {

          RadioButton radbtn = (RadioButton) findViewById(checkedId);

          Toast.makeText(getApplicationContext(), "按鈕組值發(fā)生改變,你選了" + radbtn.getText(), Toast.LENGTH_LONG).show();

          }

          });

          運(yùn)行效果圖:

          PS:另外有一點(diǎn)要切記,要為每個(gè)RadioButton添加一個(gè)id,不然單選功能會(huì)生效?。?!

          第二種方法是通過(guò)單擊其他按鈕獲取選中單選按鈕的值,當(dāng)然我們也可以直接獲取,這個(gè)看需求~

          例子代碼如下:

          Button btnchange = (Button) findViewById(R.id.btnpost);

          RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroup);

          //為radioGroup設(shè)置一個(gè)監(jiān)聽器:setOnCheckedChanged()

          btnchange.setOnClickListener(new OnClickListener() {

          @Override

          public void onClick(View v) {

          for (int i = 0; i < radgroup.getChildCount(); i++) {

          RadioButton rd = (RadioButton) radgroup.getChildAt(i);

          if (rd.isChecked()) {

          Toast.makeText(getApplicationContext(), "點(diǎn)擊提交按鈕,獲取你選擇的是:" + rd.getText(), Toast.LENGTH_LONG).show();

          break;

          }

          }

          }

          });

          運(yùn)行效果圖:

          代碼解析: 這里我們?yōu)樘峤话粹o設(shè)置了一個(gè)setOnClickListener事件監(jiān)聽器,每次點(diǎn)擊的話遍歷一次RadioGroup判斷哪個(gè)按鈕被選中我們可以通過(guò)下述方法獲得RadioButton的相關(guān)信息!

          • getChildCount( )獲得按鈕組中的單選按鈕的數(shù)目;
          • getChinldAt(i):根據(jù)索引值獲取我們的單選按鈕
          • isChecked( ):判斷按鈕是否選中

          2)CheckBox(復(fù)選框)

          如題復(fù)選框,即可以同時(shí)選中多個(gè)選項(xiàng),至于獲得選中的值,同樣有兩種方式: 1.為每個(gè)CheckBox添加事件:setOnCheckedChangeListener 2.弄一個(gè)按鈕,在點(diǎn)擊后,對(duì)每個(gè)checkbox進(jìn)行判斷:isChecked();

          運(yùn)行效果圖:

          實(shí)現(xiàn)代碼:

          public class MainActivity extends AppCompatActivity implements View.OnClickListener,CompoundButton.OnCheckedChangeListener{

          private CheckBox cb_one;

          private CheckBox cb_two;

          private CheckBox cb_three;

          private Button btn_send;

          @Override

          protected void onCreate(Bundle savedInstanceState) {

          super.onCreate(savedInstanceState);

          setContentView(R.layout.activity_main);

          cb_one = (CheckBox) findViewById(R.id.cb_one);

          cb_two = (CheckBox) findViewById(R.id.cb_two);

          cb_three = (CheckBox) findViewById(R.id.cb_three);

          btn_send = (Button) findViewById(R.id.btn_send);

          cb_one.setOnCheckedChangeListener(this);

          cb_two.setOnCheckedChangeListener(this);

          cb_three.setOnCheckedChangeListener(this);

          btn_send.setOnClickListener(this);

          }

          @Override

          public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

          if(compoundButton.isChecked()) Toast.makeText(this,compoundButton.getText().toString(),Toast.LENGTH_SHORT).show();

          }

          @Override

          public void onClick(View view) {

          String choose = "";

          if(cb_one.isChecked())choose += cb_one.getText().toString() + "";

          if(cb_two.isChecked())choose += cb_two.getText().toString() + "";

          if(cb_three.isChecked())choose += cb_three.getText().toString() + "";

          Toast.makeText(this,choose,Toast.LENGTH_SHORT).show();

          }

          }


          2.自定義點(diǎn)擊效果

          雖然5.0后的RadioButton和Checkbox都比舊版本稍微好看了點(diǎn),但是對(duì)于我們來(lái)說(shuō) 可能還是不喜歡或者需求,需要自己點(diǎn)擊效果!實(shí)現(xiàn)起來(lái)很簡(jiǎn)單,先編寫一個(gè)自定義 的selctor資源,設(shè)置選中與沒選中時(shí)的切換圖片~!

          實(shí)現(xiàn)效果圖如下:

          PS:這里素材的原因,有點(diǎn)小...

          <?xml version="1.0" encoding="utf-8"?>

          <selector xmlns:android="http://schemas.android.com/apk/res/android">

          <item

          android:state_enabled="true"

          android:state_checked="true"

          android:drawable="@mipmap/ic_checkbox_checked"/>

          <item

          android:state_enabled="true"

          android:state_checked="false"

          android:drawable="@mipmap/ic_checkbox_normal" />

          </selector>

          寫好后,我們有兩種方法設(shè)置,也可以說(shuō)一種吧!你看看就知道了~

          ①android:button屬性設(shè)置為上述的selctor

          android:button="@drawable/rad_btn_selctor"

          ②在style中定義一個(gè)屬性,然后通過(guò)android style屬性設(shè)置,先往style添加下述代碼:

          <style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">

          <item name="android:button">@drawable/rad_btn_selctor</item>

          </style>

          然后布局那里:

          style="@style/MyCheckBox"


          3.改變文字與選擇框的相對(duì)位置

          這個(gè)實(shí)現(xiàn)起來(lái)也很簡(jiǎn)單,還記得我們之前學(xué)TextView的時(shí)候用到的drawableXxx嗎? 要控制選擇框的位置,兩部即可!設(shè)置:

          Step 1. android:button="@null"

          Step 2. android:drawableTop="@android:drawable/btn_radio"

          當(dāng)然我們可以把drawableXxx替換成自己喜歡的效果!


          4.修改文字與選擇框的距離

          有時(shí),我們可能需要調(diào)節(jié)文字與選擇框之間的距離,讓他們看起來(lái)稍微沒那么擠,我們可以:

          1.在XML代碼中控制: 使用android:paddingXxx = "xxx" 來(lái)控制距離

          2.在Java代碼中,稍微好一點(diǎn),動(dòng)態(tài)計(jì)算paddingLeft!

          示例代碼如下:

          rb.setButtonDrawable(R.drawable.rad_btn_selctor);

          int rb_paddingLeft = getResources().getDrawable(R.mipmap.ic_checkbox_checked).getIntrinsicWidth()+5;

          rb.setPadding(rb_paddingLeft, 0, 0, 0);


          本節(jié)小結(jié):

          好的,關(guān)于RadioButton和Checkbox就講到這里,如果有什么寫得不對(duì)的,不好的,或者有好的建議歡迎指出 萬(wàn)分感激

          天內(nèi)容好長(zhǎng),翻譯起來(lá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í)下工具來(lái)演練 HTTP怎么工作的。本章節(jié)介紹下一些工具,可以選擇一個(gè)你覺得比較合適的來(lái)上手。

          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, 雖然他需要在蘋果商店買,但是也有短時(shí)間免費(fèi)版本。

          你也可以選擇 Insomnia 和 Postman (譯者之前裝過(guò)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 是一個(gè)免費(fèi)的工具,用來(lái)發(fā)送HTTP 請(qǐng)求,和MAC 及絕大多數(shù) Linux 系統(tǒng)原生綁定。

          你可以很簡(jiǎn)單的啟動(dòng)curl ,用下面命令行:

          $ curl www.google.com

          如果你有windows10的 1803 及以后版本,curl 也是默認(rèn)安裝好了,如果你有更早的版本,你可以考慮安裝一個(gè),圖形界面的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的申請(qǐng)

          通過(guò)一個(gè)瀏覽器做HTTP請(qǐng)求,做HTTP請(qǐng)求很容易,你可以在瀏覽器中訪問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é)處理請(qǐng)求,并返回一個(gè)response 到瀏覽器,瀏覽器足夠“聰明”可以處理返回的響應(yīng)文件,在屏幕上把文件展示出來(lái),包含的是顏色、圖片、文字和展示。

          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:

          通過(guò)工具來(lái)做HTTP請(qǐng)求

          因?yàn)闉g覽器顯示的是服務(wù)端回復(fù)(response)被 處理后的版本,我們沒拿到服務(wù)器發(fā)回來(lái)原始的回復(fù),我們?cè)趺床榭?服務(wù)器返回的HTTP 裸回復(fù)呢?

          我們可以用HTTP工具 來(lái)實(shí)現(xiàn),我們?cè)诘刂窓谳斎險(xiǎn)RL, 我們的HTTP工具會(huì)發(fā)送一個(gè)請(qǐng)求到 https://www.reddit.com.我們的HTTP 工具,Paw,不會(huì)處理服務(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.

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

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

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

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

          國(guó)內(nèi)訪問,果然時(shí)間要長(zhǎng)很多。

          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:

          翻譯:

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



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



          翻譯:

          這里可以看到針對(duì)這個(gè)主頁(yè)的請(qǐng)求,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ò)來(lái)了)

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



          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:

          翻譯:

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

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

          一個(gè)純HTTP 工具,只會(huì)返回一個(gè)巨大的response 片段,不會(huì)考慮自動(dòng)的去把關(guān)聯(lián)到的資源拉群進(jìn)來(lái)。一個(gè)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.

          翻譯:

          請(qǐng)求方法

          讓我們重新調(diào)到第三步(在google瀏覽器inspector那一步),我們?cè)趎etwork 里面查看responses的時(shí)候,你可以看到兩列: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)時(shí)間介紹下這些列里包含信息的含義。

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

          當(dāng)你想獲取信息,用GET 方法,這是最常用的HTTP 請(qǐng)求方法,在上面的請(qǐng)求中,你可以看到幾乎所有的請(qǐng)求都是GET .狀態(tài)列顯示 每個(gè)請(qǐng)求的返回狀態(tài),我們?cè)诒緯院髸?huì)介紹狀態(tài)的含義。需要了解的是,每一個(gè)請(qǐng)求都會(huì)有一個(gè)回復(fù)(response),即使回復(fù)還是錯(cuò)誤的,這仍然是一個(gè)回復(fù)。(當(dāng)然有些超時(shí)的除外)

          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 請(qǐng)求主要目的是獲取資源的,絕大多數(shù)鏈接就是GET
          • 從GET 請(qǐng)求里面拿到的反饋可以是任何東西,如果是HTML,而且HTML 包含了其它資源,瀏覽器會(huì)自動(dòng)的請(qǐng)求這些被引用的資源。
          • 而一個(gè)純的HTTP 工具不會(huì)這樣做(自動(dòng)抓取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ù)器,這個(gè)時(shí)候需要使用HTTP 請(qǐng)求的另外一個(gè)方法,POST。 當(dāng)你需要對(duì)服務(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 請(qǐng)求允許我們發(fā)送更大的、更敏感的信息給服務(wù)器,比如照片 或者視頻。
          • 如果發(fā)送用戶名和密碼到服務(wù)器,使用GET 方法,這個(gè)時(shí)候我們的信息會(huì)被暴露,這不是我們希望做的。
          • 使用POST 方法,也不會(huì)在收到get 請(qǐng)求的大小限制,你可以發(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 工具上來(lái)模擬我們?cè)跒g覽器上的操作,代替我們?cè)跒g覽器上填入信息,我們會(huì)發(fā)送Post 請(qǐng)求到 http://al-blackjack.herokuapp.com/new_player

          我們第一個(gè)表格(我們輸入名字的)提交到這個(gè)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.

          翻譯:

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

          如果選擇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 命令實(shí)現(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.

          翻譯:

          不過(guò)疑問就在,這個(gè)數(shù)據(jù)是怎么提交到服務(wù)器上的,因?yàn)樗麤]有通過(guò)URL 提交。

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

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

          http://al-blackjack.herokuapp.com/bet. ( request 請(qǐng)求有Header ,這里是response 的header信息)你的瀏覽器看到了response header, 自動(dòng)發(fā)起了一個(gè)新的請(qǐng)求到url (location 字段里指定的),這樣就看到了第二個(gè)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.)

          翻譯:

          這里就是對(duì)上文的重復(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ī)名、接受的語(yǔ)言、用戶端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)過(guò)本章節(jié),你可以:

          使用inspector 來(lái) 查看HTTP requests 請(qǐng)求

          使用HTTP 工具來(lái)發(fā)起GET /POST 請(qǐng)求

          最需要了解的組件包括:

          HTTP 方法

          路徑

          headers 頭部

          消息體(針對(duì)post 請(qǐng)求)


          主站蜘蛛池模板: 亚洲av无码一区二区三区网站| 国产精品电影一区| 香蕉久久一区二区不卡无毒影院 | www一区二区三区| 人妻av综合天堂一区| 中文字幕亚洲一区二区三区| 久久久久久人妻一区二区三区 | 亚洲AV综合色一区二区三区| 国产精品久久无码一区二区三区网 | 国产丝袜美女一区二区三区 | 精品国产一区AV天美传媒| 国产在线视频一区二区三区98| 成人在线视频一区| 激情内射亚州一区二区三区爱妻| 国产伦精品一区二区三区免费下载| 中文字幕无码不卡一区二区三区| 国产suv精品一区二区33| 久久久久成人精品一区二区| 日本成人一区二区| 高清国产AV一区二区三区| 亚洲国产成人一区二区三区| 国产成人精品一区二区三区无码| 国产自产V一区二区三区C| 亚洲综合无码AV一区二区 | 亚洲午夜电影一区二区三区| 人体内射精一区二区三区| 国产精品高清一区二区人妖| 中文字幕AV一区二区三区 | 色噜噜狠狠一区二区| 国产精品视频一区二区三区经| 亚洲乱码av中文一区二区 | 亚洲无码一区二区三区| 国产成人久久一区二区不卡三区 | 亚洲一区影音先锋色资源| 亚洲国产成人一区二区精品区| 亚洲一区二区免费视频| 国精产品一区一区三区MBA下载| 国产天堂在线一区二区三区 | 精品黑人一区二区三区| 理论亚洲区美一区二区三区| 国产福利电影一区二区三区,亚洲国模精品一区 |