Ricerca 
it-ITen-US
Register
Login
In-Vesti Dotnetwork
IN-VESTI DNW!!!
Sono finalmente arrivate le nuovissime T-Shirt di DotNetWork!!! Con soli 15,00 € ci sosterrai nelle spese di gestione della Community e ti invieremo a casa una splendida maglietta.
Se vuoi contribuire al mantenimento di DotNetWork.it Vai sulla pagina Iscrizioni
Effettua il pagamento usando IWBank
Click per andare alla pagina di Iscrizione
Oppure un Bonifico bancario (le coordinate sono sulla pagina Iscrizioni), inviaci una mail a support@dotnetwork.it indicandoci la tua taglia e l'indirizzo di spedizione.  Non appena verificata la ricezione del pagamento provvederemo a spedirti la tua T-Shirt.  Le magliette sono disponibili nelle taglie S-M-L-XL-XXL (in caso di esaurimento di una delle taglie, indica quella di "Backup"). Grazie per IN-VESTIRTI con NOI!!!
.:DotNetWork Founders:.
    Print     


DotNetWork Forums
HTTPRequest Header
Last Post 26 Mar 2009 16:20 by Rudy (DNW TEAM). 4 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages

Posts:435

--
23 Mar 2009 17:09  
Ciao a tutti.
Volevo fare una piccola app che accede ad un sito.
Al di la della login, mi fermo gia' all'apertura della pagina

il codice che uso e' questo:

Private Sub leggi(ByVal url As String)
        If Not url = "" Then
            Try
                Dim client As New WebClient
                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(url)
                Dim reader As New StreamReader(data)
                Console.Writeline(reader.Readline)
            Catch ex As Exception

            End Try
        End If
    End Sub

ma ottengo sempre che non ho un browser supportato, qualunque cosa scriva nell'header.
I browser supportati sono IE, Firefox, opera ecc ...

Qualche suggerimento ?

Grazie.
Max

Posts:811

--
24 Mar 2009 09:11  

Non so se ti rispondo fischi per fiaschi, però l'altro giorno dovevo fare un parser che mi generava un messaggio di posta da un file HTML creando le immagini embedded come richiesto dalle API del mio server. Ho scoperto così il controllo WebBrowser che inserito su una form permette di aprire una qualsiasi pagina HTML usando come URL l'HTTP oppure il percorso su disco e ti restituisce tutto il contenuto della pagina stessa come una serie di strutture che per quanto un pochino bruttarelle perché è un wrapper COM ti permettono di fare quel che vuoi sul codice HTML della pagina letta (leggerlo e scriverci dentro anche). Non so se può risolvere il tuo problema, però credo sia più semplice usare un controllo di quel tipo, magari nascosto e poi leggere le informazioni che ti servono e fare ciò che ti serve sulla pagina stessa.

Saluti

Sabrina

 

Inserito da Max su 23 mar 2009 18.09
Ciao a tutti.
Volevo fare una piccola app che accede ad un sito.
Al di la della login, mi fermo gia' all'apertura della pagina

il codice che uso e' questo:

Private Sub leggi(ByVal url As String)
        If Not url = "" Then
            Try
                Dim client As New WebClient
                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(url)
                Dim reader As New StreamReader(data)
                Console.Writeline(reader.Readline)
            Catch ex As Exception

            End Try
        End If
    End Sub

ma ottengo sempre che non ho un browser supportato, qualunque cosa scriva nell'header.
I browser supportati sono IE, Firefox, opera ecc ...

Qualche suggerimento ?

Grazie.
Max


Sabrina

Posts:657

--
25 Mar 2009 08:02  

Prova questa:

Public Function OpenURL(ByVal sInURL As String) As String

       Dim iP As Integer
       Dim sURL As String
       Dim sURLHost As String
       Dim sURLPath As String

       Dim abyteReceive(1024) As Byte

       'The path parsing should be more robust ...
       iP = InStr(UCase$(sInURL), "HTTP://")
       If iP > 0 Then
           sURL = Mid$(sInURL, iP + 7)
       Else
           sURL = sInURL
       End If

       iP = InStr(sURL, "/")
       If iP > 0 Then
           sURLHost = Mid(sURL, 1, iP - 1)
           sURLPath = Mid(sURL, iP)
       Else
           sURLHost = sURL
           sURLPath = "/"
       End If

       'should be supporting HTTP 1.1
       Dim s As String = ""
       Dim sGet As String = "GET " & sURLPath & " HTTP/1.0" & vbCrLf
