System.Net.WebClient().DownloadString lee el código de forma errónea

(1/1)

z3nth10n:
Hola buenas, pues sí, parece ser o que bien la web de la que me estoy descargando el contenido tiene alguna forma de protección de la que desconozco...

O bien, que VB.NET no descarga correctamente el contenido de dicha página...

Estaba haciendo un descargador de música, mrtzcmp3.net

Código
ReadOnly motor As String = "http://mrtzcmp3.net/"
   Dim sourceString As String
   Dim matches As MatchCollection
   ReadOnly regexSearch As String = "D\?.+? _"
   Dim firstMatch As String
 
Private Sub Buscar()
 
       sourceString = New System.Net.WebClient().DownloadString(motor & TextBox1.Text & "_1s.html")
       matches = Regex.Matches(sourceString, regexSearch)
       firstMatch = matches.Item(0).Value
       firstMatch = Regex.Replace(firstMatch, "._$", "")
 
       TextBox1.Text = motor & firstMatch
 
       'sourceString = New System.Net.WebClient().DownloadString(motor & firstMatch)
 
       'File.AppendAllText(".\prueba.txt", sourceString)
 
   End Sub

El caso es que he hecho un TextBox que cuando introduzcas x palabra te la busque con esta web:

Código
sourceString = New System.Net.WebClient().DownloadString(motor & TextBox1.Text & "_1s.html")

Le he hecho una especie de debug, File.AppendAllText(".\prueba.txt", sourceString)

he visto que era correcto he continuado y he hecho este Regex:

D\?.+? _

del cual, ahora me arrepiento, porque se ve que al descargar el source code, por ejemplo:

Código
<a rel="nofollow" href="D?QAk1Bl0_Pitbull_Prueba _WRFDqkYz" target="_blank"><img alt="Download" title="Download" border="0" width="18" height="18" src="images/Download.png"></a>

La web bloquea la parte del link, y quita el "WRFDqkYz", para que no se pueda descargar...

Así que, supongo que la web está protegiendo ese contenido, para que nadie se lo pueda descargar, ahora la pregunta, porque cuando me meto desde Google Chrome si veo ese "enlace oculto" y cuando me lo descargo por VB.NET no me sale?

Que debería hacer para que saliese?

PD: A lo mejor, la chapucería me ayudaría en este caso, por ejemplo un webBrowser oculto, del que hiciese un webbrowser1.document.body.innerhtml o algo por el estilo...

Un saludo.

PD: Según se puede ver aquí también sucede: http://www.iwebtool.com/code_viewer?domain=mrtzcmp3.net%2Fprueba_1s.html Por lo que se ve que a web tiene algún tipo de protección...

PD: Es más, cuando hago click con un WebBrowser me salta todo el rato el mismo error:

Shout:
Cambia el user agent

z3nth10n:
Hola @shout, pues casi, pero no, resulta ser que son las cookies, cosa en la que no había caído y gracias a Kub0x pues me he dado cuenta..

Me dio algunos pasos a seguir, pero tiré por libre he hice este code, que por ahora parece que funciona a la perfección:

Código
   ReadOnly motor As String = "http://mrtzcmp3.net/"
   Dim sourceString As String
   Dim matches As MatchCollection
   ReadOnly regexSearch As String = "D\?.+? _"
   Dim firstMatch As String
   Dim pageHtml As String
   Dim url As String
   Dim Cookies As New CookieContainer
   Dim cookieString As String
   Dim request As System.Net.HttpWebRequest
   Dim response As System.Net.HttpWebResponse
   Dim cookieName As String
   Dim sr As System.IO.StreamReader
 
 
   Private Sub Buscar()
 
       'Cookie Name
       cookieName = "haras"
 
       'Get first source
       request = CType(HttpWebRequest.Create(motor & TextBox1.Text & "_1s.html"), HttpWebRequest)
       request.CookieContainer = Cookies 'Request Cookies
       response = CType(request.GetResponse(), HttpWebResponse)
 
       'DO multiple things with the cookies
       For Each cookieValue As Cookie In response.Cookies
           If cookieValue.ToString.Substring(0, cookieValue.ToString.IndexOf("=")) = cookieName Then
               cookieString = cookieValue.ToString.Replace(cookieName & "=", "") 'Get haras cookie value
           End If
           Cookies.Add(cookieValue) 'Add cookies to the container for use it again
       Next
 
       'Read Source String
       sr = New System.IO.StreamReader(response.GetResponseStream())
       sourceString = sr.ReadToEnd()
 
       'Check the result
       matches = Regex.Matches(sourceString, regexSearch)
 
       'If results = 0 stop sub
       If matches.Count = 0 Then
           MsgBox("Se encontraron 0 resultados de esta canción.", MsgBoxStyle.Information, "Información")
           Exit Sub
       End If
 
       'Get first song link
       firstMatch = matches.Item(0).Value
 
       'Enter with Fake Cookie
       request = CType(HttpWebRequest.Create(motor & firstMatch & cookieString), HttpWebRequest)
       request.CookieContainer = Cookies
       response = CType(request.GetResponse(), HttpWebResponse)
 
       'Read source String
       sr = New System.IO.StreamReader(response.GetResponseStream())
       sourceString = sr.ReadToEnd()
 
       'Append text to a new text file (this will be the future step, for get song download link)
       If Not File.Exists(".\prueba.html") Then
           File.AppendAllText(".\prueba.html", sourceString)
       Else
           File.Delete(".\prueba.html")
           File.AppendAllText(".\prueba.html", sourceString)
       End If
 
       'For advise that the function has finished
       MsgBox("Done!")
 
   End Sub

Parte de esta información la he sacado de un snippet que me ha servido bastante para hacer lo que yo quería:

Código
   Private Sub Login_in_FB()
       Dim cookieJar As New Net.CookieContainer()
       Dim request As Net.HttpWebRequest
       Dim response As Net.HttpWebResponse
       Dim strURL As String = ""
 
       Try
           'Get Cookies
           strURL = "https://login.facebook.com/login.php"
           request = CType(HttpWebRequest.Create(strURL), HttpWebRequest)
           request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3"
           request.Method = "GET"
           request.CookieContainer = cookieJar
           response = CType(request.GetResponse(), HttpWebResponse)
 
           For Each tempCookie As Net.Cookie In response.Cookies
               cookieJar.Add(tempCookie)
           Next
 
           'Send the post data now
           request = CType(HttpWebRequest.Create(strURL), HttpWebRequest)
           request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3"
           request.Method = "POST"
           request.AllowAutoRedirect = True
           request.CookieContainer = cookieJar
 
           Dim writer As StreamWriter = New StreamWriter(request.GetRequestStream())
           writer.Write("email=username&pass=password")
           writer.Close()
           response = CType(request.GetResponse(), HttpWebResponse)
 
           'Get the data from the page
           Dim stream As StreamReader = New StreamReader(response.GetResponseStream())
           Dim data As String = stream.ReadToEnd()
           response.Close()
 
           If data.Contains("<title>Facebook") = True Then
               'LOGGED IN SUCCESSFULLY
               MessageBox.Show("logged in sucessfully")
           Else
               MessageBox.Show("Not logged in")
           End If
 
       Catch ex As Exception
           MessageBox.Show(ex.ToString)
       End Try
   End Sub

Un saludo.

Navegación

[0] Índice de Mensajes