lunedì 9 settembre 2013

Http Client in Java/Android

Un breve esempio di come realizzare una connessione automatica in protocollo HTTP mediante Java/Android


-------------------------------------------------------------------------------------
package httpclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;


public class Httpclient {

    public static void main(String[] args) throws IOException {
    try {
        URL url = new URL("http://www.google.it/");
               
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.arpat.toscana.it", 8080));
        HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
        //se non c'e' proxy commentare la riga superiore e decommentare quella inferiore
        //HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        
        //decommentare le due righe successive per mostrare il codice di errore HTTP
        //ritornato dal server
        //int code = connection.getResponseCode();
        //System.out.println(Integer.toString(code));
        
        BufferedReader read = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        line = read.readLine();
        String html = "";
        while(line!=null) {
            html += line;
            line = read.readLine();
        }
        System.out.println(html);
} catch(MalformedURLException ex) {
} catch(IOException ioex) {
}
    }
    
}

Nessun commento:

Posta un commento

Perche' investire su Unix

 Un libro trovato nel libero scambio alla Coop su cio' che poteva essere e non e' stato...interessante la storia su Unix del primo c...