所以我有一个很大的Web浏览器项目,并且一切似乎都正常。 我具有一项功能,当用户单击特定按钮时,该功能可使用户获取有关我的网站的新闻。 这种工作方式应该是从我的保管箱中的txt文件中读取文本,然后在我的应用程序的richtextbox中显示该文本。
我这样做的方法是,我只是将一个txt文件上传到我的Dropbox文件夹中,并给它一个永不更改的名称,以便与我的代码一起使用,然后我复制了此文件的共享链接并将其放在代码。
现在,每当我要更新新闻时,我只需要在Dropbox文件夹中编辑txt文件,并保持相同的文件名,以使链接保持不变并且不会更改,因此允许我的应用程序正确更新新闻。
我在我的笔记本电脑上进行了测试(我在上面进行了所有编程),并且效果很好。我在旧台式机上进行了测试,效果很好。我有来自世界各地的一些朋友对此进行了测试,并在此发生了问题。它对某些人来说非常有用,而对于另一些人则失败并给出了错误...(无法解析主机名) 这不仅使我的应用程序中的新闻功能不可用,而且使我也无法使用的自动更新功能,因为它也使用相同的方法。
现在让我们进入代码,请帮助找出问题所在...
以下是可用于从Dropbox下载文件的主要代码:
Private Sub Download_Dropbox(URL As String, FileName As String)
Dim Data() = HTTP_Get(URL)
File.WriteAllBytes(FileName, Data)
End Sub
Private Function HTTP_Get(Page As String) As Byte()
Dim Request As HttpWebRequest = WebRequest.Create(Page)
Request.Method = "GET"
Request.KeepAlive = True
Request.ContentType = "application/x-www-form-urlencoded"
Request.UserAgent = "Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36"
Request.AllowAutoRedirect = True
Dim Response As HttpWebResponse = Request.GetResponse()
Dim Data As Stream = Response.GetResponseStream()
Dim Bytes() As Byte
Using Writer As New MemoryStream
Dim Buffer(&HFFF) As Byte
Do
Dim BytesRead As Long = Data.Read(Buffer, 0, Buffer.Length)
If BytesRead > 0 Then Writer.Write(Buffer, 0, BytesRead) Else Exit Do
Loop
Bytes = Writer.ToArray()
End Using
Return Bytes
End Function
这是尝试更新新闻(读取txt文件)的按钮的代码:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Form1.IsConnectionAvailable = True Then
Try
'Download_Dropbox("https://www.dropbox.com/s/8m2apm0x0rh91e0/orbitnews.txt?dl=1", CurDir & "\orbitnews.txt")
Dim ABC As String
Dim myWebClient As New System.Net.WebClient
Dim file As New System.IO.StreamReader(myWebClient.OpenRead("https://www.dropbox.com/s/8m2apm0x0rh91e0/orbitnews.txt?dl=1"))
ABC = file.ReadToEnd
file.Close()
newsshower.Text = ABC
My.Settings.oldnews = ABC
dater.Text = (DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"))
My.Settings.newsrefdate = dater.Text
My.Settings.Save()
Timer1.Enabled = True
Label2.Visible = True
If newsshower.Text.Contains("Click the button below to update your browser now!") Then
If newsshower.Text.Contains(Application.ProductVersion) Then
My.Settings.newsupdate = False
updatetoggler.Visible = False
Else
updatetoggler.Visible = True
My.Settings.newsupdate = True
My.Settings.Save()
End If
End If
Catch ex As Exception
MsgBox("An error has occured while refreshing news." & vbCrLf & "Please contact customer support and send them the following:" & vbCrLf & ex.ToString & vbCrLf & vbCrLf & "Customer Support E-mail:" & vbCrLf & "omaradoinc@hotmail.com")
End Try
Else
MsgBox("You aren't connected to the internet." & vbCrLf & "Please connect to the internet to be able to refresh the news.")
End If
End Sub
这是从Dropbox下载文件的代码(当程序自行更新时会发生这种情况):
Public Sub mainupdate()
Download_Dropbox("https://www.dropbox.com/s/eomar7a70hokm0l/Orbit.exe?dl=1", CurDir & "\Orbit.exe")
Download_Dropbox("https://www.dropbox.com/s/ij1qceoe5kr6tmp/Orbit.exe.config?dl=1", CurDir & "\Orbit.exe.config")
Download_Dropbox("https://www.dropbox.com/s/em4mo4lsoswba4p/Orbit.pdb?dl=1", CurDir & "\Orbit.pdb")
Download_Dropbox("https://www.dropbox.com/s/g0361qpzvq74ge4/Orbit.vshost.exe?dl=1", CurDir & "\Orbit.vshost.exe")
Download_Dropbox("https://www.dropbox.com/s/o5bbkn72cbs9bo7/Orbit.vshost.exe.config?dl=1", CurDir & "\Orbit.vshost.exe.config")
Download_Dropbox("https://www.dropbox.com/s/wzsyzjczibwa9sz/Orbit.xml?dl=1", CurDir & "\Orbit.xml")
Download_Dropbox("https://www.dropbox.com/s/l0ogz6kxrn951zv/OWBV.txt?dl=1", CurDir & "\OWBV.txt")
End Sub
Public Sub othersupdate()
Download_Dropbox("https://www.dropbox.com/s/lyv0kdmpi85rbdp/libzplay.dll?dl=1", CurDir & "\libzplay.dll")
End Sub
Public Sub soundsupdate()
Download_Dropbox("https://www.dropbox.com/s/hfl1vidanwecad4/not.wav?dl=1", CurDir & "\not.wav")
Download_Dropbox("https://www.dropbox.com/s/cfroifknr8zmnub/pokked.wav?dl=1", CurDir & "\pokked.wav")
Download_Dropbox("https://www.dropbox.com/s/c73af0a30hxg7gp/screenshotsound.wav?dl=1", CurDir & "\screenshotsound.wav")
Download_Dropbox("https://www.dropbox.com/s/ps5ztudy9cwvwnl/timerend.wav?dl=1", CurDir & "\timerend.wav")
End Sub
Private Sub Download_Dropbox(URL As String, FileName As String)
Dim Data() = HTTP_Get(URL)
File.WriteAllBytes(FileName, Data)
End Sub
这是Form1中IsConnectionAvailable()
的代码:
Public Function IsConnectionAvailable() As Boolean
Dim objUrl As New System.Uri("http://www.google.com")
Dim objWebReq As System.Net.WebRequest
objWebReq = System.Net.WebRequest.Create(objUrl)
Dim objresp As System.Net.WebResponse
Try
objresp = objWebReq.GetResponse
objresp.Close()
objresp = Nothing
Return True
Catch ex As Exception
objresp = Nothing
objWebReq = Nothing
Return False
End Try
End Function
这是我的更新客户端的全部代码,完全无法从Dropbox下载任何文件或读取文本:
Option Explicit On
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Public Class Form1
Dim Listener As New TcpListener(8000)
Dim Client As TcpClient
Dim CurDir As String = My.Application.Info.DirectoryPath
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
updatecheck()
End Sub
Private Sub helper_Click(sender As Object, e As EventArgs) Handles helper.Click
helpform.Show()
End Sub
Public Sub mainupdate()
Download_Dropbox("https://www.dropbox.com/s/eomar7a70hokm0l/Orbit.exe?dl=1", CurDir & "\Orbit.exe")
Download_Dropbox("https://www.dropbox.com/s/ij1qceoe5kr6tmp/Orbit.exe.config?dl=1", CurDir & "\Orbit.exe.config")
Download_Dropbox("https://www.dropbox.com/s/em4mo4lsoswba4p/Orbit.pdb?dl=1", CurDir & "\Orbit.pdb")
Download_Dropbox("https://www.dropbox.com/s/g0361qpzvq74ge4/Orbit.vshost.exe?dl=1", CurDir & "\Orbit.vshost.exe")
Download_Dropbox("https://www.dropbox.com/s/o5bbkn72cbs9bo7/Orbit.vshost.exe.config?dl=1", CurDir & "\Orbit.vshost.exe.config")
Download_Dropbox("https://www.dropbox.com/s/wzsyzjczibwa9sz/Orbit.xml?dl=1", CurDir & "\Orbit.xml")
Download_Dropbox("https://www.dropbox.com/s/l0ogz6kxrn951zv/OWBV.txt?dl=1", CurDir & "\OWBV.txt")
End Sub
Public Sub othersupdate()
Download_Dropbox("https://www.dropbox.com/s/lyv0kdmpi85rbdp/libzplay.dll?dl=1", CurDir & "\libzplay.dll")
End Sub
Public Sub soundsupdate()
Download_Dropbox("https://www.dropbox.com/s/hfl1vidanwecad4/not.wav?dl=1", CurDir & "\not.wav")
Download_Dropbox("https://www.dropbox.com/s/cfroifknr8zmnub/pokked.wav?dl=1", CurDir & "\pokked.wav")
Download_Dropbox("https://www.dropbox.com/s/c73af0a30hxg7gp/screenshotsound.wav?dl=1", CurDir & "\screenshotsound.wav")
Download_Dropbox("https://www.dropbox.com/s/ps5ztudy9cwvwnl/timerend.wav?dl=1", CurDir & "\timerend.wav")
End Sub
Private Sub Download_Dropbox(URL As String, FileName As String)
Dim Data() = HTTP_Get(URL)
File.WriteAllBytes(FileName, Data)
End Sub
Private Function HTTP_Get(Page As String) As Byte()
Dim Request As HttpWebRequest = WebRequest.Create(Page)
Request.Method = "GET"
Request.KeepAlive = True
Request.ContentType = "application/x-www-form-urlencoded"
Request.UserAgent = "Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36"
Request.AllowAutoRedirect = True
Dim Response As HttpWebResponse = Request.GetResponse()
Dim Data As Stream = Response.GetResponseStream()
Dim Bytes() As Byte
Using Writer As New MemoryStream
Dim Buffer(&HFFF) As Byte
Do
Dim BytesRead As Long = Data.Read(Buffer, 0, Buffer.Length)
If BytesRead > 0 Then Writer.Write(Buffer, 0, BytesRead) Else Exit Do
Loop
Bytes = Writer.ToArray()
End Using
Return Bytes
End Function
Public Function IsConnectionAvailable() As Boolean
Dim objUrl As New System.Uri("http://www.google.com")
Dim objWebReq As System.Net.WebRequest
objWebReq = System.Net.WebRequest.Create(objUrl)
Dim objresp As System.Net.WebResponse
Try
objresp = objWebReq.GetResponse
objresp.Close()
objresp = Nothing
Return True
Catch ex As Exception
objresp = Nothing
objWebReq = Nothing
Return False
End Try
End Function
Public Sub updatecheck()
Try
If IsConnectionAvailable() = True Then
Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://pastebin.com/wCt79dEc")
Dim response As System.Net.HttpWebResponse = request.GetResponse()
Dim sr As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())
Dim newestversion As String = sr.ReadToEnd()
Dim fileContents As String
Try
fileContents = My.Computer.FileSystem.ReadAllText(CurDir & "\OWBV.txt")
If newestversion.Contains(fileContents) Then
updater.Enabled = False
Else
End If
Catch ex As Exception
MsgBox("Orbit version file not found. Update button disabled." & "You can do one of the following 2 methods to fix this error. (Administrator rights might be required)" & vbCrLf & "1- Please open Orbit and then head to (Settings) , then check the option (Enable Console) and save changes. " & vbCrLf & "2- Open (File) Menu and launch (Browser Console)" & vbCrLf & "3- Type in the following command without the brackets: ( /vfcreate ) and then press Enter on your keyboard." & vbCrLf & "If the process is successful , then please launch the update client again either through Orbit itself or close Orbit and launch it manually by running (OWBUpdater.exe) in the application directory. " & vbCrLf & "If the process isn't successful , then please contact customer support at: omaradoinc@hotmail.com" & vbCrLf & "The other method to fix this error is:" & vbCrLf & "1- Navigate to Orbit Installation directory/folder." & vbCrLf & "2-Create a notepad (.txt file) and name that file the following name without the brackets: (OWBV)" & vbCrLf & "3- Open the file and type Orbit's old version that you have or as instructed by customer support , save the changes." & vbCrLf & "If you fail to fix this error , please contact customer support at:" & vbCrLf & "omaradoinc@hotmail.com")
updater.Enabled = False
End Try
Else
updater.Enabled = False
repairer.Enabled = False
intnot.Visible = True
Me.Text = "Orbit Web Browser Update Client (No Internet connection)"
End If
Catch ex As Exception
MsgBox("An Error Has Occured While Checking For Updates..." & vbCrLf & "This May Happen Because Of a Load On The Server" & vbCrLf & "Please Try Again Later" & vbCrLf & "If The Problems Still Exists Then Please Report This To:" & vbCrLf & "omaradoinc@hotmail.com")
End Try
End Sub
Private Sub updater_Click(sender As Object, e As EventArgs) Handles updater.Click
Try
m1.Visible = False
m2.Visible = False
progressor.Visible = True
Me.Enabled = False
mainupdate()
Process.Start(CurDir & "\Orbit.exe")
MsgBox("Orbit was successfully updated to the latest version.")
Me.Close()
Catch ex As Exception
m1.Visible = True
m2.Visible = True
progressor.Visible = False
Me.Enabled = True
My.Computer.FileSystem.WriteAllText(CurDir & "\errorlog.txt", ex.ToString, True)
MsgBox("Update failed." & vbCrLf & "A log file was created in the application directory , Please send it to customer support at:" & vbCrLf & "omaradoinc@hotmail.com")
End Try
End Sub
Private Sub repairer_Click(sender As Object, e As EventArgs) Handles repairer.Click
Try
m1.Visible = False
m2.Visible = False
progressor.Visible = True
Me.Enabled = False
othersupdate()
soundsupdate()
Process.Start(CurDir & "\Orbit.exe")
MsgBox("Orbit was successfully repaired.")
Me.Close()
Catch ex As Exception
m1.Visible = True
m2.Visible = True
progressor.Visible = False
Me.Enabled = True
My.Computer.FileSystem.WriteAllText(CurDir & "\errorlog.txt", ex.ToString, True)
MsgBox("Repair failed." & vbCrLf & "A log file was created in the application directory , Please send it to customer support at:" & vbCrLf & "omaradoinc@hotmail.com")
End Try
End Sub
Private Sub remover_Click(sender As Object, e As EventArgs) Handles remover.Click
Try
Listener.Start()
m1.Visible = False
m2.Visible = False
progressor.Visible = True
My.Computer.FileSystem.WriteAllText(CurDir & "\uninstcmd.txt", "cmd:/send\performfactoryreset", True)
Timer1.Enabled = True
Timer1.Start()
Process.Start(CurDir & "\Orbit.exe")
updater.Enabled = False
repairer.Enabled = False
remover.Enabled = False
Catch ex As Exception
Listener.Stop()
Timer1.Stop()
Timer1.Enabled = False
m1.Visible = True
m2.Visible = True
progressor.Visible = False
MsgBox("Orbit.exe wasn't found." & vbCrLf & "Unable to complete uninstallation.")
End Try
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim Message As String
Dim nStart As Integer
Dim nLast As Integer
If Listener.Pending = True Then
Message = ""
Client = Listener.AcceptTcpClient
Dim Reader As New StreamReader(Client.GetStream())
While Reader.Peek > -1
Message &= Convert.ToChar(Reader.Read()).ToString
End While
If Message.Contains("</>") Then
nStart = InStr(Message, "</>") + 4
nLast = InStr(Message, "<\>")
Message = Mid(Message, nStart, nLast - nStart)
End If
If Message.Contains("cmd:/send\completedfactoryreset") Then
Try
My.Computer.FileSystem.WriteAllText(CurDir & "\uninstcmd.txt", "Nothing here.", True)
MsgBox("Orbit settings have been successfully cleaned." & vbCrLf & "Thanks for using Orbit , This client will now exit and launch the main uninstaller which will completely remove Orbit from your PC." & vbCrLf & "For customer support , Contact: omaradoinc@hotmail.com")
Process.Start(CurDir & "\Uninstal.exe")
Me.Close()
Catch ex As Exception
MsgBox("Orbit settings have been successfully cleaned." & vbCrLf & "Thanks for using Orbit , This client will now exit and launch the main uninstaller which will completely remove Orbit from your PC." & vbCrLf & "For customer support , Contact: omaradoinc@hotmail.com")
Process.Start(CurDir & "\Uninstal.exe")
Me.Close()
End Try
End If
End If
End Sub
End Class
现在我不记得错误所在的行,但是我将尝试让我的朋友重新测试并获得确切的错误,因为我已经为我的应用程序设置了很多Try Catch块和容易捕获错误的方法在txt文件中并将其发送给我,这样在我得到完整错误之前应该不会太麻烦。 目前是否有什么空间不足或可以改进以更好地工作的东西?