从URL下载图像文件时"The remote server returned an error (502) Bad Gateway"



我正在尝试从网站下载图像,但是它给了我错误,即(远程服务器返回了一个错误:(502)坏网关。我在Internet上进行了很多搜索,但没有得到精确的解决方案。该代码在某些网站上工作,但在很少的网站上显示上述错误。请检查我的下面代码...

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim filename As String = Application.StartupPath
    If Not filename.EndsWith("") Then filename &= ""
    txtLocalFile.Text = "D:a.jpg" 'filename & " "
    txtRemoteFile.Text = "http://bseworld.bseindia.com/eventgallery/cricket2015/19Feb2015/2.jpg"
End Sub

Private Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click
    Application.DoEvents()
    Try
        Dim web_client As WebClient = New WebClient
        Dim wp = New WebProxy("proxy ip", proxy_port)
        wp.Credentials = New NetworkCredential("uname", "password", "domainname")
        web_client.Proxy = wp
        web_client.DownloadFile(txtRemoteFile.Text, txtLocalFile.Text)
        MessageBox.Show("Done")
    Catch ex As Exception
    End Try
    End Sub

您能告诉我丢失代码的哪一部分或我必须更改。

我做了一些rnd,最后我的代码正常工作。

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim filename As String = Application.StartupPath
    If Not filename.EndsWith("") Then filename &= ""
    txtLocalFile.Text = "" 'filename & " "
    txtRemoteFile.Text = ""
End Sub
Private Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click
    Application.DoEvents()
    Dim web_client As WebClient = New WebClient
    Try
        web_client.UseDefaultCredentials = True
        web_client.DownloadFile(txtRemoteFile.Text, txtLocalFile.Text)
        MessageBox.Show("Done")
    Catch ex As Exception
        Dim wp = New WebProxy("Proxy IP", Proxy_port)
        wp.Credentials = New NetworkCredential("MachineUsername", "Machine Password", "Domain Name")
        web_client.Proxy = wp
        web_client.DownloadFile(txtRemoteFile.Text, txtLocalFile.Text)
        MessageBox.Show("Done")
    End Try
End Sub

相关内容

最新更新