You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by Eric Chow <ec...@macaucabletv.com> on 2003/05/22 08:59:02 UTC

No response return after execute PostMethod ???

Hi,

I tried to use HttpClient to Login and post some data to a web site.

but it just return null in the ResponseBody.

When called the book() method, it will not return any response.


The following is my source code.


import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.cookie.*;
import org.apache.commons.httpclient.util.*;
import java.io.*;



public class BookCourt {
   private static String logonURL =
"https://isw.umac.mo/cdweb/CDW210_LoginServlet";
   private static String logoutURL =
"https://isw.umac.mo/cdweb/CDW250_Logout.jsp";
   private static String listURL =
"https://isw.umac.mo/cdweb/CDW222_TimetableEnqResult.jsp";
   private static String bookURL =
"https://isw.umac.mo/cdweb/CDW230_WebBookingNew.jsp";
   private static String bookAction =
"https://isw.umac.mo/cdweb/CDW230_WebBookingNewServlet";
   private static String bookConfirmAction =
"https://isw.umac.mo/cdweb/CDW231_WebBookingNewAddServlet";


   private static String[] timeStatus = { "7:00pm - 10:00pm",
                                          "6:00pm - 9:00pm",
                                          "8:00pm - 10:00pm",
                                          "7:00pm - 9:00pm",
                                          "6:00pm - 8:00pm"
                                        };

   public static void main(String argv[]) {
      HttpClient client = new HttpClient();

      try {
         System.setProperty("java.protocol.handler.pkgs",
"com.sun.net.ssl.internal.www.protocol");


         System.setProperty("org.apache.commons.logging.Log",
"org.apache.commons.logging.impl.SimpleLog");


System.setProperty("org.apache.commons.logging.simplelog.showdatetime",
"true");


System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire
", "debug");


System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.comm
ons.httpclient", "trace");



         HostConfiguration hc = new HostConfiguration();
         hc.setHost("isw.umac.mo", 443, "https");
         client.setHostConfiguration(hc);

         // login to the system
         login(client);

         //info(client);

         int b = isCourtAvailable(client, argv[0], 1);
         b = isCourtAvailable(client, argv[0], 2);
         b = isCourtAvailable(client, argv[0], 3);

         if (b > 0) {
            System.out.println(argv[0] + " available time is " +
timeStatus[b-1] + "\n");
         }


         switch(b) {
            case 1:
               //book(client, argv[0], "19", "22", 1);
               //book(client, argv[0], "19", "22", 2);
               book(client, argv[0], "19", "22", 3);
               break;

            case 2:
               //book(client, argv[0], "18", "21", 1);
               //book(client, argv[0], "18", "21", 2);
               book(client, argv[0], "18", "21", 3);
               break;

            case 3:
               //book(client, argv[0], "20", "22", 1);
               //book(client, argv[0], "20", "22", 2);
               book(client, argv[0], "20", "22", 3);
               break;

            case 4:
               //book(client, argv[0], "19", "21", 1);
               //book(client, argv[0], "19", "21", 2);
               book(client, argv[0], "19", "21", 3);
               break;

            case 5:
               //book(client, argv[0], "18", "20", 1);
               //book(client, argv[0], "18", "20", 2);
               book(client, argv[0], "18", "20", 3);
               break;

            default:
               System.out.println("No court available");
         };

      } catch(Exception e) {
         e.printStackTrace();
      } finally {
         try {
            logout(client);
         } catch(Exception e1) {}
      }
   }


   public static int isCourtAvailable(HttpClient client, String d, int
tries) {

      try {
         HttpMethod bookMethod = new GetMethod(listURL +
"?wv_CheckPlace=SQ1&wv_EnqMode=P");

         int bStatus = client.executeMethod(bookMethod);

         String responseBody = bookMethod.getResponseBodyAsString();

         //System.out.println(responseBody);

         if (tries == 3) {


            if (responseBody.indexOf(d) >= 0) {

               StringReader sr = new StringReader(responseBody);
               LineNumberReader lnr = new LineNumberReader(sr);


               String s = null;

               do {
                  s = lnr.readLine();

                  //System.out.println(s);

                  if (s.indexOf(d) >= 0) {
                     String s1 = "";
                     String s2 = "";
                     String s3 = "";
                     String s4 = "";

                     for(int i = 0; i < 10; i++) {
                        s = lnr.readLine();
                     }

                     s1 = lnr.readLine(); // 6:00pm
                     s2 = lnr.readLine(); // 7:00pm
                     s3 = lnr.readLine(); // 8:00pm
                     s4 = lnr.readLine(); // 9:00pm

                     //System.out.println("....");
                     //System.out.println(s1);
                     //System.out.println(s2);
                     //System.out.println(s3);
                     //System.out.println(s4);
                     //System.out.println("....");

                     if (s2.indexOf("available") >= 0 &&
s3.indexOf("available") >= 0 && s4.indexOf("available") >= 0) {
                        //System.out.println("Available from 7:00pm -
10:00pm");

                        return 1;
                     }

                     if (s1.indexOf("available") >= 0 &&
s2.indexOf("available") >= 0 && s3.indexOf("available") >= 0) {
                        //System.out.println("Available from 6:00pm -
9:00pm");

                        return 2;
                     }


                     if ( s3.indexOf("available") >= 0 &&
s4.indexOf("available") >= 0) {
                        //System.out.println("Available from 8:00pm -
10:00pm");

                        return 3;
                     }

                     if ( s2.indexOf("available") >= 0 &&
s3.indexOf("available") >= 0) {
                        //System.out.println("Available from 7:00pm -
9:00pm");

                        return 4;
                     }

                      if ( s1.indexOf("available") >= 0 &&
s2.indexOf("available") >= 0) {
                        //System.out.println("Available from 6:00pm -
8:00pm");

                        return 5;
                     }

                  }
               } while(s != null);


               lnr.close();
               sr.close();

            }
         }

         redirect(client, bStatus,
bookMethod.getResponseHeader("location"));

         bookMethod.releaseConnection();
      } catch(Exception e) {
         e.printStackTrace();
      }

      return 0;
   }



   public static boolean book(HttpClient client, String d, String sTime,
String eTime, int tries) throws Exception {


      System.out.println("Try to book court on " + d + " (" + sTime+":00 - "
+ eTime+":00)");


      NameValuePair nv[] = new NameValuePair[35];


      nv[0] = new NameValuePair("wv_booking_date", d);
      nv[1] = new NameValuePair("wv_booking_place", "PAV");
      nv[2] = new NameValuePair("wv_booking_time_from", "14");
      nv[3] = new NameValuePair("wv_booking_time_to", "15");
      nv[4] = new NameValuePair("wv_booking_light", "Y");
      //nv[5] = new NameValuePair("chk_booking_light", "on");
      nv[5] = new NameValuePair("wv_booking_air", "N");
      //nv[7] = new NameValuePair("chk_booking_air", "on");
      nv[6] = new NameValuePair("wv_booking_facilities", "xxx");

      nv[7] = new NameValuePair("wv_booking_date", "");
      nv[8] = new NameValuePair("wv_booking_place", "");
      nv[9] = new NameValuePair("wv_booking_time_from", "");
      nv[10] = new NameValuePair("wv_booking_time_to", "");
      nv[11] = new NameValuePair("wv_booking_light", "");
      nv[12] = new NameValuePair("wv_booking_air", "");
      nv[13] = new NameValuePair("wv_booking_facilities", "");

      nv[14] = new NameValuePair("wv_booking_date", "");
      nv[15] = new NameValuePair("wv_booking_place", "");
      nv[16] = new NameValuePair("wv_booking_time_from", "");
      nv[17] = new NameValuePair("wv_booking_time_to", "");
      nv[18] = new NameValuePair("wv_booking_light", "");
      nv[19] = new NameValuePair("wv_booking_air", "");
      nv[20] = new NameValuePair("wv_booking_facilities", "");

      nv[21] = new NameValuePair("wv_booking_date", "");
      nv[22] = new NameValuePair("wv_booking_place", "");
      nv[23] = new NameValuePair("wv_booking_time_from", "");
      nv[24] = new NameValuePair("wv_booking_time_to", "");
      nv[25] = new NameValuePair("wv_booking_light", "");
      nv[26] = new NameValuePair("wv_booking_air", "");
      nv[27] = new NameValuePair("wv_booking_facilities", "");


      nv[28] = new NameValuePair("wv_booking_date", "");
      nv[29] = new NameValuePair("wv_booking_place", "");
      nv[30] = new NameValuePair("wv_booking_time_from", "");
      nv[31] = new NameValuePair("wv_booking_time_to", "");
      nv[32] = new NameValuePair("wv_booking_light", "");
      nv[33] = new NameValuePair("wv_booking_air", "");
      nv[34] = new NameValuePair("wv_booking_facilities", "");



      PostMethod pMethod = new PostMethod(bookAction);

      pMethod.setRequestBody(nv);
      client.executeMethod(pMethod);


      String s = pMethod.getResponseBodyAsString();

      System.out.println(s);

      System.out.println(pMethod.getStatusLine().toString());




      int statuscode = pMethod.getStatusCode();
      redirect(client, statuscode, pMethod.getResponseHeader("location"));

      System.out.println("end booking");


      pMethod.releaseConnection();

      return false;
   }


   public static void login(HttpClient client) throws Exception {


      PostMethod authpost = new PostMethod(logonURL);

      NameValuePair nv[] = new NameValuePair[4];


      nv[0] = new NameValuePair("wv_user_id", "MY_USER_NAME");
      nv[1] = new NameValuePair("wv_user_password", "MY_PASSWORD");
      nv[2] = new NameValuePair("BtnReset", "Reset");
      nv[3] = new NameValuePair("BtnSubmit", "Login");

      authpost.setRequestBody(nv);



      System.out.print("Logon .... ");

      client.executeMethod(authpost);

      System.out.println(authpost.getStatusLine().toString());



       int statuscode = authpost.getStatusCode();



      redirect(client, statuscode, authpost.getResponseHeader("location"));

      authpost.releaseConnection();
   }

   public static void logout(HttpClient client) throws Exception {
      GetMethod logoutMethod = new GetMethod(logoutURL);

      System.out.print("Logout .... ");

      int logoutStatus = client.executeMethod(logoutMethod);

      System.out.println(logoutMethod.getStatusLine().toString());

      //System.out.println(new String(logoutMethod.getResponseBody()));

      logoutMethod.releaseConnection();
   }


   public static void redirect(HttpClient client, int statuscode, Header
iheader) throws Exception {

      if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
            (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
            (statuscode == HttpStatus.SC_SEE_OTHER) ||
            (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {

         Header header = iheader;

         if (header != null) {
            String newuri = header.getValue();

            if ((newuri == null) || (newuri.equals(""))) {
               newuri = "/";
            }

            System.out.println("Redirect target: " + newuri);

            GetMethod redirect = new GetMethod(newuri);

            client.executeMethod(redirect);

            System.out.println("Redirect: " +
redirect.getStatusLine().toString());

            //System.out.println(new String(redirect.getResponseBody()));

            redirect.releaseConnection();
         } else {
            System.out.println("Invalid redirect");
            System.exit(1);
         }
      }
   }

}





==========================
If you know what you are doing,
it is not called RESEARCH!
==========================