sending raw http requests with java.net.socket

Discussion in 'Java' started by yawnmoth, Aug 14, 2006.

  1. yawnmoth

    yawnmoth Guest

    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...
     
    yawnmoth, Aug 14, 2006
    #1
    1. Advertising

  2. yawnmoth wrote:
    > 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
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Aug 15, 2006
    #2
    1. Advertising

  3. yawnmoth

    Tom Cole Guest

    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

    Arne Vajhøj wrote:
    > yawnmoth wrote:
    > > 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, Aug 15, 2006
    #3
  4. yawnmoth

    Guest

  5. yawnmoth

    yawnmoth Guest

    Tom Cole wrote:
    > 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?
     
    yawnmoth, Aug 21, 2006
    #5
  6. yawnmoth

    EJP Guest

    yawnmoth wrote:

    > 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()
     
    EJP, Aug 21, 2006
    #6
  7. yawnmoth

    yawnmoth Guest

    EJP wrote:
    > yawnmoth wrote:
    >
    > > 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.
     
    yawnmoth, Aug 21, 2006
    #7
  8. On 21 Aug 2006 07:30:06 -0700, yawnmoth wrote:
    > 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

    --
    [ 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
     
    Gordon Beaton, Aug 21, 2006
    #8
  9. yawnmoth

    yawnmoth Guest

    Gordon Beaton wrote:
    > On 21 Aug 2006 07:30:06 -0700, yawnmoth wrote:
    > > 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);
    > }

    That worked - thanks!

    And looking at my code... I see I should have been doing
    e.printStackTrace as well... ah well...
     
    yawnmoth, Aug 21, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fernando Arámburu

    web requests and mobile requests

    Fernando Arámburu, Apr 8, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    448
    Joerg Jooss
    Apr 8, 2005
  2. d c
    Replies:
    2
    Views:
    128
    Francis Cianfrocca
    Dec 24, 2007
  3. Dave Garcia
    Replies:
    5
    Views:
    600
    Dave Garcia
    Jun 5, 2009
  4. Yuchung Cheng

    Module of sending http 1.1 and/or 1.0 requests

    Yuchung Cheng, Jun 26, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    108
  5. yawnmoth

    sending http requests without cookies

    yawnmoth, Feb 14, 2006, in forum: Javascript
    Replies:
    13
    Views:
    254
    Michael Winter
    Feb 15, 2006
Loading...

Share This Page