Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - Coda di download
Forum - C# / VB.NET - Coda di download - Pagina 3

Pagine: [ 1 2 3 4 ] Precedente | Prossimo
Avatar
manvb.net (Member)
Guru


Messaggi: 663
Iscritto: 28/01/2008

Segnala al moderatore
Postato alle 18:23
Martedì, 25/08/2009
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 il 25/08/2009 alle 18:24
PM Quote
Avatar
LittleHacker (Member)
Guru


Messaggi: 1033
Iscritto: 28/04/2009

Segnala al moderatore
Postato alle 18:27
Martedì, 25/08/2009
Mi andava quando gli davo un'indirizzo e basta invece adesso che ho messo pure la coda non mi va più...Boh...:k:

PM Quote
Avatar
manvb.net (Member)
Guru


Messaggi: 663
Iscritto: 28/01/2008

Segnala al moderatore
Postato alle 18:29
Martedì, 25/08/2009
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ì:
Codice sorgente - presumibilmente VB.NET

  1. Imports System.Net
  2. Imports System.IO
  3. Public Class frmDownload
  4.     Public WithEvents TCP As New WebClient
  5.     Dim total As Integer = 0
  6.     Dim downloades As Integer = 0
  7.     Dim TDownload As New Threading.Thread(AddressOf TCoda)
  8.  
  9.     Private Shared Function SetAllowUnsafeHeaderParsing20() As Boolean
  10.         Dim sectionAssembly As Reflection.Assembly = Reflection.Assembly.GetAssembly(GetType(Net.Configuration.SettingsSection))
  11.  
  12.         If sectionAssembly IsNot Nothing Then
  13.             Dim settingsType As Type = sectionAssembly.[GetType]("System.Net.Configuration. SettingsSectionInternal")
  14.  
  15.             If settingsType IsNot Nothing Then
  16.                 Dim sectionInstance As Object = settingsType.InvokeMember("Section", Reflection.BindingFlags.GetProperty Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.[Static], Nothing, Nothing, New Object(-1) {})
  17.  
  18.                 If sectionInstance IsNot Nothing Then
  19.                     Dim useUnsafeHeaderParsing As Reflection.FieldInfo = settingsType.GetField("useUnsafeHeaderParsing", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)
  20.                     If useUnsafeHeaderParsing IsNot Nothing Then
  21.                         useUnsafeHeaderParsing.SetValue(sectionInstance, True)
  22.                         Return True
  23.                     End If
  24.                 End If
  25.             End If
  26.         End If
  27.  
  28.         Return False
  29.     End Function
  30.  
  31.  
  32.     Private Function Convert(ByVal url As String) As String
  33.         url = url.Replace("www.youtube.com", "youtube.com")
  34.         If url.IndexOf("http://youtube.com/v/") >= 0 Then
  35.             url.Replace("http://youtube.com/v/", "http://youtube.com/watch?v=")
  36.         End If
  37.         If url.IndexOf("http://youtube.com/watch?v=") < 0 Then
  38.             url = ""
  39.         End If
  40.         Return (url)
  41.     End Function
  42.  
  43.     Private Function GetContent(ByVal url As String) As String
  44.         Dim buffer As String
  45.         'Try
  46.         Dim outputBuffer As String = "where=46038"
  47.         Dim req As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
  48.         req.Method = "POST"
  49.         req.ContentLength = outputBuffer.Length
  50.         req.ContentType = "application/x-www-form-urlencoded"
  51.         Dim swOut As New StreamWriter(req.GetRequestStream())
  52.         swOut.Write(outputBuffer)
  53.         'swOut.Close()
  54.         Dim resp As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
  55.         Dim sr As New StreamReader(resp.GetResponseStream())
  56.         buffer = sr.ReadToEnd()
  57.         sr.Close()
  58.         'Catch exp As Exception
  59.         '    buffer = "Errore: " & exp.Message.ToString()
  60.         'End Try
  61.         swOut.Close()
  62.         Return (buffer)
  63.     End Function
  64.  
  65.     Private Sub Download_Video(ByVal url As String)
  66.         Dim buffer As String = GetContent(url)
  67.         If buffer.IndexOf("Errore:") < 0 Then
  68.             Dim start As Integer = 0, [End] As Integer = 0
  69.             Dim startTag As String = "/watch_fullscreen?"
  70.             Dim endTag As String = ";"
  71.             start = buffer.IndexOf(startTag, StringComparison.CurrentCultureIgnoreCase)
  72.             [End] = buffer.IndexOf(endTag, start, StringComparison.CurrentCultureIgnoreCase)
  73.             Dim str As String = buffer.Substring(start + startTag.Length, [End] - (start + startTag.Length))
  74.             Dim vid As String = str.Substring(str.IndexOf("video_id"), str.IndexOf("&", str.IndexOf("video_id")) - str.IndexOf("video_id"))
  75.             Dim l As String = str.Substring(str.IndexOf("&l"), str.IndexOf("&", str.IndexOf("&l") + 1) - str.IndexOf("&l"))
  76.             Dim t As String = str.Substring(str.IndexOf("&t"), str.IndexOf("&", str.IndexOf("&t") + 1) - str.IndexOf("&t"))
  77.             Dim title As String = str.Substring(str.IndexOf("&title=") + 7)
  78.             title = title.Substring(0, title.Length - 1)
  79.             lblvideo.Text = title
  80.             TCP.DownloadFile(New Uri("http://youtube.com/get_video?" & vid & l & t), ("C:\video.flv"))
  81.         End If
  82.     End Sub
  83.  
  84.     Private Sub DownloadComplete(ByVal sender As Object, ByVal e As System.Net.DownloadDataCompletedEventArgs) Handles TCP.DownloadDataCompleted
  85.         If e.Cancelled = True Then
  86.             MsgBox("I/Il download sono/è stati/o annullati/o", MsgBoxStyle.Exclamation)
  87.         Else
  88.             MsgBox("I/Il download sono/è stati/o completati/o", MsgBoxStyle.Information)
  89.         End If
  90.     End Sub
  91.  
  92.     Private Sub PrgChngd(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles TCP.DownloadProgressChanged
  93.         pbdownloadvideo.Value = e.ProgressPercentage()
  94.         pbtotaldownload.Value = (CInt((total - downloades) / total) * 100)
  95.     End Sub
  96.  
  97.     Private Sub frmDownload_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  98.         SetAllowUnsafeHeaderParsing20()
  99.         TDownload.Start()
  100.         Control.CheckForIllegalCrossThreadCalls = False
  101.         total = lstVideo.Items.Count
  102.     End Sub
  103.  
  104.     Private Sub TCoda() 'Thread di estrazione dalla coda
  105.         Dim url As String = ""
  106.         While True 'Ciclo infinito
  107.             If lstVideo.Items.Count > 0 Then
  108.                 url = lstVideo.Items(0).ToString() 'Ottengo l'url
  109.                 lstVideo.Items.RemoveAt(0) 'Rimuovo l'elemento dalla ListBox dei download
  110.                 downloades = total - lstVideo.Items.Count
  111.                 Download_Video(url) 'Mi raccomando, il download deve essere sincrono, non asincrono
  112.             End If
  113.             Threading.Thread.Sleep(500) 'Diamo un attimo di respiro al pc
  114.         End While
  115.     End Sub
  116.  
  117.     Private Sub cmdexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdexit.Click
  118.         TCP.CancelAsync()
  119.         Me.Close()
  120.     End Sub
  121. End Class


Ultima modifica effettuata da manvb.net il 25/08/2009 alle 18:38
PM Quote
Avatar
LittleHacker (Member)
Guru


Messaggi: 1033
Iscritto: 28/04/2009

Segnala al moderatore
Postato alle 19:39
Martedì, 25/08/2009
Nono ho sempre lo stesso errore...:k:8-|

PM Quote
Avatar
GoLDBeRG (Ex-Member)
Expert


Messaggi: 331
Iscritto: 19/12/2005

Segnala al moderatore
Postato alle 16:10
Giovedì, 27/08/2009
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

PM Quote
Avatar
LittleHacker (Member)
Guru


Messaggi: 1033
Iscritto: 28/04/2009

Segnala al moderatore
Postato alle 23:43
Sabato, 29/08/2009
Ok fatto...Ora qualcuno ha dei consigli per questo errore?

Codice sorgente - presumibilmente C# / VB.NET

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



PM Quote
Avatar
GoLDBeRG (Ex-Member)
Expert


Messaggi: 331
Iscritto: 19/12/2005

Segnala al moderatore
Postato alle 13:24
Domenica, 30/08/2009
prova solo stream senza writer

PM Quote
Avatar
LittleHacker (Member)
Guru


Messaggi: 1033
Iscritto: 28/04/2009

Segnala al moderatore
Postato alle 13:30
Domenica, 30/08/2009
Testo quotato

Postato originariamente da GoLDBeRG:

prova solo stream senza writer



Mi da 3 errori di sintassi:
1°: I limiti di matrice non possono trovarsi negli identificatori di tipo. questo nella riga req.GetRequestStream() dopo la parentesi dello streamWriter che mi hai detto di togliere
2°: nome swOut non dichiarato. questo nella riga:  swOut.Write(outputBuffer)
3à: nome swOut non dichiarato. questo nella riga: swOut.Close()
:d:-?

PM Quote
Pagine: [ 1 2 3 4 ] Precedente | Prossimo