Results 1 to 10 of 10
  1. #1
    emanuel is offline Member
    Join Date
    Sep 2014
    Posts
    7
    Rep Power
    0

    Default Download an object using url with the help of socket

    Hello ,

    I want to download objects (css , jpeg, html ,png ) using sockets in java without using httpurlconnection . How can i do this with the help of simple socket library ?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Download an object using url with the help of socket

    That can be done using the Socket class. You'll have to code your own HTTP handling code.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    emanuel is offline Member
    Join Date
    Sep 2014
    Posts
    7
    Rep Power
    0

    Default Re: Download an object using url with the help of socket

    Quote Originally Posted by Norm View Post
    That can be done using the Socket class. You'll have to code your own HTTP handling code.
    Hey Thanks for your reply . Can you please explain your approach with the help of an example ??

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Download an object using url with the help of socket

    Sorry, I don't often write sample code. Writing code to handle HTTP will not be trivial.
    The code would build a HTTP header and Get String, connect to the server with a Socket and send the header and Get. It would read and parse what was returned and as needed decode and save the returned file to disk.

    Is this an exercise to see how to do what the HttpURLConnection code does?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    emanuel is offline Member
    Join Date
    Sep 2014
    Posts
    7
    Rep Power
    0

    Default Re: Download an object using url with the help of socket

    Quote Originally Posted by Norm View Post
    Sorry, I don't often write sample code. Writing code to handle HTTP will not be trivial.
    The code would build a HTTP header and Get String, connect to the server with a Socket and send the header and Get. It would read and parse what was returned and as needed decode and save the returned file to disk.

    Is this an exercise to see how to do what the HttpURLConnection code does?

    Is this the right way to do it??

    Java Code:
                                   Socket socket = new Socket(servername, 3128);
                                   DataOutputStream bw = new DataOutputStream(
    						new DataOutputStream(socket.getOutputStream()));
    				bw.writeBytes("GET " + filename + " HTTP/1.1\n");
    				bw.writeBytes("Host: " + servername + ":80\n\n");
    
    				DataInputStream in = new DataInputStream(
    						socket.getInputStream());
    				String[] parts =  filename.split("/");
    				OutputStream dos = new FileOutputStream("/home/pratibha/networksassgn/" + servername + "/" + parts[parts.length-1]);
    				int count;
    				byte[] buffer = new byte[2048];
    				while ((count = in.read(buffer)) != -1) {
    					dos.write(buffer, 0, count);
    					dos.flush();
    				}
    				dos.close();
    Last edited by emanuel; 09-22-2014 at 02:55 PM.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Download an object using url with the help of socket

    What happens when you compile and execute it?
    There needs to be more code for the different possible responses.

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    emanuel is offline Member
    Join Date
    Sep 2014
    Posts
    7
    Rep Power
    0

    Default Re: Download an object using url with the help of socket

    Quote Originally Posted by Norm View Post
    What happens when you compile and execute it?
    There needs to be more code for the different possible responses.

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.


    I am getting this error when i run my code .. Please let me know how can i resolve the following error :

    java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(Abstrac tPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress( AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractP lainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.j ava:392)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:208)
    at Downloader.main(Downloader.java:41)

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Download an object using url with the help of socket

    Do you have the correct servername and port number for the site? Most HTTP servers use port 80.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    emanuel is offline Member
    Join Date
    Sep 2014
    Posts
    7
    Rep Power
    0

    Default Re: Download an object using url with the help of socket

    Quote Originally Posted by Norm View Post
    Do you have the correct servername and port number for the site? Most HTTP servers use port 80.

    Actually i am trying to download the objects using HAR (Http archive file ). So the server name is basically the host name in the HAR and the filename is the URL in the HAR file . And for internet we have praxy setting . So i am opening the server socket with the port 3128 .

    Java Code:
                                    String servername = (String) innObj.get("value");
    				String filename = (String) request.get("url");
    				Socket socket = new Socket(servername, 3128);
    				DataOutputStream bw = new DataOutputStream(
    						new DataOutputStream(socket.getOutputStream()));
    				bw.writeBytes("GET " + filename + " HTTP/1.1\n");
    				bw.writeBytes("Host: " + servername + ":80\n\n");

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Download an object using url with the help of socket

    Ok. I know nothing about the server you are using. I assumed you were working with a normal HTTP server on the internet (or localhost if testing locally).
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Problem in passing an object class through socket
    By alladin in forum New To Java
    Replies: 15
    Last Post: 02-14-2012, 11:25 AM
  2. How to send an object over a socket?
    By nousername in forum New To Java
    Replies: 3
    Last Post: 05-31-2011, 06:18 AM
  3. Sending object through socket
    By Alexandrinne in forum New To Java
    Replies: 0
    Last Post: 11-15-2010, 07:03 AM
  4. sending object through network/socket
    By skandalouz in forum Networking
    Replies: 1
    Last Post: 12-24-2009, 07:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •