sending raw http requests with java.net.socket

Y

yawnmoth

I would like to be able to send HTTP requests without having to rely on
java.net.URL. Any ideas as to how I'd do this? I don't see any
function that'd allow me to send any sort of data on java.sun.com...
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

yawnmoth said:
I would like to be able to send HTTP requests without having to rely on
java.net.URL. Any ideas as to how I'd do this? I don't see any
function that'd allow me to send any sort of data on java.sun.com...

The Socket class can be used.

Connect to host port 80 and send

"GET /foobar.html HTTP/1.1\r\nHost: www.xxx.com\r\n\r\n"

Arne
 
T

Tom Cole

You can connect to anything using sockets. In the end that's how it's
going to get done anyway... The trick is reading the correct RFC so you
know the proper syntax for what you pump down the stream and read from
the stream.

The RFC for HTTP 1.1 happens to be 2616 and you can find a copy here;
http://www.faqs.org/rfcs/rfc2616.html

HTH
 
Y

yawnmoth

Tom said:
You can connect to anything using sockets. In the end that's how it's
going to get done anyway... The trick is reading the correct RFC so you
know the proper syntax for what you pump down the stream and read from
the stream.

The RFC for HTTP 1.1 happens to be 2616 and you can find a copy here;
http://www.faqs.org/rfcs/rfc2616.html
<snip>
I'm familiar with HTTP/1.0 and 1.1. I'm just not sure how to *send*
data with java.net.Socket. Looking at its documentation, I see three
functions that have send in their name - getSentBufferSize (which
doesn't send anything), sendUrgentData (which sends one byte), and
setSendBufferSize (again, doesn't send anything). So how do I send
"GET / HTTP/1.0\r\nHost: www.google.com"? Do I send it one byte at a
time or can I send the whole string?
 
Y

yawnmoth

EJP said:
Socket.getOutputStream().write()
That doesn't seem to be working as I'd expect it to. Here's my
program.

import java.net.*;
import java.io.*;

public class Test
{
public static void main(String[] args)
{
try
{
Socket sock = new Socket("www.google.com", 80);
sock.getOutputStream().write("GET / HTTP/1.0\r\nHost:
www.google.com\r\n\r\n".getBytes());
BufferedReader text = new BufferedReader(new
InputStreamReader(sock.getInputStream()));
while ( text.ready() )
{
System.out.println(text.readLine());
}
}
catch (Exception e)
{
}
}
}

I don't get any output when running it, however.
 
G

Gordon Beaton

I don't get any output when running it, however.

It may be necessary to flush the OutputStream.

It's almost certainly necessary to drop the call to ready().

Change your read loop to something like this:

while ((line = text.readLine()) != null) {
System.out.println(line);
}

/gordon
 
Y

yawnmoth

Gordon said:
It may be necessary to flush the OutputStream.

It's almost certainly necessary to drop the call to ready().

Change your read loop to something like this:

while ((line = text.readLine()) != null) {
System.out.println(line);
}
That worked - thanks!

And looking at my code... I see I should have been doing
e.printStackTrace as well... ah well...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top