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

首頁 > 編程 > .NET > 正文

使用.NET訪問 Internet(1) Paul_Ni(原作)

2024-07-10 13:08:14
字體:
來源:轉載
供稿:網友
國內最大的酷站演示中心!
microsoft .net 框架提供 internet 服務的分層的、可擴展的和托管的實現,您可以將這些 internet 服務快速而輕松地集成到您的應用程序中。您的應用程序可建立在可插接式協議的基礎之上以便自動利用新的 internet 協議,或者它們可以使用 windows 套接字接口的托管實現來使用套接字級別上的網絡。

介紹可插接式協議


microsoft .net 框架提供分層的、可擴展的和托管的 internet 服務實現,您可以將它們快速而輕松地集成到您的應用程序中。system.net 和 system.net.sockets 命名空間中的 internet 訪問類可用于實現基于 web 和基于 internet 的應用程序。

internet 應用程序


internet 應用程序大體上分為兩類:客戶端應用程序(請求信息)和服務器應用程序(響應來自客戶端的信息請求)。典型的 internet 客戶端-服務器應用程序是萬維網 (world wide web),在萬維網中,人們使用瀏覽器來訪問世界各地的 web 服務器上存儲的文檔和其他數據。
應用程序并不限于僅充當其中的一個角色;例如,大家所熟悉的中間層應用程序服務器通過請求其他服務器的數據來響應客戶端的請求,在這種情況中,它既作為服務器,也作為客戶端。
客戶端應用程序通過標識所請求的 internet 資源以及用于該請求和響應的通訊協議來發出請求。如有必要,客戶端還提供完成請求所需的任何附加數據,例如代理位置或身份驗證信息(用戶名、密碼等)。只要構成了請求,就可以將該請求發送到服務器。

標識資源


.net 框架使用統一資源標識符 (uri) 來標識所請求的 internet 資源和通訊協議。uri 至少由三個(也可能是四個)片段組成:方案標識符(標識用于請求和響應的通訊協議)、服務器標識符(由域名系統 (dns) 主機名或 tcp 地址組成,用于唯一標識 internet 上的服務器)、路徑標識符(定位服務器上請求的信息)以及可選的查詢字符串(將信息從客戶端傳送到服務器)。例如,uri“http://www.contoso.com/whatsnew.aspx?date=today”就是由方案標識符“http”、服務器標識符“www.contoso.com”、路徑“whatsnew.aspx”和查詢字符串“?date=today”組成的。
只要服務器接收到請求并進行了響應,它就將該響應返回到客戶端應用程序。響應包括補充信息,例如內容的類型(如原始文本或 xml 數據)。

.net 框架中的請求和響應


.net 框架使用特定類來提供通過請求/響應模型訪問 internet 所需的三部分信息:uri 類(包含您要查找的 internet 資源的 uri)、webrequest 類(包含對該資源的請求)以及 webresponse 類(為傳入的響應提供容器)。
客戶端應用程序通過將網絡資源的 uri 傳遞到 webrequest.create 方法來創建 webrequest 實例。此靜態方法創建特定協議(例如 http)的 webrequest 實例。返回的 webrequest 實例提供對屬性的訪問,這些屬性既控制對服務器的請求,又控制對建立請求時發送的數據流的訪問。webrequest 實例上的 getresponse 方法將來自客戶端應用程序的請求發送到在 uri 中標識的服務器。在響應可能被延遲的情況下,可以使用 webrequest 實例上的 begingetresponse 方法異步建立請求,并且可以使用 endgetresponse 方法在以后返回響應。
getresponse endgetresponse 方法返回一個 webresponse 實例,該實例提供對服務器返回的數據的訪問。因為此數據由 getresponsestream 方法作為流提供給發出請求的應用程序,所以它可以在應用程序中的使用數據流的任何地方使用。
webrequest 類和 webresponse 類是可插接式協議的基礎——這是一種網絡服務的實現,該實現使您可以在開發使用 internet 資源的應用程序時,無需擔心每一資源使用的協議的特定詳細信息。用 webrequest 類注冊 webrequest 的子代類可以管理有關建立與 internet 資源的實際連接的詳細信息。
舉例來說,httpwebrequest 類管理有關使用 http 連接到 internet 資源的詳細信息。默認情況下,當 webrequest.create 方法遇到以“http:”或“https:”(http 和安全 http 的協議標識符)開頭的 uri 時,返回的 webrequest 實例可以按照原樣使用,或者可以被類型轉換為 httpwebrequest 以訪問協議特定的屬性。在大多數情況下,webrequest 實例提供建立請求所必需的所有信息。
可被表示為請求/響應事務的所有協議都可以在 webrequest 中使用。您可以從 webrequestwebresponse 導出協議特定的類,然后通過靜態 webrequest.registerprefix 方法注冊它們以供應用程序使用。
當要求 internet 請求的客戶端驗證時,webrequest 的 credentials 屬性提供必需的憑據。這些憑據可以是用于基本 http 或簡要身份驗證的簡單名稱/密碼對,或者是用于 ntlm 或 kerberos 身份驗證的名稱/密碼/域組。一組憑據可以存儲在 networkcredentials 實例中,或者多組憑據可以同時存儲在 credentialcache 實例中。credentialcache 使用該請求的 uri 和服務器支持的身份驗證方案來確定哪些憑據將發送到服務器。

