aula10Thttp

fetchURL.java

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

 class FetchURL
{
    /*
    public static void main(String args[]) throws Exception
    {
        int argc = args.length;

        if (argc != 1){
            System.out.println ("Syntax: java FetchURL url");
            return;
        }
        try {
            URL myURL = new URL( args[0] );
            InputStream in = myURL.openStream();
            BufferedInputStream bufIn = new BufferedInputStream(in);
            while((data = bufIn.read())!= -1){ //!EOF
                System.out.print ((char)data);
            }
            System.out.println ("\nHit <Enter> to continue");
            System.in.read();
        }catch (MalformedURLException e){
            System.err.println ("Unable to parse URL!");
            return;
        }catch (IOException ioe){
            System.err.println ("I/O Error : " + ioe);
            return;
        }
    }

     */
}

SingleQuoteConsumer.java

import java.io.*;
import java.net.*;
import com.google.gson.*;
import javax.json.*;
public class SingleQuoteConsumer {
    public static void main(String args[]) throws MalformedURLException,
            IOException {
        String quoteId = args.length >0 ? args[0] : "random";
        getQuoteFromQuoteService(quoteId);
    }
    public static void getQuoteFromQuoteService(String quoteId) throws
            MalformedURLException, IOException {
        String uri = "http://gturnquist-quoters.cfapps.io/api/"+quoteId;
        URL url = new URL(uri);
        HttpURLConnection connection;
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Accept", "application/xml, */*");
        InputStream in = connection.getInputStream();
        JsonReader jsonReader = Json.createReader(in);
        JsonObject object = jsonReader.readObject();
        jsonReader.close(); connection.disconnect();
        Gson gson = new GsonBuilder().create();
        Quote q = gson.fromJson(object.toString(), Quote.class);
        System.out.println("Tipo: " + q.getType());
        System.out.println("Id: " + q.getValue().getId());
        System.out.println("Citacao: " + q.getValue().getQuote());
    }
}

url.java

import java.net.MalformedURLException;
import java.net.URL;

public class url {
    public static void main(String args[])
    {
        int argc = args.length;
        if (argc != 1){
            System.out.println ("Syntax : java URLParser url ");
            return;
        }
        try {
            URL myURL = new URL( args[0] );
            System.out.println ("Protocol : " + myURL.getProtocol() );
            System.out.println ("Hostname : " + myURL.getHost() );
            System.out.println ("Port     : " + myURL.getPort() );
            System.out.println ("Filename : " + myURL.getFile() );
        }catch (MalformedURLException e){
            System.err.println ("Unable to parse URL!");
            return;
        }
    }
}

URLConnection.java

import java.net.*;
import java.io.*;

public class URLConnection {

    public static void main(String args[]) throws Exception {
        int argc = args.length;
        if (argc != 1) {
            System.out.println("Syntax: java FetchURLConnection url");
            return;
        }
        try {
            java.net.URL myURL = new URL(args[0]);
            URLConnection connection = myURL.openConnection();
            connection.connect();

            // DISPLAY THE MIME CONTENT-TYPE (E.G. TEXT/HTML)
            String MIME = ((java.net.URLConnection) connection).getContentType();
            System.out.println("Content-type: " + MIME);

            // DISPLAY, IF AVAILABLE, THE CONTENT LENGTH
            int contentLength = connection.getContentLength();
            if (contentLength != -1) {
                System.out.println("Content-length: " + contentLength);
            }

            // Pause for user
            System.out.println("Hit enter to continue");
            System.in.read();

            // READ THE CONTENTS OF THE RESOURCE FROM THE CONNECTION
            InputStream in = connection.getInputStream();

            // BUFFER THE STREAM, FOR BETTER PERFORMANCE
            BufferedInputStream bufIn = new BufferedInputStream(in);
            while ((data = bufIn.read()) > 0) {
                System.out.print((char) data);
            }
        } catch (MalformedURLException e) {
            System.err.println("Unable to parse URL!");
            return;
        } catch (IOException e) {
            System.err.println("I/O Error : " + ioe);
            return;
        }
    }
}
Tags : ,

0 thoughts on “aula10Thttp”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.