需要做一个提取网页内容的东东,在网上查了一些资料, 在MSDN里查了一些,仅仅是做个备份,方便以后使用,匆匆忙忙发上来,也没做整理,自己可以看懂就可以了,嘿嘿
默认的GET 好像比较简单,唯一需要注意的就是 字符集的转换,否则中文的显示会有问题
Dim WebReq As Net.HttpWebRequest
Dim strUrl As String
strUrl = Trim(txtAddress.Text) & "?" & _
"strDistrictSmall=" & System.Web.HttpUtility.UrlEncode(txtPara1.Text, System.Text.Encoding.GetEncoding("GB2312")) & _
"&strDistrict=" & System.Web.HttpUtility.UrlEncode(Txtpara2.Text, System.Text.Encoding.GetEncoding("GB2312"))
WebReq = CType(WebRequest.Create(strUrl), HttpWebRequest)
WebReq.Headers("Accept-Language") = "zh-CN" 'req.Headers("Accept-Language") = "zh-CN"
'WebReq.Headers.Add("Accept-Encoding", "utf-8")
Dim WebRes As Net.HttpWebResponse = WebReq.GetResponse()
Dim Readr As New IO.StreamReader(WebRes.GetResponseStream, System.Text.Encoding.GetEncoding("GB2312"))
TextBox1.Text = Readr.ReadToEnd
MsgBox("Complete")
POST稍微复杂一点点,个人出错的地方就是 WebRequest 的 Method 属性没设置 (MSDN资料中也没设置)
Dim WebReq As Net.HttpWebRequest
Dim strUrl As String
strUrl = "strDistrictSmall=" & System.Web.HttpUtility.UrlEncode(txtPara1.Text, System.Text.Encoding.GetEncoding("GB2312")) & _
"&strDistrict=" & System.Web.HttpUtility.UrlEncode(Txtpara2.Text, System.Text.Encoding.GetEncoding("GB2312"))
WebReq = CType(WebRequest.Create(Trim(txtAddress.Text)), HttpWebRequest)
'WebReq.Headers("Accept-Language") = "zh-CN" 'req.Headers("Accept-Language") = "zh-CN"
'WebReq.Headers.Add("Accept-Encoding", "utf-8")
Dim Postbyte As Byte() = Encoding.ASCII.GetBytes(strUrl)
WebReq.ContentType = "application/x-www-form-urlencoded"
WebReq.ContentLength = Postbyte.Length
WebReq.Method = "POST"
Dim NewStream As IO.Stream = WebReq.GetRequestStream()
NewStream.Write(Postbyte, 0, Postbyte.Length)
NewStream.Close()
Dim WebRes As Net.HttpWebResponse = WebReq.GetResponse
Dim Readr As New IO.StreamReader(WebRes.GetResponseStream, System.Text.Encoding.GetEncoding("GB2312"))
TextBox1.Text = Readr.ReadToEnd
Readr.Close()
MsgBox("Complete")