禁止错误(403)通过Web客户端的DownloadFileAsync方法

时间:2014-07-06 10:55:26

标签: vb.net visual-studio-2010

过去两年我一直在通过这种方法下载文件,但现在却给出了这个禁止的错误(403)。这是地址:http://www.nseindia.com/content/historical/DERIVATIVES/2014/JUL/fo04JUL2014bhav.csv.zip。这就是代码:

wcNseFut.DownloadFileAsync(uriNseFut, strFullFileName)

strFullFileName是保存文件的路径。我已经尝试了很多,但还没有解决。请帮忙。 完整代码:

 If CheckValidations() = True Then
            If CheckNet() Then
                Dim strFullFileName As String

                dtBhav = CDate(dtBhavcopyDate.Value)
                btnNseFuturesDownload.Enabled = False
                btnNseFuturesDownload.Text = "Downloading....."

                AddHandler wcNseFut.DownloadFileCompleted, AddressOf wcNseFut_DownloadFileCompleted
                AddHandler wcNseFut.DownloadProgressChanged, AddressOf wcNseFut_DownloadProgressChanged

                MDIStockMIS.UStatusBar.Panels("Text").Text = ""
                MDIStockMIS.UStatusBar.Panels("Text").Text = "Downloading file ..........."

                ''-------- Download NSE future file
                ''-------- http://www.nseindia.com/content/historical/DERIVATIVES/2014/APR/fo11APR2014bhav.csv.zip
                Dim uriNseFut As New Uri(NseFuturesFTPAddress(dtBhav))

                strFullFileName = ""
                strFullFileName = m_strPathEQ & "\" & BHAVCOPY_NSE_FO_FOLDER & ".zip"
                DeleteFile(strFullFileName, False)
                wcNseFut.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)")
                wcNseFut.DownloadFileAsync(uriNseFut, strFullFileName)
            End If
        Else
            ShowUltraMessage(Nothing, "Please select the path and enter a valid date.", "Error", MessageBoxButtons.OK, _
                          MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, "", "", _
                          "Check conditions", "Stock MIS")
        End If

1 个答案:

答案 0 :(得分:0)

@kentucker建议将用户代理添加到webclient标头非常接近该解决方案。我只是用提琴手来获取正确的用户代理,添加了一些网络凭据,然后就可以正常工作了。