GET request with Java sockets

孤人 提交于 2019-12-06 02:28:47

问题


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();

    }

}

回答1:


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.




回答2:


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...




回答3:


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


来源:https://stackoverflow.com/questions/32086560/get-request-with-java-sockets

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!