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 .. 48 49 50 51 52 53 < 54 > 55 56 57 58 59 60 .. 166 >> Next

One final note on this class is to understand the complexity of writing characters to the Web server. In the above programs, I use the default encoding when writing the String to the underlying socket. You could explicitly write bytes instead of characters by first retrieving the bytes via getBytes() of the String class. Additionally, you could explicitly set the encoding of the characters using the OutputStreamWriter class.
An Alternative Open Source HTTP Client
A more intuitive open source package called HTTPClient can be downloaded from http://www.innovation.ch/java/HTTPClient. We will use two classes in this package, HTTPConnection and HTTPResponse, to accomplish the same functionality in GoodURLPost.java. Listing 17.5 demonstrates posting raw data using this package.
01 package org 7 1 m e t i s l l a f t i p a v a j
02
03 import HTTPClient.*;
04
05 import java .net.*;
06 import java .io.*;
07
08 public class HTTPClientPost
09 {
10 public static void main(String args[])
11 {
12 // get an HTTP connection to POST to
13 if (args.length < 2)
14 {
15 System.out.println("USAGE: java 2
org.javapitfalls.net.mcd.i1.HTTPClientPost host cgi-program");
16 System.exit(1);
17 }
Listing 17.5 HTTPClientPost.java (continued)
138 Item 17
18
19 try
20 {
21 // get the url as a string
22 String sHost = args[0];
23 String sfile = args[1];
24
25 HTTPConnection con = new HTTPConnection(sHost);
26
27 String msg = "Hi HTTP SERVER! Just a quick hello!";
28
29 HTTPResponse resp = con.Post(sfile, msg);
30 InputStream is = resp.getInputStream();
31
32 // any response?
33 InputStreamReader isr = new InputStreamReader(is);
34 BufferedReader br = new BufferedReader(isr);
35 String line = null;
36
37 while ( (line = br.readLine()) != null)
38 System.out.println("line: " + line);
39 } catch (Throwable t)
40 {
41 t.printStackTrace();
42 }
43 }
44 }
Listing 17.5 (continued)
A run of the HttpClientPost program produces:
E:\classes\org\javapitfalls>java org.javapitfalls.Item17.HTTPClientPost localhost /cgi-bin/echocgi.exe
line: <HEAD>
line: <TITLE> Echo CGI program </TITLE>
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>
As you can see, the results are the same as with GoodURLPost. Instead of raw data, you may want to send form input. Listing 17.6 is an example that sends the same form input as demonstrated in Figure 17.1.
When Posting to a URL Won't 139
01 package 7 1 m e t i s l l a f t i p a v a j g. r o
02
03 import HTTPClient.*;
04
05 import java.net.*;
06 import java.io.*;
07
08 public class HTTPClientPost2
09 {
10 public static void main(String args[])
11 {
12 // get an HTTP connection to POST to
13 if (args.length < 2)
14 {
15 System.out.println("USAGE: java 2
org.javapitfalls.net.mcd.i1.HTTPClientPost2 host cgi-program");
16 System.exit(1);
17 }
18
19 try
20 {
21 // get the url as a string
22 String sHost = args[0];
23 String sfile = args[1];
24
25 HTTPConnection con = new HTTPConnection(sHost);
26
27 NVPair form_data[] = new NVPair[2];
28 form_data[0] = new NVPair("theName", "Bill Gates");
29 form_data[1] = new NVPair("question1", "No");
30
31 HTTPResponse resp = con.Post(sfile, form_data);
32 InputStream is = resp.getInputStream();
33
34 // any response?
35 InputStreamReader isr = new InputStreamReader(is);
36 BufferedReader br = new BufferedReader(isr);
37 String line = null;
38
39 while ( (line = br.readLine()) != null)
40 System.out.println("line: " + line);
41 } catch (Throwable t)
42 {
43 t.printStackTrace();
44 }
45 }
46 }
Listing 17.6 HTTPClientPost2.java
140 Item 18
A run of the program HTTPClientPost2 produces the following:
E:\classes\org\javapitfalls\net\mcd\i1>java org.javapitfalls.net ^
.mcd.i1.HTTPClientPost2 localhost /cgi-bin/echocgi.exe
line: <HEAD>
line: <TITLE> Echo CGI program </TITLE>
line: </HEAD>
line: <BODY BGCOLOR='#ebebeb'><CENTER>
line: <H2> Echo </H2>
line: </CENTER>
line: Length of content: 31
line: Content: theName=Bill+Gates&question1=No
line: </BODY>
line: </HTML>
The results of HTTPClientPost2 are identical to the results in Figure 17.3. In conclusion, while you can use URLConnection to post data to Web servers, you will find it more intuitive to use an open-source alternative.
Item 18: Effective String Tokenizing8
This pitfall revealed itself when a junior developer needed to parse a text file that used a three-character delimiter (###) between tokens. His first attempt used the StringTokenizer class to parse the input text. He sought my advice after he discovered what he considered to be strange behavior. The run of the program below demonstrates code similar to his:
Previous << 1 .. 48 49 50 51 52 53 < 54 > 55 56 57 58 59 60 .. 166 >> Next