http: submit a form from a Java class through a .bat file

Discussion in 'Java' started by Spendius, Jan 14, 2011.

  1. Spendius

    Spendius Guest

    Hi,
    I have a small NAS device that I can switch off once I'm logged on
    to it: I just have to open the management page using my favorite
    browser, type my login/pwd, then click on a 'Shutdown' button and
    that's it - no need to get back down to my basement to manually
    switch the contraption off.

    => I'd like write a small Java class that will
    1 perform the http connection to this device web server,
    2 then run/submit the 'shutdown' action to its small web server too...

    Is there an easy way to perform these 2 steps (using Struts maybe) ?
    I'd really like to be able to type something like:
    C:\temp> java -cp %CLASSPATH% SwitchOffMyNAS
    in a DOS window and have my external device to shut down...

    Feasible ?
    Thanks a lot in advance,
    Spendius
    Spendius, Jan 14, 2011
    #1
    1. Advertising

  2. Spendius

    Luuk Guest

    On 14-01-11 10:32, Spendius wrote:
    > Hi,
    > I have a small NAS device that I can switch off once I'm logged on
    > to it: I just have to open the management page using my favorite
    > browser, type my login/pwd, then click on a 'Shutdown' button and
    > that's it - no need to get back down to my basement to manually
    > switch the contraption off.
    >
    > => I'd like write a small Java class that will
    > 1 perform the http connection to this device web server,
    > 2 then run/submit the 'shutdown' action to its small web server too...
    >
    > Is there an easy way to perform these 2 steps (using Struts maybe) ?
    > I'd really like to be able to type something like:
    > C:\temp> java -cp %CLASSPATH% SwitchOffMyNAS
    > in a DOS window and have my external device to shut down...
    >
    > Feasible ?
    > Thanks a lot in advance,
    > Spendius


    it basically depend on how this page is created, and what info is
    submitted, but WGET could be a sollution

    http://www.gnu.org/software/wget/manual/wget.html


    --
    Luuk
    Luuk, Jan 14, 2011
    #2
    1. Advertising

  3. On 14 jan, 10:32, Spendius <> wrote:
    > Hi,
    > I have a small NAS device that I can switch off once I'm logged on
    > to it: I just have to open the management page using my favorite
    > browser, type my login/pwd, then click on a 'Shutdown' button and
    > that's it - no need to get back down to my basement to manually
    > switch the contraption off.
    >
    > => I'd like write a small Java class that will
    > 1 perform the http connection to this device web server,
    > 2 then run/submit the 'shutdown' action to its small web server too...
    >
    > Is there an easy way to perform these 2 steps (using Struts maybe) ?
    > I'd really like to be able to type something like:
    > C:\temp> java -cp %CLASSPATH% SwitchOffMyNAS
    > in a DOS window and have my external device to shut down...
    >
    > Feasible ?
    > Thanks a lot in advance,
    > Spendius


    wget is probably one of the easiest options.
    I also use the iMacros Firefox addon (https://addons.mozilla.org/fr/
    firefox/addon/imacros-for-firefox/) for these kinds of repetitive
    tasks. After all, my browser is almos always open, and not my console.
    If you really want to do it in Java, I'd recommend HtmlUnit (http://
    htmlunit.sourceforge.net/). It should be a matter of 10 lines of code.

    JB.
    Jean-Baptiste Nizet, Jan 14, 2011
    #3
  4. Spendius

    markspace Guest

    On 1/14/2011 1:32 AM, Spendius wrote:

    > Is there an easy way to perform these 2 steps (using Struts maybe) ?



    No not Struts, just use the standard API. Check out HttpURLConnection:

    http://download.oracle.com/javase/6/docs/api/java/net/HttpURLConnection.html

    I haven't actually done this myself, but I've seen others talk about it
    so I'm sure it's feasible. You'll probably need a few more classes
    besides HttpURLConnection.


    > I'd really like to be able to type something like:
    > C:\temp> java -cp %CLASSPATH% SwitchOffMyNAS
    > in a DOS window and have my external device to shut down...



    Also feasible. Java SE installs a file association for Jar files by
    default. On my own system, I can type <someJar.jar> into the Search box
    on the Start Menu (bottom left on the windows task bar) and the Jar will
    execute.

    You have to have the Jar file on the PATH (not class path). I just set
    up a single personal directory "C:\usr\bin" on my windows drive and copy
    Jars I want to execute into there.
    markspace, Jan 14, 2011
    #4
  5. Spendius

    Tarkin Guest

    On Jan 14, 11:19 am, markspace <> wrote:
    > On 1/14/2011 1:32 AM, Spendius wrote:
    >
    > > Is there an easy way to perform these 2 steps (using Struts maybe) ?

    >
    > No not Struts, just use the standard API.  Check out HttpURLConnection:
    >
    > http://download.oracle.com/javase/6/docs/api/java/net/HttpURLConnecti...
    >
    > I haven't actually done this myself, but I've seen others talk about it
    > so I'm sure it's feasible.  You'll probably need a few more classes
    > besides HttpURLConnection.
    >
    > > I'd really like to be able to type something like:
    > > C:\temp>  java -cp %CLASSPATH% SwitchOffMyNAS
    > > in a DOS window and have my external device to shut down...

    >
    > Also feasible.  Java SE installs a file association for Jar files by
    > default.  On my own system, I can type <someJar.jar> into the Search box
    > on the Start Menu (bottom left on the windows task bar) and the Jar will
    > execute.
    >
    > You have to have the Jar file on the PATH (not class path).  I just set
    > up a single personal directory "C:\usr\bin" on my windows drive and copy
    > Jars I want to execute into there.


    Be sure to check out Jetty 6 or 7, as both feature the HttpClient
    class-
    from the Jetty 6 docs @ Codehaus:
    http://docs.codehaus.org/display/JETTY/Jetty HTTP Client

    HTH,
    Tarkin
    Tarkin, Jan 14, 2011
    #5
  6. Spendius

    Roedy Green Guest

    On Fri, 14 Jan 2011 01:32:50 -0800 (PST), Spendius
    <> wrote, quoted or indirectly quoted someone
    who said :

    >Hi,
    >I have a small NAS device that I can switch off once I'm logged on
    >to it: I just have to open the management page using my favorite
    >browser, type my login/pwd, then click on a 'Shutdown' button and
    >that's it - no need to get back down to my basement to manually
    >switch the contraption off.
    >
    >=> I'd like write a small Java class that will
    >1 perform the http connection to this device web server,
    >2 then run/submit the 'shutdown' action to its small web server too...
    >
    >Is there an easy way to perform these 2 steps (using Struts maybe) ?
    >I'd really like to be able to type something like:
    >C:\temp> java -cp %CLASSPATH% SwitchOffMyNAS
    >in a DOS window and have my external device to shut down...


    This is pretty easy. I do write little browserless browser automators
    all the time using the HTTP class I wrote. See
    http://mindprod.com/products1.html#HTTP

    http://mindprod.com/products1.html#FILETRANSFER (Look at the Download
    class, it is 90% of the way to what you need.)

    for other examples, see
    http://mindprod.com/products1.html#AVAILABLE
    http://mindprod.com/products1.html#SUBMITTER
    http://mindprod.com/products1.html#BROKENLINKS
    http://mindprod.com/products1.html#VERCHECK


    Hint: use Wireshare http://mindprod.com/jgloss/wireshark.html
    to see just what your normal tool sends to the NAS (Network Attached
    Storage), then you can check that your automaton sends the same
    sequence.

    see http://mindprod.com/jgloss/http.html for background information
    about HTTP GET/POST and friends.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    To err is human, but to really foul things up requires a computer.
    ~ Farmer's Almanac
    It is breathtaking how a misplaced comma in a computer program can
    shred megabytes of data in seconds.
    Roedy Green, Jan 15, 2011
    #6
  7. Spendius

    Arne Vajhøj Guest

    On 14-01-2011 04:32, Spendius wrote:
    > I have a small NAS device that I can switch off once I'm logged on
    > to it: I just have to open the management page using my favorite
    > browser, type my login/pwd, then click on a 'Shutdown' button and
    > that's it - no need to get back down to my basement to manually
    > switch the contraption off.
    >
    > => I'd like write a small Java class that will
    > 1 perform the http connection to this device web server,
    > 2 then run/submit the 'shutdown' action to its small web server too...
    >
    > Is there an easy way to perform these 2 steps (using Struts maybe) ?
    > I'd really like to be able to type something like:
    > C:\temp> java -cp %CLASSPATH% SwitchOffMyNAS
    > in a DOS window and have my external device to shut down...
    >
    > Feasible ?


    Yes - use Jakarta Commons HttpClient - it has a convenient API
    for this.

    You could do it using the standard Java classes
    URLConnection and HttpURLConnection, but maintaining the
    login session is just extra work you save by using HttpClient.

    Arne
    Arne Vajhøj, Jan 15, 2011
    #7
  8. Spendius

    Arne Vajhøj Guest

    On 14-01-2011 13:06, Tarkin wrote:
    > On Jan 14, 11:19 am, markspace<> wrote:
    >> On 1/14/2011 1:32 AM, Spendius wrote:
    >>
    >>> Is there an easy way to perform these 2 steps (using Struts maybe) ?

    >>
    >> No not Struts, just use the standard API. Check out HttpURLConnection:
    >>
    >> http://download.oracle.com/javase/6/docs/api/java/net/HttpURLConnecti...
    >>
    >> I haven't actually done this myself, but I've seen others talk about it
    >> so I'm sure it's feasible. You'll probably need a few more classes
    >> besides HttpURLConnection.
    >>
    >>> I'd really like to be able to type something like:
    >>> C:\temp> java -cp %CLASSPATH% SwitchOffMyNAS
    >>> in a DOS window and have my external device to shut down...

    >>
    >> Also feasible. Java SE installs a file association for Jar files by
    >> default. On my own system, I can type<someJar.jar> into the Search box
    >> on the Start Menu (bottom left on the windows task bar) and the Jar will
    >> execute.
    >>
    >> You have to have the Jar file on the PATH (not class path). I just set
    >> up a single personal directory "C:\usr\bin" on my windows drive and copy
    >> Jars I want to execute into there.

    >
    > Be sure to check out Jetty 6 or 7, as both feature the HttpClient
    > class-
    > from the Jetty 6 docs @ Codehaus:
    > http://docs.codehaus.org/display/JETTY/Jetty HTTP Client


    A complete servlet engine to get a HTTP client may be
    a bit too much "extras".

    Arne
    Arne Vajhøj, Jan 15, 2011
    #8
  9. Spendius

    Arne Vajhøj Guest

    On 14-01-2011 04:32, Spendius wrote:
    > Is there an easy way to perform these 2 steps (using Struts maybe) ?


    Oh - and Struts is a server side web framework, which is not relevant
    for the client side problem you have.

    Arne
    Arne Vajhøj, Jan 15, 2011
    #9
  10. Spendius

    Spendius Guest

    Just would like to thank y'all.

    In fact grâce à Jean-Baptiste I've solved my issue
    with HtmlUnit within a few seconds, but I'll have
    a look on other suggestions, if only for the sake
    of my curiosity...

    Once again *thanks*.

    Spendius
    Spendius, Jan 17, 2011
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Grant Merwitz
    Replies:
    3
    Views:
    9,001
    Grant Merwitz
    Sep 15, 2005
  2. chandan
    Replies:
    0
    Views:
    293
    chandan
    Mar 18, 2008
  3. Jerry Manner
    Replies:
    6
    Views:
    1,241
    Jerry Manner
    Jun 22, 2008
  4. Replies:
    2
    Views:
    357
    Dave Anderson
    Oct 10, 2006
  5. Replies:
    3
    Views:
    648
Loading...

Share This Page