【IT168技术文档】
方法1:读取远程服务器上xml内容到dataset中,一开始直接用dataset的readxml(url)方法读取,总是出现"需要代理身份验证407"错误,用在程序中声明全局代理可以实现.
方法2:用dataset中的readxml(io.stream)方法就可以读取到dataset中了Dim proxy As System.Net.WebProxy = New System.Net.WebProxy("61.144.254.101:80") 2 'proxy.Credentials = New System.Net.NetworkCredential("micheng", "123456789") 3 4 System.Net.GlobalProxySelection.Select = proxy
imports system.Net GetPageString 1Public Shared Function GetPageString()Function GetPageString(ByVal url As String, ByRef postMethod As String, ByRef cookies As CookieCollection, ByVal wp As WebProxy, Optional ByRef encType As String = "GB2312", Optional ByRef refer As String = "") As String 2 3 Dim streamData As Stream 4 5 If (url.StartsWith("http://") = False) Then 6 url = "http://" & url 7 End If 8 9 Dim hRqst As HttpWebRequest = HttpWebRequest.Create(url) 10 11 If cookies IsNot Nothing Then 12 hRqst.CookieContainer = New CookieContainer 13 hRqst.CookieContainer.Add(cookies) 14 End If 15 16 hRqst.ContentType = "application/x-www-form-urlencoded" 17 hRqst.Headers.Add("Accept-Language", "zh-cn") 18 19 If wp IsNot Nothing Then 20 hRqst.Proxy = wp 21 End If 22 23 If String.IsNullOrEmpty(postMethod) Then 24 hRqst.Method = "GET" 25 Else 26 hRqst.Method = postMethod 27 End If 28 29 ' Dim bt() As Byte 30 31 hRqst.AllowAutoRedirect = True 32 ' hRqst.AllowWriteStreamBuffering = True 33 ' bt = System.Text.Encoding.ASCII.GetBytes(postPara) 34 ' hRqst.ContentLength = bt.Length 35 hRqst.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" 36 ' hRqst.Referer = refer 37 ' hRqst.KeepAlive = False 38 ' hRqst.Timeout = 20000 39 ' streamData = hRqst.GetRequestStream() 40 ' streamData.Write(bt, 0, bt.Length) 41 ' streamData.Close() 42 43 Dim hRsp As HttpWebResponse 44 hRsp = hRqst.GetResponse() 45 streamData = hRsp.GetResponseStream 46 If cookies IsNot Nothing And hRsp.Cookies IsNot Nothing Then 47 cookies.Add(hRsp.Cookies) 48 End If 49 50 If (encType = "") Then 51 encType = "GB2312" 52 End If 53 54 Dim readStream As New IO.StreamReader(streamData, System.Text.Encoding.GetEncoding(encType)) 55 GetPageString = readStream.ReadToEnd() 56 streamData.Close() 57 58 End Function