Solved

Quesion about HTTP Get Request

Posted on 2004-04-11
4
157 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 96
JList custom Cell Renderer refresh 15 56
what is a "java.lang.System Property"   ? 20 65
Java SE 8u111  Lot of stuff broke 11 69
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

775 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