Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    4,826

    [c#]lettura pagina html da client

    Ciao.
    Devo leggere una pagina html da internet e controllarne il titolo.
    Per quanto riguarda il parsing utilizzo una libreria , questa:
    http://www.majestic12.co.uk/projects/html_parser.php

    ma per fare il download della pagina ho trovato questo codice:
    codice:
    using System;
    using System.Text;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    
    public class GetSocket
    {
        private static Socket ConnectSocket(string server, int port)
        {
            Socket s = null;
            IPHostEntry hostEntry = null;
    
            // Get host related information.
            hostEntry = Dns.GetHostEntry(server);
    
            // Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
            // an exception that occurs when the host IP Address is not compatible with the address family
            // (typical in the IPv6 case).
            foreach(IPAddress address in hostEntry.AddressList)
            {
                IPEndPoint ipe = new IPEndPoint(address, port);
                Socket tempSocket = 
                    new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
    
                tempSocket.Connect(ipe);
    
                if(tempSocket.Connected)
                {
                    s = tempSocket;
                    break;
                }
                else
                {
                    continue;
                }
            }
            return s;
        }
    
        // This method requests the home page content for the specified server.
        private static string SocketSendReceive(string server, int port) 
        {
            string request = "GET / HTTP/1.1\r\nHost: " + server + 
                "\r\nConnection: Close\r\n\r\n";
            Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
            Byte[] bytesReceived = new Byte[256];
    
            // Create a socket connection with the specified server and port.
            Socket s = ConnectSocket(server, port);
    
            if (s == null)
                return ("Connection failed");
    
            // Send request to the server.
            s.Send(bytesSent, bytesSent.Length, 0);  
    
            // Receive the server home page content.
            int bytes = 0;
            string page = "Default HTML page on " + server + ":\r\n";
    
            // The following will block until te page is transmitted.
            do {
                bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
                page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
            }
            while (bytes > 0);
    
            return page;
        }
    
        public static void Main(string[] args) 
        {
            string host;
            int port = 80;
    
            if (args.Length == 0)
                // If no server name is passed as argument to this program, 
                // use the current host name as the default.
                host = Dns.GetHostName();
            else
                host = args[0];
    
            string result = SocketSendReceive(host, port); 
            Console.WriteLine(result);
        }
    }
    il problema è che devo "fare girare" questo programma in una ditta(cliente) dietro un proxy e non so se la porta 80 è "aperta".
    pero' riesco a navigare tranquillamente dalla postazione in cui devo installare il programma, se navigo dovrebbe essere possibile che tutto funzioni?
    grazie.

  2. #2
    Utente di HTML.it L'avatar di albgen
    Registrato dal
    Jun 2005
    Messaggi
    3,249
    esistono già delle classi che ti permettono di lavorare con l'http.
    ecco un esempio semplice, ma completo per quello che devi fare...

    String URL = "http://www.google.com";
    Uri uri = new Uri(URL);
    HttpWebRequest richiestaHTTP = (HttpWebRequest)WebRequest.Create(uri);

    WebProxy wp = new WebProxy();
    //configurazione wp

    richiestaHTTP.Proxy = wp;

    HttpWebResponse rispostaHTTP = (HttpWebResponse)richiestaHTTP.GetResponse();
    StreamReader stream = new StreamReader(rispostaHTTP.GetResponseStream(), Encoding.UTF8);
    String strRispostaHTML = stream.ReadToEnd();
    Console.Write(strRispostaHTML );

    quindi vedi la classe WebProxy per quello che serve a te...
    ciao
    I got the remedy

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.