This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Other Open Source Projects and the fly likes Java program to Login a website using url and to download a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Java program to Login a website using url and to download a file" Watch "Java program to Login a website using url and to download a file" New topic
Author

Java program to Login a website using url and to download a file

Abhishekshri Shrivastava
Ranch Hand

Joined: Jan 13, 2011
Posts: 31
Hi,

Please help me to make a java program(basically i will be writing web apps) which will connect to an url, wil login to website and will download a spreadsheet from it.
Suggest me the feasibility and way to proceed.

Regards,
abhishek shrivastava
Shashank Ag
Ranch Hand

Joined: Dec 22, 2009
Posts: 88

Abhishekshri Shrivastava wrote:
Please help me to make a java program(basically i will be writing web apps) ......


Well it will always be two step process unless you implement SSO (single sign on).
Here I am showing non-sso, simple approach:

1. Open login page on web browser and open html source of it.
The login page must be containing a form having input fields of username & password.
Note the form's action="<url>" and field names of username & password fields.

2. Now download apache HttpClient library and create instance of PostMethod with above url.
Also add parameters with above name and correct values and call PostMethod.send()

This will login your application into website.
NOTE: Take care to collect all cookies from response.

Now, similarly you call the url to download file but this time take care to add cookies collected above to request.

Hope it helps.


SCJP 91%, SCWCD 97%
Abhishekshri Shrivastava
Ranch Hand

Joined: Jan 13, 2011
Posts: 31
can you please share the sample code or reference material url for the suggested way.
Shashank Ag
Ranch Hand

Joined: Dec 22, 2009
Posts: 88

can you please share the sample code or reference material url for the suggested way.


Here is a sample code:

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42055
    
  64
If this was my problem I'd use a higher-level library like jWebUnit that abstracts away a lot of the HTTP/HTML handling; it's easier to use and less code to write than if using HttpClient.


Ping & DNS - my free Android networking tools app
Abhishekshri Shrivastava
Ranch Hand

Joined: Jan 13, 2011
Posts: 31
Ulf Dittmer wrote:If this was my problem I'd use a higher-level library like jWebUnit that abstracts away a lot of the HTTP/HTML handling; it's easier to use and less code to write than if using HttpClient.


If you can please share the idea in details
Abhishekshri Shrivastava
Ranch Hand

Joined: Jan 13, 2011
Posts: 31
Shashank Ag wrote:
can you please share the sample code or reference material url for the suggested way.


Here is a sample code:



I am using "commons-httpclient-3.0.1.jar" for it. is it ok? i am getting
"java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.commons.httpclient.HttpClient.(HttpClient.java:65)
.........................................
......."

i have no idea what to do with "LogFactory" here
even i haven't used logfactory here(not making any logs).
please suggest me am i using correct jar
i am deploying it on weblogic 10.3

i also went through "org.apache.commons.httpclient.HttpClient" class from jar after decompling it.. but didn't get any proper idea
Shashank Ag
Ranch Hand

Joined: Dec 22, 2009
Posts: 88

Well will need to use commons-logging.jar too in your class path.
Also you may need commons-codec*.jar, use it if your program gives error.
Abhishekshri Shrivastava
Ranch Hand

Joined: Jan 13, 2011
Posts: 31
Shashank Ag wrote:Well will need to use commons-logging.jar too in your class path.

Also you may need commons-codec*.jar, use it if your program gives error.

How "commons-logging.jar " is used here
and what's the need of using "commons-codec*.jar"
please share me the link from which i can get all these jar
I am using jdk1.6
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42055
    
  64
Abhishekshri Shrivastava wrote:i also went through "org.apache.commons.httpclient.HttpClient" class from jar after decompling it.

HttpClient is open source - if for some reason you want to check its source code you can just download it; no need to decompile. I'm guessing what you need are the javadocs and the tutorials, though, both of which are are on the HttpClient web site.

please share me the link from which i can get all these jar

They're all somewhere on commons.apache.org. If you're not yet familiar with that site I suggest you spend some time looking what's available - it has some very useful and popular libraries! See http://hc.apache.org/httpclient-3.x/dependencies.html
Shashank Ag
Ranch Hand

Joined: Dec 22, 2009
Posts: 88

In simple terms HttpClient API is made using commons-logging and commons-codec api.
So, its dependent upon it and you need to have these in classpath while using HttpClient.

I am not attaching download links as you should be able to google it. Use codec 1.4 and logging 1.1 version.
Abhishekshri Shrivastava
Ranch Hand

Joined: Jan 13, 2011
Posts: 31
Shashank Ag wrote:In simple terms HttpClient API is made using commons-logging and commons-codec api.
So, its dependent upon it and you need to have these in classpath while using HttpClient.

I am not attaching download links as you should be able to google it. Use codec 1.4 and logging 1.1 version.


I don't get any exception after adding thos jar files. but the thing is that how to proceed after setting paramater values as:
client.getParams().setParameter("login", "xyz");
client.getParams().setParameter("password", "xyz");
and then is it required to load the page or tell me how i will proceed to the downloadble link on welcome page after successfull login to the webpage.

and i had one more doubts reltaed to jsp and servlet I think you can also help me on the topic. please follow the link below:
http://www.coderanch.com/t/523495/JSP/java/setting-HTML-element-value-JSP

http://www.coderanch.com/t/523497/Servlets/java/set-JSP-HTML-element-values

http://www.coderanch.com/t/523492/JSP/java/Getting-name-JSP-servlet
Shashank Ag
Ranch Hand

Joined: Dec 22, 2009
Posts: 88

Well to download once again using your web browser login to the site.

And upon the page where hyperlink to download the file is show stop there.
No right click on hyperlink and click copy link location (as it appears in my firefox) and paste it into notepad.

Now, in java program make a new HttpClient instance and call

Your file should be created.
Note mostly the websites use cookies to determine authentication.
Hence, I asked you to firstly fetch cookies.
Kavitha shan
Greenhorn

Joined: Aug 23, 2012
Posts: 2
Hi Everyone,

I am in need of a java code to login to a website.

I tried the above given code, but am getting 'CANNOT FIND SYMBOL' error in place of GetMethod()

GetMethod method = new GetMethod("http://localhost:8080/");

Please help me.

Regards,
Kavitha
 
GeeCON Prague 2014
 
subject: Java program to Login a website using url and to download a file