通過 webclient 進行簡單請求


對于需要建立用于 internet 資源的簡單請求的應用程序而言,webclient 類提供將數據上載到 internet 服務器或從 internet 服務器下載數據的公共方法。webclient 依賴 webrequest 類來提供對 internet 資源的訪問;因此,webclient 類可以使用任何注冊的可插接式協議。
對于不能使用請求/響應模型的應用程序而言,或者對于需要偵聽網絡并發送請求的應用程序而言,system.net.sockets 命名空間提供 tcpclient 類、tcplistener 類和 udpclient 類。這些類處理使用不同的傳輸協議建立連接的詳細信息,并且作為流向應用程序公開網絡連接。
對于熟悉 windows 套接字接口的開發人員或者需要通過在套接字級別編程所提供的控制的開發人員,他們將發現 system.net.sockets 類可以滿足他們的需要。system.net.sockets 類是從托管到 system.net 類中本機代碼的轉換點。在大多數情況下,system.net.sockets 類將數據封送到其 windows 32 位副本中,以及處理任何必需的安全檢查。

請求數據


開發在當今的 internet 分布式操作環境中運行的應用程序要求采用高效、易用的方法檢索所有類型資源的數據??刹褰邮絽f議令您可以開發使用單個接口檢索來自多個 internet 協議的數據的應用程序。
對于簡單請求和響應事務,webclient 類提供將數據上載到 internet 服務器或從 internet 服務器下載數據的最簡便的方法。webclient 提供上載和下載文件、發送和接收流以及將數據緩沖區發送到服務器并接收響應的方法。webclient 使用 webrequest 類和 webresponse 類來建立與 internet 資源的實際連接,以便注冊的所有可插接式協議都可供使用。以下示例請求 web 頁并在流中返回結果。
[c#]
webclient myclient = new webclient();
stream response = myclient.openread("http://www.contoso.com/index.htm");
// the stream data is used here.
response.close();

需要進行更復雜的事務處理的客戶端應用程序使用 webrequest 類及其子代來請求服務器中的數據。webrequest 封裝連接到服務器、發送請求并接收響應的詳細信息。webrequest 是抽象類,定義可用于使用可插接式協議的所有應用程序的一組屬性和方法。webrequest 的子代(例如 httpwebrequest)實現由 webrequest 定義的屬性和方法,所采取的方式與基礎協議一致。
webrequest 類通過使用傳遞到其 create 方法的 uri 的值來確定要創建的特定派生類實例,創建 webrequest 子代的協議特定的實例。應用程序通過使用 webrequest.registerprefix 方法注冊子代的構造函數,指示哪一 webrequest 子代應被用于處理請求。
通過調用 webrequest 實例上的 getresponse 方法來建立對 internet 資源的請求。getresponse 方法構造來自于 webrequest 實例的屬性的協議特定的請求,建立與服務器的 tcp 或 udp 套接字連接,并發送該請求。對于將數據發送到服務器的請求,例如 http post 或 ftp put 請求,webrequest.getrequeststream 方法提供要將數據發送到其中的網絡流。
getresponse 方法返回與 webrequest 匹配的協議特定 webresponse,如以下示例所示。
[c#]
webrequest req = webrequest.create("http://www.contoso.com/");
webresponse resp = req.getresponse();

webresponse 類也是抽象類,定義可用于使用可插接式協議的所有應用程序的屬性和方法。webresponse 子代實現這些用于基礎協議的屬性和方法。例如,httpwebresponse 類實現用于 http 的 webresponse 類。
由服務器返回的數據被提供給由 webresponse.getresponsestream 返回的流中的應用程序。您可以像使用任何其他流一樣使用此流,如以下示例所示。
[c#]
streamreader sr =
   new streamreader(resp.getresponsestream(), encoding.ascii);

創建 internet 請求


應用程序通過 webrequest.create 方法創建 webrequest 實例。這是創建子代 webrequest 實例的靜態方法(基于傳遞到該實例的 uri 方案)。
.net 框架提供繼承自 webrequest 的 httpwebrequest 類,以處理對 internet 的 http 和 https 請求。在大多數情況下,webrequest 提供您建立請求所需的所有屬性;但是,如有必要,您可以將通過 webrequest.create 方法創建的 webrequest 對象類型轉換為 httpwebrequest,以訪問請求的 http 特定的屬性。
httpwebresponse 處理對 internet 的 http 和 https 請求的響應。為訪問 httpwebresponse 的 http 特定屬性,您需要將 webresponse 類型轉換為 httpwebresponse。
為處理使用其他 internet 協議(例如 ftp)的請求,您需要導出 webrequest webresponse 的協議特定子代。

使用 internet 請求和響應類


若要從 internet 請求數據并讀取響應,請使用以下步驟。
如果要訪問像 web 頁這樣的資源,請通過用要使用的資源的 uri 調用 webrequest.create 來創建 webrequest 實例,如下例所示。
[c#]
webrequest wreq = webrequest.create("http://www.contoso.com/");
注意 .net 框架為以“http:”、“https:”和“file:”開頭的 uri 提供協議特定的 webrequestwebresponse 子代。若要訪問其他協議,必須實現協議特定的 webrequestwebresponse 子代。
webrequest 實例中設置任何所需的屬性值。例如,若要支持身份驗證,請將 credentials 屬性設置為 networkcredential 類的實例,如下例所示。
[c#]
wreq.credentials =
   new networkcredential("username","password");
大多數情況下,webrequest 實例本身就已足夠發送和接收數據。但是,如果需要設置協議特定的屬性,則將 webrequest 實例轉換為協議特定的實例。僅當處理 webrequestwebrequest 子代正確時,此類型轉換才有效。例如,若要訪問 httpwebrequest 的 http 特定的屬性,請將 webrequest 轉換為 httpwebrequest。    下面的代碼實例顯示如何設置 http 特定的 useragent 屬性。
[c#]
if (wreq is httpwebrequest)
{
((httpwebrequest)wreq).useragent = ".net framework example client";
}
若要從 internet 下載資源,請調用 webrequest 的 getresponse 方法。
若要將數據發送或上載到資源,請調用 webrequest 的 getrequeststream 方法,并使用結果 stream 對象編寫數據。完成上載后,必須使用 stream.close 方法關閉請求流。關閉流后,可以調用 getresponse 以確保服務器已正確接收到數據。為 webresponse 實例返回的實際類由所請求的 uri 方案決定。下面的代碼實例顯示如何使用 getresponse 方法創建 webresponse 實例。
[c#]
webresponse wresp = wreq.getresponse();
注意 調用 webresponse 后,必須使用 webresponse.close 或 stream.close 關閉響應。如果不關閉每個響應,應用程序將用完與服務器的連接,而無法處理其他請求。
使用 webresponse 實例的 getresponsestream 方法從網絡資源中獲取包含響應數據的流。還可以訪問 webresponse 實例的屬性或將 webresponse 實例轉換為協議特定的實例以讀取協議特定的屬性。例如,若要訪問 httpwebresponse 的 http 特定的屬性,請將 webresponse 轉換為 httpwebresponse。下面的代碼示例顯示如何訪問 http 特定的屬性和讀取響應流。
[c#]
// read an http-specific property.
if (wresp is httpwebresponse)
{
   datetime updated = ((httpwebresponse)wresp).lastmodified;
}
// get the response stream.
stream respstream = wresp.getresponsestream();

// this example uses a streamreader to read the entire response
// into a string and then writes the string to the console.
streamreader reader = new streamreader(respstream, encoding.ascii);
string resphtml = reader.readtoend();
console.writeline(resphtml);

// close the response stream.
respstream.close();
如果應用程序只需要 webresponse 中返回的標頭信息并忽略任何返回的數據,則不必獲取響應流。下面的代碼示例顯示如何從 internet 主機返回服務器標頭信息。
[c#]
webrequest wreq = webrequest.create("http://www.contoso.com");
webresponse wresp = wreq.getresponse();
string server = wresp.headers["server"];
讀取響應中的數據后,必須使用 stream.close 方法關閉所有打開的流或使用 webresponse.close 方法關閉響應。
[c#]
wresp.close();
不必在響應流和 webresponse 實例上都調用 close 方法,但這樣做并沒有什么害處。webresponse.close 在關閉響應時調用 stream.close。
下面的示例應用程序說明 webrequestwebresponse 類的用法。
[c#]
using system;
using system.net;
using system.text;
using system.io;

class clientget {
public static void main(string[] args)
{
    if (args.length < 1)
    {
      showusage();
      return;
    }

    // get the uri from the command line.
    uri site = new uri(args[0]);
    // create the request instance.
    webrequest wreq = webrequest.create(site);
    // set the http-specific useragent property
    if (wreq is httpwebrequest)
    {
      ((httpwebrequest)wreq).useragent =
        ".net framework example client";
    }
    // get the response instance
    webresponse wresp = wreq.getresponse();
    // read an http-specific property.
    if (wresp is httpwebresponse)
    {
      datetime updated = ((httpwebresponse)wresp).lastmodified;
    }
    // get the response stream.
    stream respstream = wresp.getresponsestream();
    // this example uses a streamreader to read the entire response
   // into a string and then writes the string to the console.
   streamreader reader =
       new streamreader(respstream, encoding.ascii);
    string resphtml = reader.readtoend();
    console.writeline(resphtml);
    // close the response and response stream.
    wresp.close();
  }
  public static void showusage()
  {
     console.writeline("attempts to get a uri.");
     console.writeline("/r/nusage:");
     console.writeline("  clientget uri");
     console.writeline("example:");
     console.writeline("  clientget http://www.contoso.com/");
  }
}

在網絡上使用流


網絡資源在 .net 框架中表示為流。通過對流進行一般處理,.net 框架提供下列功能:
  • 發送和接收 web 數據的通用方法。無論文件的實際內容是什么(html、xml 或其他任何內容),應用程序都將使用 stream.write 和 stream.read 發送和接收數據。
  • 整個框架中流的兼容性。流用于整個 .net 框架中,此框架具有豐富的結構來處理流。例如,通過只修改初始化流的幾行代碼,可以修改從 filestream 中讀取 xml 數據的應用程序,使其改為從 networkstream 中讀取數據。networkstream 類和其他流之間的主要區別是:networkstream 是不可查找的,canseek 屬性始終返回 false,而 seek 和 position 方法引發 notsupportedexception。
  • 當數據到達時處理數據。流在數據從 internet 下載的過程中提供對數據的訪問,而不是強制應用程序等待下載完整個數據集。

system.net.sockets 命名空間包含一個 networkstream 類,該類實現專門用于 internet 的 stream 類。system.net.sockets 命名空間中的類使用 networkstream 類表示流。
若要使用返回的流向網絡發送數據,請在 webrequest 實例上調用 getrequeststream。webrequest 將請求標頭發送到服務器,然后您可以通過在返回的流上調用 beginwrite、endwrite 或 write 方法,將數據發送到 internet 資源。某些協議(如 http)可能要求在發送數據之前設置協議特定的屬性。下面的代碼示例顯示如何為發送數據設置 http 特定的屬性。該示例假定變量 senddata 包含要發送的數據,而變量 sendlength 是要發送的數據字節數。
[c#]
httpwebrequest request =
   (httpwebrequest) webrequest.create("http://www.contoso.com/");
request.method = "post";
request.contentlength = sendlength;
try
{
   stream sendstream = request.getrequeststream();
   sendstream.write(senddata,0,sendlength);
   sendstream.close();
}
catch
{
   // handle errors . . .
}
若要從網絡接收數據,請在 webresponse 實例上調用 getresponsestream。然后,您可以通過在返回的流上調用 beginread、endread 或 read 方法,從 internet 資源讀取數據。
使用來自網絡資源的流時,請記住以下幾點。
  • 由于 networkstream 類無法更改流中的位置,因此 canseek 屬性總是返回 false。seekposition 方法引發 notsupportedexception。
  • 當使用 webrequestwebresponse 時,通過調用 getresponsestream 創建的流實例是只讀的,通過調用 getrequeststream 創建的流實例是只寫的。
  • 使用 streamreader 類使編碼更容易。下面的代碼實例使用 streamreaderwebresponse 實例讀取用 ascii 編碼的流(此示例不顯示如何創建請求)。
  • 如果網絡資源不可用,則對 getresponse 的調用可能會阻塞。應考慮利用 begingetresponse 和 endgetresponse 方法使用異步請求。
  • 創建對服務器的連接時,對 getrequeststream 的調用可能會阻塞。應考慮利用 begingetrequeststream 和 endgetrequeststream 方法對流使用異步請求。

[c#]
// create a response object.
webresponse response = request.getresponse();
// get a readable stream from the server.
streamreader sr =
   new streamreader(response.getresponsestream(), encoding.ascii);
// use the stream. remember when you are through with the stream to close
s


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 自贡市| 榆中县| 嘉善县| 双流县| 稷山县| 新绛县| 藁城市| 宜城市| 桐城市| 九龙坡区| 贵港市| 安陆市| 潢川县| 五华县| 陆川县| 邵武市| 永丰县| 濉溪县| 随州市| 依兰县| 河津市| 广安市| 邹城市| 贵州省| 十堰市| 棋牌| 南康市| 志丹县| 漳平市| 恩平市| 疏附县| 宣化县| 浮山县| 东山县| 镇赉县| 定襄县| 梁山县| 建水县| 博野县| 晋中市| 庄浪县|