9 Replies Latest reply: Feb 23, 2009 2:29 PM by DrClap RSS

    HTTPUnit: submitting an online form in Java

    807588
      Hi, I have a problem filling out an online form using the HttpUnit API in Java. I have a java program that opens up an IE browser and goes to the website, but when I try to send the WebRequest, the program gives me a "Connection timed out" error. Not sure what I'm missing here, I have the same coding as many other examples I've found around the web for form validation. Here is my code:


      import com.meterware.httpunit.*;
      import com.meterware.httpunit.cookies.CookieProperties;
      import com.meterware.httpunit.cookies.CookieListener;
      import com.sun.org.apache.xerces.internal.util.URI;

      import java.awt.*;
      import java.net.URL;

      public class expertLogin
      {
      public static void main(String[] args)
      {
      try
      {
      String url = "http://fedexifc.infousa.com/QAA/demo/Login.aspx";

      WebConversation wc = new WebConversation();
      CookieProperties.setDomainMatchingStrict(false);
      CookieProperties.setPathMatchingStrict(false);
      HttpUnitOptions.setExceptionsThrownOnScriptError(false);

      CookieProperties.addCookieListener(new CookieListener()
      {
      public void cookieRejected(String s, int i, String s1)
      {
      System.out.println("CookieProperties.isDomainMatchingStrict() = " + CookieProperties.isDomainMatchingStrict());
      System.out.println("CookieProperties.isPathMatchingStrict() = " + CookieProperties.isPathMatchingStrict());
      System.out.println("s = " + s);
      System.out.println("s1 = " + s1);
      System.out.println("Reason: " + i);
      }
      });

      Runtime.getRuntime().exec("C:\\Program Files\\Internet Explorer\\iexplore.exe \"http://fedexifc.infousa.com/QAA/demo/Login.aspx\"");

      WebRequest req = new GetMethodWebRequest("http://fedexifc.infousa.com/QAA/demo/Login.aspx");
      WebResponse resp = wc.getResponse(req);

      WebForm loginForm = resp.getFormWithName("form1");


      String[] cookies = wc.getCookieNames();
      for (int i = 0; i < cookies.length; i++)
      {
      String cooky = cookies;
      System.out.println(cooky + " = " + wc.getCookieValue(cooky));
      }

      loginForm.setParameter("TextBoxUsername", username");
      loginForm.setParameter("TextBoxPassword", "password");
      loginForm.setParameter("TextBoxGroup", "group");

      loginForm.submit();
      for (int i = 0; i < cookies.length; i++)
      {
      String cooky = cookies[i];
      System.out.println(cooky + " = " + wc.getCookieValue(cooky));
      }
      resp = wc.getCurrentPage();

      System.out.println(resp.getText());
      }
      catch (Exception e)
      {
      e.printStackTrace();
      }
      }
      }
        • 2. Re: HTTPUnit: submitting an online form in Java
          JoachimSauer
          Making your posting easier to read and providing as much information as possible will help you get much faster and better answers:

          1.) Format your code (1. copy-and-paste your code 2. select your code 3. press "CODE" just above the text field).
          2.) Post the complete error message together with any stack traces that you might get
          3.) Post a SSCCE if possible.
          4.) Learn [How To Ask Questions The Smart Way|http://www.catb.org/~esr/faqs/smart-questions.html].
          • 3. Re: HTTPUnit: submitting an online form in Java
            807588
            Sorry, I can't figure out how to edit this thread or delete the extra one I posted. And I think I use the CODE option incorrectly.
            • 4. Re: HTTPUnit: submitting an online form in Java
              JoachimSauer
              jssutton11 wrote:
              Sorry, I can't figure out how to edit this thread
              You can't edit posts that already have replies.
              or delete the extra one I posted.
              Can't be done, just post a note in both threads about which one you want to continue using.
              And I think I use the CODE option incorrectly.
              That's what the preview tab is for.
              • 5. Re: HTTPUnit: submitting an online form in Java
                791266
                JoachimSauer wrote:
                jssutton11 wrote:
                Sorry, I can't figure out how to edit this thread
                You can't edit posts that already have replies.
                or delete the extra one I posted.
                Can't be done, just post a note in both threads about which one you want to continue using.
                I deleted the other thread.
                • 6. Re: HTTPUnit: submitting an online form in Java
                  807588
                  Well since I can't edit my post and I can't create a duplicate better post, anyway I can still get help on my question?
                  • 7. Re: HTTPUnit: submitting an online form in Java
                    JoachimSauer
                    jssutton11 wrote:
                    Well since I can't edit my post and I can't create a duplicate better post, anyway I can still get help on my question?
                    Of course. But you still need to provide the complete error message and the formatted code. Just post them as a reply here.

                    And read the links I provided.
                    • 8. Re: HTTPUnit: submitting an online form in Java
                      807588
                      I'll try this again. I have a problem filling out an online form using the HttpUnit API in Java. I have a java program that opens up an IE browser and goes to the website, but when I try to send the WebRequest, the program gives me a "Connection timed out" error. Not sure what I'm missing here, I have the same coding as many other examples I've found around the web for form validation. Here is my code:
                      import com.meterware.httpunit.*;
                      import com.meterware.httpunit.cookies.CookieProperties;
                      import com.meterware.httpunit.cookies.CookieListener;
                      import com.sun.org.apache.xerces.internal.util.URI;
                      
                      import java.awt.*;
                      import java.net.URL;
                      
                      public class expertLogin
                      {
                      public static void main(String[] args)
                      {
                      try
                      {
                      String url = "http://fedexifc.infousa.com/QAA/demo/Login.aspx";
                      
                      WebConversation wc = new WebConversation();
                      CookieProperties.setDomainMatchingStrict(false);
                      CookieProperties.setPathMatchingStrict(false);
                      HttpUnitOptions.setExceptionsThrownOnScriptError(false);
                      
                      CookieProperties.addCookieListener(new CookieListener()
                      {
                      public void cookieRejected(String s, int i, String s1)
                      {
                      System.out.println("CookieProperties.isDomainMatchingStrict() = " + CookieProperties.isDomainMatchingStrict());
                      System.out.println("CookieProperties.isPathMatchingStrict() = " + CookieProperties.isPathMatchingStrict());
                      System.out.println("s = " + s);
                      System.out.println("s1 = " + s1);
                      System.out.println("Reason: " + i);
                      }
                      });
                      
                      Runtime.getRuntime().exec("C:\\Program Files\\Internet Explorer\\iexplore.exe \"http://fedexifc.infousa.com/QAA/demo/Login.aspx\"");
                      
                      WebRequest req = new GetMethodWebRequest("http://fedexifc.infousa.com/QAA/demo/Login.aspx");
                      WebResponse resp = wc.getResponse(req);
                      
                      WebForm loginForm = resp.getFormWithName("form1");
                      String[] cookies = wc.getCookieNames();
                      for (int i = 0; i < cookies.length; i++)
                      {
                      String cooky = cookies;
                      System.out.println(cooky + " = " + wc.getCookieValue(cooky));
                      }

                      loginForm.setParameter("TextBoxUsername", username");
                      loginForm.setParameter("TextBoxPassword", "password");
                      loginForm.setParameter("TextBoxGroup", "group");

                      loginForm.submit();
                      for (int i = 0; i < cookies.length; i++)
                      {
                      String cooky = cookies[i];
                      System.out.println(cooky + " = " + wc.getCookieValue(cooky));
                      }
                      resp = wc.getCurrentPage();

                      System.out.println(resp.getText());
                      }
                      catch (Exception e)
                      {
                      e.printStackTrace();
                      }
                      }
                      }{code}

                      The error message is:

                      {code}
                      java.net.ConnectException: Connection timed out: connect
                           at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
                           at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
                           at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
                           ...11 more
                      Caused by: java.net.ConnectException: Connection timed out: connect
                      ...9 more{code}
                      • 9. Re: HTTPUnit: submitting an online form in Java
                        DrClap
                        "Connection timed out" means what you might think it means. The program tried to connect to the server, but it couldn't, so after a while it gave up. That's called "timing out" in the networking biz.

                        Obvious things to check include whether that's the right URL for the site. And whether your network will let you make HTTP connections outside the network without assistance (I'm talking about proxy servers here, for example). If you've got a networking person available, talk to them.