Oppure

Loading
25/08/09 14:31
LittleHacker
Postato originariamente da manvb.net:

Non ho letto tutto ma credo che il problema sia quì:

TCP.DownloadFileAsync(New Uri("youtube.com/…; & vid & l & t), ("C:\video.flv";))

Il file lo devi scaricare in maniera sincrona, non asincrona, e cioè:

TCP.DownloadFile(New Uri("youtube.com/…; & vid & l & t), ("C:\video.flv";))


Non è quello...:d:d
aaa
25/08/09 16:23
manvb.net
Di per se il codice funziona, ma per qualche oscuro motive non riesce a stabilire la connessione, infatti restituisce l'eccezione "Connessione sottostante chiusa: Chiusura imprevista della connessione.." nella funzione GetContent a questa istruzione:

Dim swOut As New StreamWriter(req.GetRequestStream())
Ultima modifica effettuata da manvb.net 25/08/09 16:24
aaa
25/08/09 16:27
LittleHacker
Mi andava quando gli davo un'indirizzo e basta invece adesso che ho messo pure la coda non mi va più...Boh...:k:
aaa
25/08/09 16:29
manvb.net
Sto leggendo che forse si tratta di una questione di permessi, non so, appena ho novità ti faccio sapere.

PS.
Noterai che ho tolto la coda, il codice utilizza semplicemente la lista.

Edit:

Prova così:
Imports System.Net
Imports System.IO
Public Class frmDownload
    Public WithEvents TCP As New WebClient
    Dim total As Integer = 0
    Dim downloades As Integer = 0
    Dim TDownload As New Threading.Thread(AddressOf TCoda)

    Private Shared Function SetAllowUnsafeHeaderParsing20() As Boolean
        Dim sectionAssembly As Reflection.Assembly = Reflection.Assembly.GetAssembly(GetType(Net.Configuration.SettingsSection))

        If sectionAssembly IsNot Nothing Then
            Dim settingsType As Type = sectionAssembly.[GetType]("System.Net.Configuration. SettingsSectionInternal")

            If settingsType IsNot Nothing Then
                Dim sectionInstance As Object = settingsType.InvokeMember("Section", Reflection.BindingFlags.GetProperty Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.[Static], Nothing, Nothing, New Object(-1) {})

                If sectionInstance IsNot Nothing Then
                    Dim useUnsafeHeaderParsing As Reflection.FieldInfo = settingsType.GetField("useUnsafeHeaderParsing", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)
                    If useUnsafeHeaderParsing IsNot Nothing Then
                        useUnsafeHeaderParsing.SetValue(sectionInstance, True)
                        Return True
                    End If
                End If
            End If
        End If

        Return False
    End Function


    Private Function Convert(ByVal url As String) As String
        url = url.Replace("www.youtube.com", "youtube.com")
        If url.IndexOf("http://youtube.com/v/") >= 0 Then
            url.Replace("http://youtube.com/v/", "http://youtube.com/watch?v=")
        End If
        If url.IndexOf("http://youtube.com/watch?v=") < 0 Then
            url = ""
        End If
        Return (url)
    End Function

    Private Function GetContent(ByVal url As String) As String
        Dim buffer As String
        'Try
        Dim outputBuffer As String = "where=46038"
        Dim req As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
        req.Method = "POST"
        req.ContentLength = outputBuffer.Length
        req.ContentType = "application/x-www-form-urlencoded"
        Dim swOut As New StreamWriter(req.GetRequestStream())
        swOut.Write(outputBuffer)
        'swOut.Close()
        Dim resp As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
        Dim sr As New StreamReader(resp.GetResponseStream())
        buffer = sr.ReadToEnd()
        sr.Close()
        'Catch exp As Exception
        '    buffer = "Errore: " & exp.Message.ToString()
        'End Try
        swOut.Close()
        Return (buffer)
    End Function

    Private Sub Download_Video(ByVal url As String)
        Dim buffer As String = GetContent(url)
        If buffer.IndexOf("Errore:") < 0 Then
            Dim start As Integer = 0, [End] As Integer = 0
            Dim startTag As String = "/watch_fullscreen?"
            Dim endTag As String = ";"
            start = buffer.IndexOf(startTag, StringComparison.CurrentCultureIgnoreCase)
            [End] = buffer.IndexOf(endTag, start, StringComparison.CurrentCultureIgnoreCase)
            Dim str As String = buffer.Substring(start + startTag.Length, [End] - (start + startTag.Length))
            Dim vid As String = str.Substring(str.IndexOf("video_id"), str.IndexOf("&", str.IndexOf("video_id")) - str.IndexOf("video_id"))
            Dim l As String = str.Substring(str.IndexOf("&l"), str.IndexOf("&", str.IndexOf("&l") + 1) - str.IndexOf("&l"))
            Dim t As String = str.Substring(str.IndexOf("&t"), str.IndexOf("&", str.IndexOf("&t") + 1) - str.IndexOf("&t"))
            Dim title As String = str.Substring(str.IndexOf("&title=") + 7)
            title = title.Substring(0, title.Length - 1)
            lblvideo.Text = title
            TCP.DownloadFile(New Uri("http://youtube.com/get_video?" & vid & l & t), ("C:\video.flv"))
        End If
    End Sub

    Private Sub DownloadComplete(ByVal sender As Object, ByVal e As System.Net.DownloadDataCompletedEventArgs) Handles TCP.DownloadDataCompleted
        If e.Cancelled = True Then
            MsgBox("I/Il download sono/è stati/o annullati/o", MsgBoxStyle.Exclamation)
        Else
            MsgBox("I/Il download sono/è stati/o completati/o", MsgBoxStyle.Information)
        End If
    End Sub

    Private Sub PrgChngd(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles TCP.DownloadProgressChanged
        pbdownloadvideo.Value = e.ProgressPercentage()
        pbtotaldownload.Value = (CInt((total - downloades) / total) * 100)
    End Sub

    Private Sub frmDownload_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SetAllowUnsafeHeaderParsing20()
        TDownload.Start()
        Control.CheckForIllegalCrossThreadCalls = False
        total = lstVideo.Items.Count
    End Sub

    Private Sub TCoda() 'Thread di estrazione dalla coda
        Dim url As String = ""
        While True 'Ciclo infinito
            If lstVideo.Items.Count > 0 Then
                url = lstVideo.Items(0).ToString() 'Ottengo l'url
                lstVideo.Items.RemoveAt(0) 'Rimuovo l'elemento dalla ListBox dei download
                downloades = total - lstVideo.Items.Count
                Download_Video(url) 'Mi raccomando, il download deve essere sincrono, non asincrono
            End If
            Threading.Thread.Sleep(500) 'Diamo un attimo di respiro al pc
        End While
    End Sub

    Private Sub cmdexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdexit.Click
        TCP.CancelAsync()
        Me.Close()
    End Sub
End Class
Ultima modifica effettuata da manvb.net 25/08/09 16:38
aaa
25/08/09 17:39
LittleHacker
Nono ho sempre lo stesso errore...:k:8-|
aaa
27/08/09 14:10
GoLDBeRG
scusa ma io vedo moltissima confusione e accozzaglia di codice malmessa.... allora facciamo un po di ordine....

come prima cosa tu hai un ciclo for tipo questo

for each asd as string in listbox1.items
dim p as new download
p.indirizzo=asd
p.avvia() 'sincrono
dim thr as new thread(address of p.avvia)
thr.start()
next

molto bene poi nella classe download ti gestisci il download

come prima cosa devi richiedere l'html alla pagina con webclient ci metti un attimo

una volta che hai l'html ti trovi l'indirizzo del video flash splittando il codice o come ti trovi piu comodo e fai semplicemente

webclient.downloadfile(new uri(indirizzo))

non devi fare altro non usare codice inutile che ti crea solo casino
aaa
29/08/09 21:43
LittleHacker
Ok fatto...Ora qualcuno ha dei consigli per questo errore?

 Dim swOut As New StreamWriter(req.GetRequestStream()) 'Connessione sottostante chiusa: Chiusura imprevista della connessione..

aaa
30/08/09 11:24
GoLDBeRG
prova solo stream senza writer
aaa