0

I am trying to find out how to hit a website programmatically using the JDK 6 tools, but I'm unable to find examples of this. I need to be able to go to a website and enter the username and password in the corresponding fields.

The closest thing I was able to find was this example, but it is using .NET: http://blog.scosby.com/post/2010/01/19/Hitting-a-web-page-programmatically.aspx .

Through some research, I found that you might need to use the URL and URLConnection libraries to hit a website programmatically. But I would like to see an example of how to accomplish this, as I'm new to these libraries and don't know how to use them (also, I'm not 100% sure those are the libraries that I should use).

Thanks in advance.

user224567893
  • 606
  • 4
  • 17
  • 26
  • Can't you call directly the php or whatever page with the inputs informations? (i assume that when you click on login, some form is sending informations to somewhere, if you can see where you can call it directly) – Orelsanpls Jun 09 '14 at 00:04
  • 1
    Check out the [Apache HttpComponents](http://hc.apache.org/) library. It has classes that are comparable to the .NET code in your link. – ke4ktz Jun 09 '14 at 00:06
  • @ke4ktz I know about the Apache HttpComponents library, but I need to be able to do this using the JDK 6. – user224567893 Jun 09 '14 at 00:07
  • 3
    Check this link http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests?rq=1 – Nu2Overflow Jun 09 '14 at 00:08
  • @user98622 Apache HttpComponents is compatible with Java 1.6. – ke4ktz Jun 09 '14 at 00:11
  • 1
    I second @Nu2Overflow's link and suggest that this is probably a duplicate question to that: NB. You probably need to send a POST request to the url that the website send its form POST to (with the appropriate data). Note that if you do not have control of the website you may be scuppered by cross-site scripting protection, secret token cookies or any number of other challenges that have been put there to prevent anyone doing this - but you haven't specified what your use-case is in your question. – Matt Coubrough Jun 09 '14 at 00:56
  • This would likely be much easier with HttpComponents - why the artificial limitation of not using a library? – GreyBeardedGeek Jun 09 '14 at 01:39
  • @GreyBeardedGeek It is for exercising - I want to eventually do this using Apache HttpComponents, but would like to try this first using the JDK 1.6 tools. If you I could get some examples that would be very much appreciated – user224567893 Jun 09 '14 at 01:51
  • 3
    Ah, but then I would be exercising. It seems that it is you who needs the exercise :-) – GreyBeardedGeek Jun 09 '14 at 04:07

1 Answers1

1

Why not try using Selenium? And you can write code with Java, C# or Python

macking_liu
  • 46
  • 2
  • 8