WebRequest例子

闲暇之余,自己做了一个简单的优快云论坛最新问题浏览器,以便及时掌握“最新动态”,并增加了回贴机器人的功能。
程序界面如下:


主核心函数:

        
Public Sub downloadFile(ByVal Url As StringByVal normalUserName As StringByVal normalPassword As StringByVal proxy As BooleanByVal proxyServer As StringByVal proxyPort As IntegerByVal proxyUserName As StringByVal proxyPassword As StringByVal Directory As String)
        
Dim pfileWriter As StreamWriter = Nothing
        
Dim sFile As String
        
Dim length As Integer = 1024
        
Dim Buffer(1025As Char
        
Dim bytesread As Integer = 0
        
Dim ResolvedURI As Boolean = False

        
While (Not ResolvedURI)
            
Try
                
If proxy Then
                    
Dim proxyObject As System.Net.WebProxy = GetDefaultProxy()
                    proxyObject.BypassProxyOnLocal 
= True
                    GlobalProxySelection.Select 
= proxyObject
                
End If

                
'Create the request object
                Dim request As HttpWebRequest = CType(WebRequest.Create(Url), HttpWebRequest)
                request.AllowAutoRedirect 
= True
                
If (normalUserName <> NothingThen
                    request.Credentials 
= New System.Net.CredentialCache
                
End If
                
If (proxyUserName <> NothingThen
                    request.Credentials 
= New System.Net.CredentialCache
                
End If
                
'Create the response object
                Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)

                
Dim absolutePath, fileName As String
                absolutePath 
= response.ResponseUri.AbsolutePath
                
'parse it from end to get the actual file name
                Dim i As Integer = InStrRev(absolutePath, "/")
                
If (i > 0 And Len(absolutePath) > 1Then
                    fileName 
= absolutePath.Substring(i)
                
Else
                    fileName 
= "default.htm"
                
End If
                
'Console.WriteLine("file Name is {0}", fileName)
                'Console.WriteLine(response.StatusDescription)

                
'Successfully resolved the URI
                ResolvedURI = True

                
Dim myDirectory As String

                
If (Directory.Length >= 3Then
                    
Dim j As Integer = Directory.LastIndexOf("")
                    
If (j = Directory.Length) Then
                        myDirectory 
= Directory
                        sFile 
= Directory & fileName
                    
Else
                        
If myDirectory = "" Then myDirectory = Directory
                        
If myDirectory.Substring(1<> "" Then myDirectory = Directory & ""
                        sFile 
= myDirectory & fileName
                    
End If
                
End If

                
Try
                    
If (File.Exists(sFile)) Then
                        
Select Case (MessageBox.Show("File Already Exist..Delete it..""File Exists", MessageBoxButtons.OKCancel))
                            
Case DialogResult.OK
                                File.Delete(sFile)
                            
Case Else
                                
Throw New ApplicationException("File exists change the file name before downloading")
                        
End Select
                    
End If

                    
Try
                        
Dim f As FileIOPermission = New FileIOPermission(FileIOPermissionAccess.AllAccess, myDirectory)
                        pfileWriter 
= New StreamWriter(New FileStream(sFile, FileMode.CreateNew, FileAccess.Write, FileShare.None))
                    
Catch eArgNull As ArgumentNullException
                        ResolvedURI 
= True
                        
MsgBox("null")
                    
End Try
                
Catch eIO As IOException
                    ResolvedURI 
= True
                    
MsgBox("IO error")
                
End Try

                
Dim sr As StreamReader = New StreamReader(response.GetResponseStream(), System.Text.Encoding.Unicode)
                
'Read from the stream and write any data to the console
                bytesread = sr.Read(Buffer, 0, length)
                pfileWriter.Write(Buffer, 
0, bytesread)

                
While (bytesread > 0)
                    
'Console.Write( Buffer,0, bytesread);
                    pfileWriter.Write(Buffer, 0, bytesread)
                    bytesread 
= sr.Read(Buffer, 0, length)
                
End While

            
Catch WebExcp As WebException
                
'If you get to Me point, the exception has been caught
                MsgBox("A WebException has been caught!" & Chr(13& WebExcp.ToString())
                
Dim st As Integer = Convert.ToInt32(WebExcp.Status)
                
If (st = 7Then ' 7 indicates a protocol error, thus a WebResonse object should exist
                    MsgBox("The protocol error returned by the server is " & Chr(10& WebExcp.Message)
                
End If
                
'throw back to indicate user that error has occured
                ResolvedURI = True
            
Catch UriExcp As UriFormatException
                
' If you get to Me point, the exception has been caught
                Console.WriteLine("A URIFormatException has been caught!" & Chr(10& UriExcp.ToString())
                ResolvedURI 
= True
            
Catch eOthers As Exception
                
'throw back to indicate user that error has occured
                ResolvedURI = True
                
MsgBox(eOthers.ToString)
            
Finally
                
If Not pfileWriter Is Nothing Then
                    pfileWriter.Close()
                
End If
            
End Try
            
MsgBox("Resolved Complate")
        
End While
    
End Sub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值