& " Host: " & sURLHost & vbCrLf & "Connection: Close" & vbCrLf &
vbCrLf
       Dim asciiGet As Encoding = Encoding.ASCII
       Dim abyteGet() As Byte = asciiGet.GetBytes(sGet)

       Try
           Dim hostentry As IPHostEntry = Dns.GetHostEntry(sURLHost)
           Dim hostadd As IPAddress = hostentry.AddressList(0)


           Dim EPhost As IPEndPoint = New IPEndPoint(hostadd, 80)
           Dim sockHTTP As Socket = New
Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp)

           sockHTTP.Connect(EPhost)

           If sockHTTP.Connected = False Then
               OpenURL = "Unable to connect to host: " & sURLHost
               Exit Function
           End If

           sockHTTP.Send(abyteGet, abyteGet.Length, 0)

           Dim iBytes As Integer = sockHTTP.Receive(abyteReceive,
abyteReceive.Length, 0)
           s = "HTML from " & sURL & "(" & hostadd.ToString & "):" & vbCrLf
           s &= asciiGet.GetString(abyteReceive, 0, iBytes)

           Do While iBytes > 0
               iBytes = sockHTTP.Receive(abyteReceive, abyteReceive.Length, 0)
               s &= asciiGet.GetString(abyteReceive, 0, iBytes)
           Loop

           sockHTTP.Close()
           sockHTTP = Nothing

       Catch ex As Exception

           MessageBox.Show(ex.Message)

       End Try

       OpenURL = s

   End Function

Rudy Azzan

Posts:435

--
26 Mar 2009 10:03  
Ciao Ragazzi,
Grazie epr le risposte.
Ho usato in effetti WebBrowser.

Molto carino, infatti mettendolo nella form vedi anche cosa fa..

Ho fatto un faorm che compila i campi e preme il button "Accedi".

Scrivo a futura memoria che L'oggetto Document che restituisce WebBrowser diventa nothing (null) dopo l'evento DocumentCompleted.

Tutte le operazioni di parse vanno fatte li.

Almeno questo e' quello che ho capito.
Ora vado a giocare con le RegEx per fare un po di ScreenScraping...

Ciao !

p.s. Oggi funziona, che piacere ritrovarvi !!

Posts:657

--
26 Mar 2009 16:20  
Prova questa:

Public Function OpenURL(ByVal sInURL As String) As String

Dim iP As Integer
Dim sURL As String
Dim sURLHost As String
Dim sURLPath As String

Dim abyteReceive(1024) As Byte

'The path parsing should be more robust ...
iP = InStr(UCase$(sInURL), "HTTP://")
If iP > 0 Then
sURL = Mid$(sInURL, iP + 7)
Else
sURL = sInURL
End If

iP = InStr(sURL, "/")
If iP > 0 Then
sURLHost = Mid(sURL, 1, iP - 1)
sURLPath = Mid(sURL, iP)
Else
sURLHost = sURL
sURLPath = "/"
End If

'should be supporting HTTP 1.1
Dim s As String = ""
Dim sGet As String = "GET " & sURLPath & " HTTP/1.0" & vbCrLf
& " Host: " & sURLHost & vbCrLf & "Connection: Close" & vbCrLf &
vbCrLf
Dim asciiGet As Encoding = Encoding.ASCII
Dim abyteGet() As Byte = asciiGet.GetBytes(sGet)

Try
Dim hostentry As IPHostEntry = Dns.GetHostEntry(sURLHost)
Dim hostadd As IPAddress = hostentry.AddressList(0)


Dim EPhost As IPEndPoint = New IPEndPoint(hostadd, 80)
Dim sockHTTP As Socket = New
Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp)

sockHTTP.Connect(EPhost)

If sockHTTP.Connected = False Then
OpenURL = "Unable to connect to host: " & sURLHost
Exit Function
End If

