I posted the message below on this list but saw no response.  Is this the
wrong place to post this kind of message?  If so, please could somebody
tell me the right place to post?

Thanks,

Dominic
http://boole.stanford.edu/~dominic

---------- Forwarded message ----------
Date: Sun, 26 May 2002 19:30:52 -0700 (PDT)
From: Dominic Hughes <[EMAIL PROTECTED]>
To: Jakarta Commons Developers List <[EMAIL PROTECTED]>
Subject: httpclient: possible HttpClient/PostMethod bug

The attached test program highlights a possible bug in
HttpClient/PostMethod: at some web sites the parameters of a post do not
seem to register correctly.

The test program retrieves an html page from
http://sirius.chinalake.navy.mil using a post method in two different
ways: (1) "by hand", using standard java classes (URLConnection etc), and
(2) using classes HttpClient and PostMethod.  As would be expected, the
page retrieved by (1) is identical to the one obtained by entering the
post parameters by hand at http://sirius.chinalake.navy.mil/satpred/.  
However, (2) returns a garbled page: the post parameters do not seem to
register properly.  Can anybody explain why?

I attached the output of the test program, which prints out a few lines of
the page retrieved by (1) and the corresponding lines of the page
retrieved by (2) --- clearly different.

Is this really a bug in HttpClient/PostMethod, or does the test program
employ HttpClient and PostMethod incorrectly?

Feedback welcome!

Dominic Hughes and Julien Basch
http://boole.stanford.edu/~dominic
/* HttpClientBug
   May 25, 2002
   Dominic Hughes
   [EMAIL PROTECTED]
 
This test program highlights a possible bug in the HttpClient and/or
PostMethod classes (packages org.apache.commons.httpclient and
org.apache.commons.httpclient.methods): at some web sites the
parameters of a post do not seem to register correctly.

The program retrieves an html page from
http://sirius.chinalake.navy.mil using a post method in two different
ways: (1) "by hand", using standard java classes (URLConnection etc),
and (2) using classes HttpClient and PostMethod.  As would be
expected, the page retrieved by (1) is identical to the one obtained
by entering the post parameters by hand at
http://sirius.chinalake.navy.mil/satpred/.  However, (2) returns a
garbled page: the post parameters do not seem to register properly.

The program prints out a few lines of the page retrieved by (1) and
the corresponding lines of the page retrieved by (2) --- clearly
different.

*/

import org.apache.commons.httpclient.*;         // version 2.0-alpha1
import org.apache.commons.httpclient.methods.*; // version 2.0-alpha1
import java.io.*;
import java.net.*;

public class HttpClientBug {

  // names of fields for post
  public static String[] satelliteArgs = {
    "northsouth", "latdeg", "latmin", "latsec", "eastwest",
    "longdeg", "longmin", "longsec", "altitude", "elangle",
    "toahour", "toamin", "timezone", "toamonth", "toaday",
    "toayear"
  };

  // values of fields for post
  public static String[] satelliteVals = {
    "North", "9", "9", "9", "West",
    "9", "9", "9", "9", "9",
    "9", "9","", "9", "9",
    "2002"
  };

  public static void main(String[] args) throws Exception {
    printLineOf('=');
    System.out.println(" POSSIBLE HTTPCLIENT BUG");
    printLineOf('=');
    String domain = "http://sirius.chinalake.navy.mil";;
    String cgi = "/satpred/satpred.cgi";
    System.out.println("domain:\n "+domain+"\n");
    System.out.println("cgi:\n "+cgi+"\n");
    System.out.println("post parameters:");
    for (int i=0; i<satelliteArgs.length; i++) {
      System.out.print(" \""+satelliteArgs[i]+"\"=\""+satelliteVals[i]+"\",");
      if (i%4==3) System.out.println();
    }
    printLineOf('-');

    // print result of simple post
    System.out.println("SIMPLE POST (java.io.URLConnection etc.)\n");
    String query = "";
    for (int i=0; i<satelliteArgs.length; i++) {
      query += satelliteArgs[i]+"="+satelliteVals[i];
      if (i<satelliteArgs.length-1) query += "&";
    }
    System.out.println("query:\n "+query+"\n");
    String postPage = simplePost(domain,cgi,query);
    System.out.println("Below is a fragment of the html response page.");
    System.out.println("It coincides with the result of entering\n"+
                       "the parameters by hand at "+domain+cgi+".]");
    showReleventFragment(postPage);
    System.out.println();
    printLineOf('-');

    // print result of httpclient post
    System.out.println("POST USING HTTPCLIENT PACKAGE (HttpClient and PostMethod 
classes)\n");
    String httpclientPage = httpclientPost(domain,cgi,satelliteArgs,satelliteVals);
    System.out.println("Below is a fragment of the html response page.");
    System.out.println("Surprisingly, it is a different page.\n"+
                       "The posted parameters do not seem to have registered 
properly.\n"+
                       "Is there a bug in HttpClient/PostMethod, or did I\n"+
                       "employ HttpClient/PostMethod incorrectly?");
    showReleventFragment(httpclientPage);
    printLineOf('=');
    
  }

