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


主核心函数:

Public Sub downloadFile(ByVal Url As String, ByVal normalUserName As String, ByVal normalPassword As String, ByVal proxy As Boolean, ByVal proxyServer As String, ByVal proxyPort As Integer, ByVal proxyUserName As String, ByVal proxyPassword As String, ByVal Directory As String)
Dim pfileWriter As StreamWriter = Nothing
Dim sFile As String
Dim length As Integer = 1024
Dim Buffer(1025) As 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 <> Nothing) Then
request.Credentials = New System.Net.CredentialCache
End If
If (proxyUserName <> Nothing) Then
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) > 1) Then
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 >= 3) Then
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 = 7) Then ' 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

程序界面如下:






















































































































