Problema descargar código de fuente Leido 13.922 veces

 
  Broco
Pregunta
Resuelta.
Necesito enviar el UserAgent y despues descargar el código de fuente



Dim htr As System.Net.HttpWebRequest = CType(System.Net.WebRequest.Create("http://www.google.es/"), System.Net.HttpWebRequest)
htr.UserAgent = "ExplorerExpres 3.0"
Dim htrp As System.Net.HttpWebResponse = CType(htr.GetResponse, System.Net.HttpWebResponse)


Tengo eso, he mirado el msdn y tal pero no veo nada, la unica forma que se de descargarloe es desde el WebClient pero no se puede enviar el UserAgent T_T

 

 





  Jesús
Respuesta correcta
Ha recibido:
1 premios
El MSDN es tu amigo, si quieres agregar user-agent a un webclient:
http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.80).aspx

Ejemplo de Microsoft:



Imports System
Imports System.Net
Imports System.IO

Public Class Test

Public Shared Sub Main(args() As String)
If args Is Nothing OrElse args.Length = 0 Then
Throw New ApplicationException("Specify the URI of the resource to retrieve.")
End If
Dim client As New WebClient()

' Add a user agent header in case the
' requested URI contains a query.
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)")

Dim data As Stream = client.OpenRead(args(0))
Dim reader As New StreamReader(data)
Dim s As String = reader.ReadToEnd()
Console.WriteLine(s)
data.Close()
reader.Close()
End Sub 'Main
End Class 'Test



y si quieres usar el WebRequest, sigo opinando que el MSDN es tu amigo:
http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspx

Sólo hay que crear un StreamReader para leer el html resultante.

Ejemplo de Microsoft:



Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Namespace Examples.System.Net
Public Class WebRequestGetExample

Public Shared Sub Main()
' Create a request for the URL.
Dim request As WebRequest = WebRequest.Create("http://www.contoso.com/default.html")
' If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials
' Get the response.
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
' Display the status.
Console.WriteLine(response.StatusDescription)
' Get the stream containing content returned by the server.
Dim dataStream As Stream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
' Display the content.
Console.WriteLine(responseFromServer)
' Cleanup the streams and the response.
reader.Close()
dataStream.Close()
response.Close()
End Sub 'Main
End Class 'WebRequestGetExample
End Namespace




 

 


  Jesús
Respuesta correcta
Ha recibido:
1 premios
El MSDN es tu amigo, si quieres agregar user-agent a un webclient:
http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.80).aspx

Ejemplo de Microsoft:



Imports System
Imports System.Net
Imports System.IO

Public Class Test

Public Shared Sub Main(args() As String)
If args Is Nothing OrElse args.Length = 0 Then
Throw New ApplicationException("Specify the URI of the resource to retrieve.")
End If
Dim client As New WebClient()

' Add a user agent header in case the
' requested URI contains a query.
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)")

Dim data As Stream = client.OpenRead(args(0))
Dim reader As New StreamReader(data)
Dim s As String = reader.ReadToEnd()
Console.WriteLine(s)
data.Close()
reader.Close()
End Sub 'Main
End Class 'Test



y si quieres usar el WebRequest, sigo opinando que el MSDN es tu amigo:
http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspx

Sólo hay que crear un StreamReader para leer el html resultante.

Ejemplo de Microsoft:



Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Namespace Examples.System.Net
Public Class WebRequestGetExample

Public Shared Sub Main()
' Create a request for the URL.
Dim request As WebRequest = WebRequest.Create("http://www.contoso.com/default.html")
' If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials
' Get the response.
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
' Display the status.
Console.WriteLine(response.StatusDescription)
' Get the stream containing content returned by the server.
Dim dataStream As Stream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
' Display the content.
Console.WriteLine(responseFromServer)
' Cleanup the streams and the response.
reader.Close()
dataStream.Close()
response.Close()
End Sub 'Main
End Class 'WebRequestGetExample
End Namespace




 

 


  Broco
Muchas gracias Jr funciona perfecto :)
 

 


  Alex
Muchas gracias JR me fue de gran utilidad
 

 








    ¿Cuantas columnas?     | 2 columnas | 3 columnas | 4 columnas | 5 columnas | 6 columnas |  
    Escribe la dirección web de la imagen:
  Publica tu respuesta  |  Haz Click en el siguiente cuadro para escribir