Solved

Quesion about HTTP Get Request

Posted on 2004-04-11
4
154 Views
Last Modified: 2010-03-31
I am having some difficulty sending an http get request to the server through a socket connection, downloading binary data located in a relative folder on the server and then writing it to a local file.

sock = new Socket(server, port);
in = new DataInputStream(sock.getInputStream());
out = new DataOutputStream(sock.getOutputStream());

String request = "GET /folder/folder/datafile HTTP 1.0\r\n\r\n";
//then i send the get request to the server
out.writeBytes(request);

The problem is that I when i go to read the response from the socket into a byte array and write the bytearray to the file; the data either is not written at all (filesize is 0) or in some cases the data is partially written.  Is there something I am doing wrong with the get request?  Any comments would be appreciated.  Thanks
0
Comment
Question by:aomega
4 Comments
 
LVL 14

Accepted Solution

by:
Tommy Braas earned 125 total points
ID: 10802276
I suggest that you use the URL class instead of rolling your own. See http://java.sun.com/j2se/1.4.2/docs/api/java/net/URL.html

URL url = new URL("http", server, port, "/folder/folder/datafile");
Object theContent = url.getContent();
0
 
LVL 3

Expert Comment

by:int_20h
ID: 10808843
You can use a library that deals with HTTP.  This one is very good!
http://www.innovation.ch/java/HTTPClient/
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now