  public static String httpclientPost(String domain, String cgi, String[] args, 
String[] vals) {
    try {
      HttpClient hc = new HttpClient();
      hc.startSession(new URL(domain));
      try {
        PostMethod pm = new PostMethod(cgi);
        for (int i=0; i<satelliteArgs.length; i++) {
          pm.setParameter(satelliteArgs[i],satelliteVals[i]);
        }
        try {
          hc.executeMethod(pm);
        }
        catch (HttpException e) {
          e.printStackTrace(); System.exit(0);
        }
        return pm.getResponseBodyAsString();
      } 
      catch (IOException e) {
        e.printStackTrace(); System.exit(0);
      }
    } 
    catch (MalformedURLException e) {
      e.printStackTrace(); System.exit(0);
    }
    return null;
  }
  
  public static String simplePost(String domain, String cgi, String query) {
    String page = "";
    try {
      URL url = new URL(domain+cgi);
      try {
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        PrintWriter out = new PrintWriter(connection.getOutputStream());
        out.print(query+"\n");
        out.close();
        BufferedReader in = 
          new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
          page += line+"\n";
        }
      } 
      catch (IOException e) {
        e.printStackTrace(); System.exit(0);
      }
    } 
    catch (MalformedURLException e) {
      e.printStackTrace(); System.exit(0);
    }
    return page;
  }

  public static void showReleventFragment(String page) {
    int i = page.indexOf("Altitude");
    int j = page.indexOf("<CENTER",i);
    System.out.println("\n"+page.substring(i,j));
  }

  public static void printLineOf(char c) {
    String line = "";
    for (int i=0; i<80; i++) line += c;
    System.out.println("\n"+line+"\n");
  }

}


================================================================================

 POSSIBLE HTTPCLIENT BUG

================================================================================

domain:
 http://sirius.chinalake.navy.mil

cgi:
 /satpred/satpred.cgi

post parameters:
 "northsouth"="North", "latdeg"="9", "latmin"="9", "latsec"="9",
 "eastwest"="West", "longdeg"="9", "longmin"="9", "longsec"="9",
 "altitude"="9", "elangle"="9", "toahour"="9", "toamin"="9",
 "timezone"="", "toamonth"="9", "toaday"="9", "toayear"="2002",

--------------------------------------------------------------------------------

SIMPLE POST (java.io.URLConnection etc.)

query:

northsouth=North&latdeg=9&latmin=9&latsec=9&eastwest=West&longdeg=9&longmin=9&longsec=9&altitude=9&elangle=9&toahour=9&toamin=9&t

imezone=&toamonth=9&toaday=9&toayear=2002

Below is a fragment of the html response page.
It coincides with the result of entering
the parameters by hand at
http://sirius.chinalake.navy.mil/satpred/satpred.cgi.]

Altitude  =          9.0 Meters<br>
Latitude  =    9<code>&#176 </code>  9' 9.00"N<br>
Longitude =    9<code>&#176 </code>  9' 9.00"W<br>
<P>Using an elevation mask of 9<code>&#176 </code><br>There are 11
satellites in view<P>The best GDOP is obtained using satellites
:  1, 27, 28 and 29 <br>GDOP =  2.4<br>PDOP =  2.2<br>TDOP = 
1.0<br>HDOP =  1.4<br>VDOP =  1.7<br><P><BR>


--------------------------------------------------------------------------------

POST USING HTTPCLIENT PACKAGE (HttpClient and PostMethod classes)

Below is a fragment of the html response page.
Surprisingly, it is a different page.
The posted parameters do not seem to have registered properly.
Is there a bug in HttpClient/PostMethod, or did I
employ HttpClient/PostMethod incorrectly?

Altitude  =          0.0 Meters<br>
Latitude  = -2147483648<code>&#176 </code>
-2147483647'204677678184175494379992413029724457968437121397220744632589868294450076770

1766402877622902264328852766756285587038035965167610409721679129345495919243744665943373259402487668450056773049470605970805711827

6535476696341242701676544000.00"N<br>
Longitude =    9<code>&#176 </code>  0' 0.00"E<br>
<P>Using an elevation mask of 0<code>&#176 </code><br>There are 13
satellites in view<P>The best GDOP is obtained using satellites
:  3,  6, 24 and 30 <br>GDOP =  2.3<br>PDOP =  2.2<br>TDOP = 
0.7<br>HDOP =  1.3<br>VDOP =  1.8<br><P><BR>

================================================================================


__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com
--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to