標籤:style blog http io ar color os 使用 sp
轉載:http://www.cnblogs.com/yiki/archive/2007/08/28/872528.html
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(addr);
// Downloads the XML file from the specified server.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
System.IO.StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));
Console.Write(sr.ReadToEnd());
sr.Close();
response.Close();
1.擷取http地址上的圖片
http://www.cnblogs.com/fooo/archive/2007/06/28/798335.html
HttpWebResponse resp;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://www.csdn.net/Images/logo_csdn.gif");
req.Timeout = 150000;
resp = (HttpWebResponse)req.GetResponse();
System.Drawing.Image img;
img = new System.Drawing.Bitmap(resp.GetResponseStream());
2.
HttpWebRequest&HttpWebResponse Headers
介紹
這裡簡要介紹如何使用HttpWebRequest&HttpWebResponse兩個對象與HTTP伺服器進行直接互動的過程.HttpWebRequest類對WebRequest中定義的屬性和方法提供支援,在使用HttpWebRequest對象向HTTP伺服器發起請求時請不要使用HttpWebRequest對象的建構函式,而應該使用WebRequest.Create()方法來初始化新的HttpWebRequest對象.如果統一資源識別項方案是"http://"或"https://"時,Create()則返回HttpWebResponse對象.
代碼
首先,我們需要建立一個新的HttpWebRequest對象,代碼如下:
HttpWebRequest myrequest = (HttpWebRequest)WebRequest.Create(new Uri("urlstring"));
注意:上文中已經提到過不要使用HttpWebRequest的建構函式來建立對象;使用WebRequest.Create()方法初始化HttpWebRequest對象時應該對其進行類型轉換.
接下來可以對新初始化的對象進行簡單操作,比如可以設定它的標題屬性,
下表列出了由屬性或方法設定或由系統設定的標題:
|
標題 |
設定方法 |
| Accept |
由Accept屬性設定 |
| Connection |
由Connection屬性和KeepAlive屬性設定 |
| Content-Length |
由ContentLength屬性設定 |
| Content-Type |
由ContentType屬性設定 |
| Expect |
由Expect屬性設定 |
| Date |
由系統設定為當前日期 |
| Host |
由系統設定為當前主機資訊 |
| If-Modified-Since |
由IfModifiedSince屬性設定 |
| Range |
由Range屬性設定 |
| Transfer-Encoding |
由TransferEncoding屬性設定 |
| Referer |
由Referer屬性設定 |
| User-Agent |
由UserAgent屬性設定 |
注意:HttpWebRequest自動註冊.使用以"http://"或"https://"開頭的URL之前,不需要調用RegisterPrefix方法來註冊
System.Net.HttpWebRequest選自:
MSDN:ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.NETDEVFX.v20.chs/cpref10/html/T_System_Net_HttpWebRequest.htm
在完成對伺服器的請求之後,我們需要擷取從伺服器中返回的資訊,正如前面提到過的,使用HttpWebResponse類,具體做法參照如下代碼:
HttpWebResponse myresponse = (HttpWebResponse)myrequest.GetResponse();
同樣這裡我們需要使用HttpWebResponse進行類型轉換.到這裡,我們已經成功的獲得伺服器返回的資訊,一個myresponse對象,我們可以利用它來獲得更多詳細的資訊,比如從伺服器返回的Headers標題,還有其他的有用資訊.而在此我們只給您列出如何獲得來自伺服器響應的Headers標題資訊.我們的具體方法是這樣的:
#region GetHeaders
public static WebHeaderCollection GetHeaders(Uri uri)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
// 省略部分代碼......
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// 省略部分代碼......
return response.Headers;
}
#endregion
下面就是提供的main()函數:
// The app entry point
public static void Main(string[] args)
{
WebHeaderCollection headers = GetHeaders(new Uri("http://www.listim.net"));
string[] headkeys = headers.AllKeys;
Console.WriteLine("{0,-30}{1}", "Name", "Value");
Console.WriteLine("--------------------------------");
foreach (string s in headkeys)
{
Console.WriteLine("{0,-30}{1}", s, headers[s]);
}
}
以下是該程式顯示的:
3.
http://blog.csdn.net/Cheaper_c/archive/2004/10/19/142979.aspx
最近在為公司產品(ASP.NET B/S結構)進行升級的時候遇到了客戶的這樣一個需求.
系統提供會議投票這樣一種功能,在會議參與者對此次會議所要研究的合約進行研討之後,系統把參與者的意見(同意,不同意,有條件同意)和意見的詳細資料列出,供會議決策者參考,最後由會議決策者輸入決策意見.
其中每一筆合約都包括很多資訊,如:法人資料,兄弟公司資訊,擔保人資訊等等,使用者迫切需要儲存這一系列資訊的目前狀態.因為這些資訊在未來的系統使用過程中可能會發生變更,比如擔保人的名稱變更等等.
但是所有的這些資訊是由不同的功能結合起來組成的,如果想把這些資訊進行匯總,那麼重寫所有的資訊擷取方法是不太可行的,所以直接請求頁面,從伺服器返回的HTML資料流中擷取所需內容也許是一個可行的辦法.
首先在程式中維持一個所有資訊請求頁面地址的列表,然後迴圈這個列表,進行資訊的擷取.
在程式中用到了System.Net, System.IO命名空間.
public string getPageFromURL(string url)
{
string content = "";
// Create a new HttpWebRequest object.Make sure that
// a default proxy is set if you are behind a fure wall.
//其中,HttpWebRequest執行個體不使用HttpWebRequest的建構函式來建立,二是使用WebRequest的Create方法來建立.
HttpWebRequest myHttpWebRequest1 =(HttpWebRequest)WebRequest.Create(url);
//不維持與伺服器的請求狀態
myHttpWebRequest1.KeepAlive=false;
//建立一個HttpWebRequest對象
//Assign the response object of HttpWebRequest to a HttpWebResponse variable.\
HttpWebResponse myHttpWebResponse1;
try
{
//根據微軟MSDN上所說:"決不要直接建立HttpWebResponse的執行個體,要使用HttpWebRequest的GetResponse()方法返回的執行個體."具體的原因我也不清楚,可能HttpWebResponse類的建構函式中沒有實現HttpWebResponse執行個體的代碼吧.
myHttpWebResponse1 = (HttpWebResponse)myHttpWebRequest1.GetResponse();
//設定頁面的編碼模式
System.Text.Encoding utf8 = System.Text.Encoding.Default;
Stream streamResponse=myHttpWebResponse1.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse, utf8);
Char[] readBuff = new Char[256];
//這裡使用了StreamReader的Read()方法,參數意指從0開始讀取256個char到readByff中.
//Read()方法傳回值為指定的字串數組,當達到檔案或流的末尾使,方法返回0
int count = streamRead.Read( readBuff, 0, 256 );
while (count > 0)
{
String outputData = new String(readBuff, 0, count);
content += outputData;
count = streamRead.Read(readBuff, 0, 256);
}
myHttpWebResponse1.Close();
return(content);
}
catch(WebException ex)
{
content = "在請求URL為:" + url + "的頁面時產生錯誤,錯誤資訊為" + ex.ToString();
return(content);
}
}
HttpWebRequest 和 HttpWebResponse 的應用