在我的vb.net项目中需要帮助,阅读http响应标头时出现问题 [英] Need Help in my vb.net project ,problem in reading http response headers

查看:69
本文介绍了在我的vb.net项目中需要帮助,阅读http响应标头时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我即将在vb.net 2008中使用webclient创建一个下载程序。我想要获取的是http响应标头并在列表框中显示它们...但是我收到此消息{对象参考不是在Foreach行中设置为对象的实例。}这是我的代码

  Imports  System.Net 
公开 Form1
Dim WithEvents wc As WebClient
私有 Sub Button1_Click( ByVal sender < span class =code-keyword> As System。 Object ByVal e As System.EventArgs) Handles Butto n1.Click
Dim s As String

wc.DownloadFileAsync( New Uri(TextBox1.Text), c:\ downloads \ a.mp4

对于 每个名称作为 字符串 wc.ResponseHeaders.Keys
ListBox1.Items.Add(Name + + wc.ResponseHeaders(Name))
下一步


< span class =code-keyword> End Sub

Private Sub wc_DownloadProgressChanged( ByVal 发​​件人作为 对象 ByVal e As System.Net.DownloadProgressChangedEventArgs)句柄 wc.DownloadProgressChanged
ProgressBar1.Value = e.ProgressPercentage
结束 Sub
结束





希望你能帮助我..提前感谢

解决方案

DownloadFileAsync 方法返回之前下载已完成。在引发 DownloadFileCompleted 事件之前,您无法读取响应标头。

  Imports  System.Net 

公共 Form1
Dim WithEvents wc As WebClient

私有 Sub Button1_Click( ByVal sender As System。对象 ByVal e As EventArgs)句柄 Button1.Click
Button1.Enabled = False
wc.DownloadFileAsync( New Uri(T \\ textBox1.Text), c:\ downloads \ a.mp4
结束 Sub

私有 Sub wc_DownloadProgressChanged( ByVal sender As 对象 ByVal e As DownloadProgressChangedEventArgs) 句柄 wc.DownloadProgressChanged
ProgressBar1.Value = e.ProgressPercentage
结束 Sub

私有 Sub wc_DownloadFileCompleted( ByVal sender As 对象 ByVal e As AsyncCompletedEventArgs)句柄 wc.DownloadFileCompleted
对于 每个名称作为 字符串 wc.ResponseHeaders.Keys
ListBox1 .Items.Add(Name + + wc.ResponseHeaders(Name))
下一步
Button1.Enabled = True
结束 Sub
结束


Hi all , im about to create a downloader using a webclient in vb.net 2008 .. what im trying to get is the http response headers and show them in a listbox ...but i get this message {Object reference not set to an instance of an object.} in the Foreach line ... Here is my code

Imports System.Net
Public Class Form1
    Dim WithEvents wc As New WebClient
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim s As String

        wc.DownloadFileAsync(New Uri(TextBox1.Text), "c:\downloads\a.mp4")

        For Each Name As String In wc.ResponseHeaders.Keys
            ListBox1.Items.Add(Name + ":" + wc.ResponseHeaders(Name))
        Next


    End Sub

    Private Sub wc_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles wc.DownloadProgressChanged
        ProgressBar1.Value = e.ProgressPercentage
    End Sub
End Class



Hope you will help me .. thanks in advance

解决方案

The DownloadFileAsync method returns before the download has completed. You cannot read the response headers until the DownloadFileCompleted event is raised.

Imports System.Net

Public Class Form1
    Dim WithEvents wc As New WebClient
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles Button1.Click
        Button1.Enabled = False
        wc.DownloadFileAsync(New Uri(TextBox1.Text), "c:\downloads\a.mp4")
    End Sub
 
    Private Sub wc_DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Handles wc.DownloadProgressChanged
        ProgressBar1.Value = e.ProgressPercentage
    End Sub
    
    Private Sub wc_DownloadFileCompleted(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) Handles wc.DownloadFileCompleted
        For Each Name As String In wc.ResponseHeaders.Keys
            ListBox1.Items.Add(Name + ":" + wc.ResponseHeaders(Name))
        Next
        Button1.Enabled = True
    End Sub
End Class


这篇关于在我的vb.net项目中需要帮助,阅读http响应标头时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