sockHTTP.Send(abyteGet, abyteGet.Length, 0)

Dim iBytes As Integer = sockHTTP.Receive(abyteReceive,
abyteReceive.Length, 0)
s = "HTML from " & sURL & "(" & hostadd.ToString & "):" & vbCrLf
s &= asciiGet.GetString(abyteReceive, 0, iBytes)

Do While iBytes > 0
iBytes = sockHTTP.Receive(abyteReceive, abyteReceive.Length, 0)
s &= asciiGet.GetString(abyteReceive, 0, iBytes)
Loop

sockHTTP.Close()
sockHTTP = Nothing

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

OpenURL = s

End Function


Il 24 marzo 2009 10.11, <Web@dotnetwork.it> ha scritto:
> RE: HTTPRequest Header
>
> Forum: Web Applications & Web Services - Inviato da: sabrina in data 24 mar
> 2009 09.11
>
> Non so se ti rispondo fischi per fiaschi, però l'altro giorno dovevo fare un
> parser che mi generava un messaggio di posta da un file HTML creando le
> immagini embedded come richiesto dalle API del mio server. Ho scoperto così
> il controllo WebBrowser che inserito su una form permette di aprire una
> qualsiasi pagina HTML usando come URL l'HTTP oppure il percorso su disco e
> ti restituisce tutto il contenuto della pagina stessa come una serie di
> strutture che per quanto un pochino bruttarelle perché è un wrapper COM ti
> permettono di fare quel che vuoi sul codice HTML della pagina letta
> (leggerlo e scriverci dentro anche). Non so se può risolvere il tuo
> problema, però credo sia più semplice usare un controllo di quel tipo,
> magari nascosto e poi leggere le informazioni che ti servono e fare ciò che
> ti serve sulla pagina stessa.
>
> Saluti
>
> Sabrina
>
>
>
> Inserito da Max su 23 mar 2009 18.09
> Ciao a tutti.
> Volevo fare una piccola app che accede ad un sito.
> Al di la della login, mi fermo gia' all'apertura della pagina
>
> il codice che uso e' questo:
>
> Private Sub leggi(ByVal url As String)
>         If Not url = "" Then
>             Try
>                 Dim client As New WebClient
>                 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(url)
>                 Dim reader As New StreamReader(data)
>                 Console.Writeline(reader.Readline)
>             Catch ex As Exception
>
>             End Try
>         End If
>     End Sub
>
> ma ottengo sempre che non ho un browser supportato, qualunque cosa scriva
> nell'header.
> I browser supportati sono IE, Firefox, opera ecc ...
>
> Qualche suggerimento ?
>
> Grazie.
> Max
>
>
>
Rudy Azzan
You are not authorized to post a reply.

Active Forums 4.1
       
Articoli
L'Albero degli elementi
Come iniziare a capire com'è fatto WPF (parte 3)
2007/08/13 | Author: Patrizia Cosolo
Mixed Fry - General purpouse classes (part 1)
Helper: A class to manage strings
2007/10/20 | Author: Sabrina Cosolo
Ereditarietà applicata ai controlli
Creazione di una combobox che mostra immagini al posto del testo
2008/07/06 | Author: Andrea Zingoni
Our Dll's in the Add Reference .NET Tab
How to be able to show our DLLs in the Add reference Windows Under the .NET Tab.
2008/07/27 | Author: Sabrina Cosolo
.NET e l'Interoperabilità COM
Come utilizzare Dll non referenziabili per convertire un documento Word in PDF
2008/07/05 | Author: Massimo Lofrano
Codedom Getting Started part 1
How to generate automatically C# or VB code using CodeDom and the .NET framework
2009/11/07 | Author: Sabrina Cosolo
Working with Arrays
The backpack problem
2007/07/29 | Author: Alberto De Luca
SQL Server 2000/2005 Manutenzione Database
Uso di DBCC ShowContig e di sys.dm_db_index_physical_stats
2007/08/05 | Author: Sabrina Cosolo
    Print     
Home|Forums|Blogs|Mappa del sito
© 2007-2010 by DotNetWork  .:.  Terms Of Use  .:.  Privacy Statement  .:.  Login  .:.