日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HttpWebRequest WebResponse 对象简单了解

發(fā)布時間:2025/5/22 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HttpWebRequest WebResponse 对象简单了解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.HTTP亦即Hpyer Text Transfer Protocal的縮寫,它是現(xiàn)代互聯(lián)網(wǎng)上最重要的一種網(wǎng)絡(luò)協(xié)議,超文本傳輸協(xié)議位于TCP/IP協(xié)議的應用層,是一個面向?? 無連接、簡單、快速的C/S結(jié)構(gòu)的協(xié)議 。HTTP的工作過程大體上分連接、請求、響應和斷開連接

2..NET類庫中提供了WebRequest和WebResponse就是利用這兩個類實現(xiàn)的網(wǎng)絡(luò)功能

?HttpWebRequest:HttpWebRequest 類對 WebRequest 中定義的屬性和方法提供支持,也對使用戶能夠直接與使用

?????????????????????????? HTTP 的服務器交互的附加 屬性和方法提供支持。

?????????????????????????? http://msdn.microsoft.com/zh-cn/library/system.net.httpwebrequest.connection(v=VS.80).aspx

WebResponse 類是 abstract 基類,協(xié)議特定的響應類從該抽象基類派生。應用程序可以使用 WebResponse 類的實例以協(xié)議不可知的方式參與請求和響應事務,而從 WebResponse 派生的協(xié)議特定的類攜帶請求的詳細信息

?????????????????????????? http://msdn.microsoft.com/zh-cn/library/system.net.webresponse(v=VS.80).aspx

3.簡單應用:

???? 導入命名空間:using System.Net?? using System.IO?

??? 程序使用 HTTP 協(xié)議和服務器交互主要是進行數(shù)據(jù)的提交,通常數(shù)據(jù)的提交是通過 GET 和 POST 兩種方式

????//創(chuàng)建一個url新的httpwebrequest 對象?

??? HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:1782/WebForm1.aspx?");

?? //設(shè)置myHttpWebRequest 對象屬性

?? ?myHttpWebRequest.Method = "post"

? ? myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";

?? myHttpWebRequest.UserAgent = ".NET Framework Client";

??? //輸入?yún)?shù) id=中文和Econding

????string inputData = System.Console.ReadLine();?

?? //創(chuàng)建utf-8?或者GB2312 來處理中文?

?? // 處理英文就是這個就可以

? //ASCIIEncoding encoding = new ASCIIEncoding();

? ?Encoding myEncoding = Encoding.GetEncoding("utf-8");

? byte[] byteinputdata = myEncoding.GetBytes(inputData);

? //寫入當前流對象發(fā)送個服務器

? myHttpWebRequest.ContentLength = byteinputdata.Length;

? Stream newStream = myHttpWebRequest.GetRequestStream();

? newStream.Write(byteinputdata, 0, byteinputdata.Length); ?

??newStream.Close();

?

?//獲取服務器響應的結(jié)果(根據(jù)條件獲取對象解析返回結(jié)果)

?? ?HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

??? Stream streamResponse = myHttpWebResponse.GetResponseStream(); ?

??? StreamReader streamRead = new StreamReader(streamResponse);???????

??? Char[] readBuff = new Char[256]; ?

??? int count = streamRead.Read(readBuff, 0, 256);

????System.Console.WriteLine("\nThe contents of HTML Page are :\n"); ???

?????while (count > 0) ???????????

??? { ??????????????

????? String outputData = new String(readBuff, 0, count); ??

???? ?System.Console.Write(outputData); ?????????????

???? ?count = streamRead.Read(readBuff, 0, 256); ?????

?????}????????????

??? streamRead.Close(); ?????????

??? streamResponse.Close();

?? myHttpWebResponse.Close();

?

?

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/linsu/archive/2011/12/04/2276124.html

總結(jié)

以上是生活随笔為你收集整理的HttpWebRequest WebResponse 对象简单了解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。