Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 47 48 49 50 51 52 < 53 > 54 55 56 57 58 59 .. 166 >> Next

134 Item 17
¦¦ Although it is commented out, it is also illegal to attempt to set a request property after getting an input or output stream. The documentation for URLConnection does state the sequence to set up a connection, although it does not state this is a mandatory sequence.
If we did not have the luxury of examining the source code (which definitely should not be a requirement to use an API), we would be reduced to trial and error (the absolute worst way to program). Neither the documentation nor the API of the HttpURLConnection class afford us any understanding of how the protocol is implemented, so we feebly attempt to reverse the order of calls to getInputStream() and getOutputStream(). Listing 17.3, BadURLPostl.java, is an abbreviated version of that program.
01 package org 7 1 m e t i s l l a f t i p a v a j
02
03 import java .net.*;
04 import java .io.*;
05
06 public class BadURLPost1
07 {
08 public static void main(String args[])
09 {
// removed for brevity
35 System.out.println("Getting an output stream...");
36 OutputStream os = con.getOutputStream();
37
38 System.out.println("Getting an input stream...");
39 InputStream is = con.getInputStream();
// removed for brevity
67 }
68 }
Listing 17.3 BadURLPost1.java
A run of Listing 17.3 produces the following:
E:\classes\org\javapitfalls\Item17>java org.javapitfalls. ^
item17.BadURLPost1 http://localhost/cgi-bin/echocgi.exe Received a : sun.net.www.protocol.http.HttpURLConnection Getting an output stream...
Getting an input stream...
After flushing output stream. line: <HEAD>
line: <TITLE> Echo CGI program </TITLE> line: </HEAD>
line: <BODY BGCOLOR=’#ebebeb’><CENTER>
When Posting to a URL Won't 135
line: <H2> Echo </H2>
line: </CENTER>
line: No content! ERROR!
line: </BODY> line: </HTML>
Although the program compiles and runs, the CGI program reports that no data was sent! Why? Again we were bitten by the side effects of getInputStream(), which caused the POST request to be sent before anything was put in the post's output buffer, thus sending an empty post request.
Now, after having failed twice, we understand that the getInputStream() is the key method that actually writes the requests to the server. Therefore, we must perform the operations serially (open output, write, open input, read) as we do in Listing 17.4, GoodURLPost.java.
01 package org 7 1 m e t i s l l a f t i p a v a j
02
03 import java .net.*;
04 import java .io.*;
05
06 public class GoodURLPost
07 {
08 public static void main(String args[])
09 {
10 // get an HTTP connection to POST to
11 if (args.length < 1)
12 {
13 System.out.println("USAGE: java 2
GOV.dia. mditds. util.GoodURLPost url");
14 System.exit(1);
15 }
16
17 try
18 {
19 // get the url as a string
20 String surl = args[0];
21 URL url = new URL(surl);
22
23 URLConnection con = url.openConnection();
24 System.out.println("Received a : " + 2
con.getClass(). getName());
25
26 con.setDolnput(true);
27 con.setDoOutput(true);
28 con.setUseCaches(false);
29
30 String msg = "Hi HTTP SERVER! Just a quick hello!";
Listing 17.4 GoodURLPost.java (continued)
136 Item 17
31 con.setRequestProperty("CONTENT_LENGTH", "" + ^
msg.length()); // Not checked
32 System.out.println("Msg Length: " + msg.length());
33
34 System.out.println("Getting an output stream...");
35 OutputStream os = con.getOutputStream();
36
37 OutputStreamWriter osw = new OutputStreamWriter(os);
38 osw.write(msg);
39 /** REMEMBER THIS osw.flush(); **/
40 osw.flush();
41 osw.close();
42
43 System.out.println("After flushing output stream. ");
44
45 System.out.println("Getting an input stream...");
46 InputStream is = con.getInputStream();
47
48 // any response?
49 InputStreamReader isr = new InputStreamReader(is);
50 BufferedReader br = new BufferedReader(isr);
51 String line = null;
52
53 while ( (line = br.readLine()) != null)
54 {
55 System.out.println("line: " + line);
56 }
57 } catch (Throwable t)
58 {
59 t.printStackTrace();
60 }
61 }
62 }
Listing 17.4 (continued)
A run of Listing 17.4 produces the following:
E:\classes\
org\javapitfalls\Item17>javaorg.javapitfalls.item17.GoodURLPost
http://localhost/cgi-bin/echocgi.exe
Received a : sun.net.www.protocol.http.HttpURLConnection ^
Msg Length: 35
Getting an output stream...
After flushing output stream.
Getting an input stream... line: <HEAD>
line: <TITLE> Echo CGI program </TITLE>
When Posting to a URL Won't 137
line: </HEAD>
line: <BODY BGCOLOR='#ebebeb'><CENTER> line: <H2> Echo </H2> line: </CENTER> line: Length of content: 35
line: Content: Hi HTTP SERVER! Just a quick hello! line: </BODY> line: </HTML>
Finally, success! We now can post data to a CGI program running on a Web server. To summarize, to avoid the HTTP post pitfall, do not assume the methods behave as they do for a socket. Instead, the getInputStream() method has the side effect of writing the requests to the Web server. Therefore, the proper sequence must be observed.
Previous << 1 .. 47 48 49 50 51 52 < 53 > 54 55 56 57 58 59 .. 166 >> Next