读取远程xml文件内容
2008-04-08 11:15
393 查看
方法1:读取远程服务器上xml内容到dataset中,一开始直接用dataset的readxml(url)方法读取,总是出现"需要代理身份验证407"错误,用在程序中声明全局代理可以实现.
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
方法2:用dataset中的readxml(io.stream)方法就可以读取到dataset中了
imports system.Net
Public 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
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
方法2:用dataset中的readxml(io.stream)方法就可以读取到dataset中了
imports system.Net
Public 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
相关文章推荐
- LINQ--利用LINQ读取XML文件中的内容
- 我学Flash/Flex(2):AS3读取XML文件内容
- jQuery读取XML文件内容的方法
- java读取文件夹里的所有xml文件内容
- java遍历读取xml文件内容
- java 遍历读取xml文件内容
- C#读取剪贴板内容并把内容记录在XML文件中
- asp.net 上传XML,txt 直接读取文件内容
- 读取远程xml文件
- 如何读取XML文件内容!
- Spring依赖注入的XML配置文件的实现思想(1)——准备工作:如何读取XML的文件的内容
- VB.NET 从XML文件中读取内容到JSON
- 读取xml文件内容,并将其内容以treeview的方式展现出来
- 关于如何使用vc6.0读取xml文件中的内容
- Android读取资源文件——读取原始Xml内容和使用Menu文件
- 读取含有命名空间xml文件内容
- C++下通过MSXML读取XML文件内容
- TinyXML读取XML文件内容 [大三四八九月实习]
- vb.net读取xml文件中的节点内容
- 一个读取xml文件内容的类