Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

sending raw http requests with java.net.socket

Thread view: 
yawnmoth - 14 Aug 2006 21:04 GMT
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...
Arne Vajhøj - 15 Aug 2006 00:32 GMT
> 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
Tom Cole - 15 Aug 2006 13:09 GMT
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

> > 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
[quoted text clipped - 7 lines]
>
> Arne
jvsoft.org@gmail.com - 15 Aug 2006 13:18 GMT
See Java socket tutorial

http://www.developerzone.biz/index.php?option=com_content&task=view&id=94&Itemid=36
yawnmoth - 21 Aug 2006 04:38 GMT
> 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
[quoted text clipped - 4 lines]
> 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?
EJP - 21 Aug 2006 05:26 GMT
> I'm familiar with HTTP/1.0 and 1.1.  I'm just not sure how to *send*
> data with java.net.Socket.

Socket.getOutputStream().write()
yawnmoth - 21 Aug 2006 15:30 GMT
> > I'm familiar with HTTP/1.0 and 1.1.  I'm just not sure how to *send*
> > data with java.net.Socket.
>
> 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.
Gordon Beaton - 21 Aug 2006 15:36 GMT
> 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

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

yawnmoth - 21 Aug 2006 15:40 GMT
> > I don't get any output when running it, however.
>
[quoted text clipped - 7 lines]
>     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...


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.