About Question enthuware.ocejws.v6.2.203 :

Moderators: Site Manager, fjwalraven

Post Reply
austinor
Posts: 41
Joined: Mon Oct 27, 2014 11:35 pm
Contact:

About Question enthuware.ocejws.v6.2.203 :

Post by austinor »

There's another approach for Java SE clients of web services (one of my favorites):

Sockets.

Code: Select all

    Socket socket = new Socket(); 
    socket.connect( new InetSocketAddress(InetAddress.getByName(host),port), timeout );

    BufferedWriter reqWrtr = new BufferedWriter( 
                                                   new OutputStreamWriter( socket.getOutputStream(),"UTF-8" )); 
    ... 

    BufferedReader respRdr = new BufferedReader( 
                                                    new InputStreamReader( socket.getInputStream() ) ); 
    ... 


fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

Re: About Question enthuware.ocejws.v6.2.203 :

Post by fjwalraven »

That is a nice one!

... but you won't find that one on the exam.

Regards,
Frits

johnlong
Posts: 197
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

Re: About Question enthuware.ocejws.v6.2.203 :

Post by johnlong »

Isn't necessary to do the following :

Code: Select all

connection.setDoInput(true);

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

Re: About Question enthuware.ocejws.v6.2.203 :

Post by fjwalraven »

Isn't necessary to do the following :
Yes, you are right that isn't necessary as the input flag is true by default.

Regards,
Frits

johnlong
Posts: 197
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

Re: About Question enthuware.ocejws.v6.2.203 :

Post by johnlong »

URLConnection connection = (URLConnection) restURL.openConnection();

Is cast necessary here?

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

Re: About Question enthuware.ocejws.v6.2.203 :

Post by fjwalraven »

No, you are right that is not necessary.

Regards,
Frits

witek_m
Posts: 16
Joined: Sat Jun 09, 2018 12:09 pm
Contact:

Re: About Question enthuware.ocejws.v6.2.203 :

Post by witek_m »

Hello, in the second answer:

Code: Select all

public static void main(String[]args)throwsIOException
        {  
    
            URLrestURL = newURL("http://localhost:8080/SimpleRS/jax/rs/add/5/8");      
            HttpURLConnectionconnection = (HttpURLConnection) restURL.openConnection();      
            connection.setRequestMethod("GET");      
            connection.setReadTimeout(10000);      
            connection.connect();          
            InputStreamReaderins = newInputStreamReader(connection.getInputStream());      
            BufferedReaderin = newBufferedReader(ins);      StringinputLine;      
            while ((inputLine = in.readLine()) != null) {         
                System.out.println(inputLine);      
            }      
            in.close();   
        }
is it not required to add a connection.setDoOutput(true)?

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

Re: About Question enthuware.ocejws.v6.2.203 :

Post by fjwalraven »

is it not required to add a connection.setDoOutput(true)?
No, with a GET you will only read from the connection (DoOutput is for a POST)

Regards,
Frits

witek_m
Posts: 16
Joined: Sat Jun 09, 2018 12:09 pm
Contact:

Re: About Question enthuware.ocejws.v6.2.203 :

Post by witek_m »

Yes, my mistake.

Post Reply

Who is online

Users browsing this forum: No registered users and 23 guests