aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Http Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Http" Watch "Http" New topic
Author

Http

Richard West
Ranch Hand

Joined: Jan 07, 2005
Posts: 127
Hi everyone,

I am currently trying to do an application that does the upload as well the download of files from a http server that needs authentication.
The thing is i am not very sure where to begin and i am open to any suggestions.

Basically what i am trying to upload is a html file onto the http server and also have the capability to download back that file back to my computer if i need it. The server i am using does not support ftp so using the sun's ftp classes are out but i am not sure if there are classes that exist for the uploading and downloading of files from a http server.

I am not using servlets but trying to upload or download a simple html file to http server from a java application

Sample codings as well as informative links would be helpfull

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8971
    
    9

The Java Tutorial: Working with URLs
You are going to need some CGI or Servlet on the server side to handle the file. Jakarta Commons FileUpload can do the heavy lifting for that side. If your client is not a browser, you'll also need something to generate the multipart form for the submit, like Jakarta Commons HttpClient
[ July 13, 2005: Message edited by: Joe Ess ]

[How To Ask Questions On JavaRanch]
Richard West
Ranch Hand

Joined: Jan 07, 2005
Posts: 127
Hi everyone,

I am basically trying to upload a html file or download a html file to or from a http address. The thing is that i have been reading up on some http and it seems that i can use the URLConnection class to get the output or input stream from that http address but the thing is it does not say how do i authenticate(ie. username and password) before i can connect to that particular http server. Do you know how this can be achieved??

Thank for your links as they were extremely helpful

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8971
    
    9

Originally posted by West Richard:
Do you know how this can be achieved??


With the appropriately named
java.net.Authenticator
Richard West
Ranch Hand

Joined: Jan 07, 2005
Posts: 127
Hi everyone,

From what you are saying this is what i understand
say for example if my username w and password is w1
so to use the class this what i would have to do



My problem is now when do i know when i need to authenticate. I do not know if the class con which i have above which sets the default authenticator is correct before i try to connect to that URL and get its stream and would really appreciate some guidance from you.

Some sample codings would really be helpfull

Thank You

Your Sincerely

Richard West
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8971
    
    9

I do not know if the class con which i have above which sets the default authenticator is correct before i try to connect to that URL and get its stream

Change the lines

to this:

and you're set.
Have a look at this example. Not much different from what you have.
The method getPasswordAuthentication() gets invoked when URL tries to access the protected URL. Either you hard-code the username and password in the Authenticator or you prompt the user at that time for the values.
Richard West
Ranch Hand

Joined: Jan 07, 2005
Posts: 127
Hi everyone,

From what i get is that the Authenticator class takes care of all the authentication needs so i do not have to to worry about about encoding my user name and password with BASE64 alogrithom and pass it to the server as the Authenticator class takes care of all of this.

Am i right to assume this??

Richard West
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8971
    
    9

You are correct.
Richard West
Ranch Hand

Joined: Jan 07, 2005
Posts: 127
Hi everyone,

One more thing i can use the InputStream class to download the content from a http source. Say for example if the URL of that website is "http://www.pol.com/tell.zip"

By using the URLConnection class and connecting to that above address, can it actually download that particular zip file down back to my disk by using streams??

Another thing is if i were needed to connect to that URL via a proxy how could that be achieved??

Some example codings would really be helpfull for the proxy question

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8971
    
    9


By using the URLConnection class and connecting to that above address, can it actually download that particular zip file down back to my disk by using streams??


You must have missed the page Reading from and Writing to a URLConnection in the Java Tutorial link I posted earlier.


Another thing is if i were needed to connect to that URL via a proxy how could that be achieved??


Java 1.5 Deployment Guide: Proxy Configuration
Richard West
Ranch Hand

Joined: Jan 07, 2005
Posts: 127
Hi everyone,

The thing is that i am using 1.4.2 and can't really switch to 1.5 yet but hear me out for a while. If i were to set the http proxy for a particular website under 1.4.2 i did this



Is the way that i did the best way for versions under 1.5 and did i do it correctly??

Richard West
 
 
subject: Http