Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GET request with Java sockets

I am writing a simple program to send a get request to a specific url "http://badunetworks.com/about/". The request works if I send it to "http://badunetworks.com" but I need to send it to the about page.

package badunetworks;
import java.io.*;
import java.net.*;

public class GetRequest {


    public static void main(String[] args) throws Exception {

        GetRequest getReq = new GetRequest();

        //Runs SendReq passing in the url and port from the command line
        getReq.SendReq("www.badunetworks.com/about/", 80);


    }

    public void SendReq(String url, int port) throws Exception {

        //Instantiate a new socket
        Socket s = new Socket("www.badunetworks.com/about/", port);

        //Instantiates a new PrintWriter passing in the sockets output stream
        PrintWriter wtr = new PrintWriter(s.getOutputStream());

        //Prints the request string to the output stream
        wtr.println("GET / HTTP/1.1");
        wtr.println("Host: www.badunetworks.com");
        wtr.println("");
        wtr.flush();

        //Creates a BufferedReader that contains the server response
        BufferedReader bufRead = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String outStr;

        //Prints each line of the response 
        while((outStr = bufRead.readLine()) != null){
            System.out.println(outStr);
        }


        //Closes out buffer and writer
        bufRead.close();
        wtr.close();

    }

}
like image 546
user3236794 Avatar asked Aug 19 '15 04:08

user3236794


People also ask

Does HTTP request use socket?

HTTP connection is a protocol that runs on a socket.

Is java socket TCP or UDP?

Yes, Socket and ServerSocket use TCP/IP. The package overview for the java.net package is explicit about this, but it's easy to overlook. UDP is handled by the DatagramSocket class.


3 Answers

if the about page link is about.html ,then you have change this line wtr.println("GET / HTTP/1.1") into wtr.println("GET /about.html HTTP/1.1").

in socket creation remove the /about

wtr.println("GET / HTTP/1.1");--->this line call the home page of the host you specified.

like image 161
RAJKUMAR NAGARETHINAM Avatar answered Oct 09 '22 01:10

RAJKUMAR NAGARETHINAM


When you're doing such a low-level access to a webserver, you should understand the 7 OSI layers. Socket is on layer 5 and HTTP on layer 7. That's also the reason, why java.net.Socket only accepts host names or InetAddr and no URLs. To do it with sockets, you have to implement the HTTP protocol properly, that is

  • Create a socket connection to the host and port, i.e. www.badunetworks.com and 80
  • Send a HTTP packet to the outputstream containing, method, resource by path and protocol version, i.e. GET /about/ HTTP/1.1
  • Read and interpret the response properly (headers and body)

But I wonder why you're doing it this complicated, there are a lot of alternatives to implementing low-level http clients yourself:

  • good old java.net.URL, as deprecated as its handling is, it's still one of the easiest way to read a resource, just call openStream() to read it
  • Apache HTTP Client is one of the most widely used http client implementation for java, which is quite easy to use and more flexible than the reading via URL
  • javax.ws.rs has a good builder api for creating web clients
like image 43
Gerald Mücke Avatar answered Oct 09 '22 00:10

Gerald Mücke


you need open Socket to url without path e.g.

Socket("www.badunetworks.com", port);

and after send command GET /{path} HTTP/1.1 e.g.

GET /about HTTP/1.1

... other header...

like image 32
Nightw0rk Avatar answered Oct 08 '22 23:10

Nightw0rk