.NET 存取 web

技術筆記
.NET framework 存取 web 的 class 大概有以下幾種

WebClient

WebRequest

HttpWebRequest

其實我也分不出來有什麼不同



不過 WebClient 感覺是比較簡單的模組

可以直接提供下載某個URL到指定的檔案 (DownloadFile)

或是讀到一個 char array  (DownloadData)

如果要POST的話

就用 UploadData(URL, method, data)

就可以模擬 form 登入了

Dim WebClient As New System.Net.WebClient
WebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
Dim ASCIIEncoding As New System.Text.ASCIIEncoding
Dim PostData As Byte() = ASCIIEncoding.GetBytes("Account=" & Account & "Password=" & Password)
Dim ResultHTML As String =
ASCIIEncoding.GetChars(WebClient.UploadData(LogInURL, "POST", PostData))

UploadData 呼叫完畢以後 也會傳回頁面

可以拿來接收桌布

所以要把收到的 Byte 寫入檔案

BinaryWriter^ bw = gcnew BinaryWriter(fileName);
bw->WriteLine("A text file is born!");
bw->Write("You can use WriteLine");
bw->WriteLine("...or just Write");
bw->WriteLine("and do {0} output too.", "formatted");
bw->WriteLine("You can also send non-text objects:");
bw->WriteLine(DateTime::Now);
bw->Close();

要用 BinaryWriter, 原本在網路上 copy 的範例是 StreamWriter

那個只能寫 text = =