I am trying to learn about how a HTTP client works in Java. I am trying to build my own client that will make a request to a web server for a php file.

Currently when I make the request the server gives me the following error:

HTTP/1.1 400 Bad Request

However, I am able to access the file from within a browser no problem. I don't know what I could be doing wrong but I can't figure it out. Below is the code for my HTTP Client class:

public class MyHttpClient {

MyHttpRequest request;
String host;

public MyHttpResponse execute(MyHttpRequest request) throws IOException {

    //Creating the response object
    MyHttpResponse response = new MyHttpResponse();

    //Get web server host and port from request.
    String host = request.getHost();
    int port = request.getPort();

    //Check 1: HOST AND PORT NAME CORRECT!
    System.out.println("host: " + host + " port: " + String.valueOf(port));

    //Get resource path on web server from requests.
    String path = request.getPath();

    //Check 2: ENSURE PATH IS CORRECT!
    System.out.println("path: " + path);

    //Open connection to the web server
    Socket s = new Socket(host, port);

    //Get Socket input stream and wrap it in Buffered Reader so it can be read line by line.
    BufferedReader inFromServer = new BufferedReader(new InputStreamReader(s.getInputStream()));

    //Get Socket output stream and wrap it in a Buffered Writer so it can be written to line by line.
    PrintWriter outToServer = new PrintWriter(s.getOutputStream(),true);

    //Get request method
    String method = request.getMethod();

    //Check 3: ENSURE REQUEST IS CORRECT GET/POST!
    System.out.println("Method: " + method);

    //GET REQUEST
    if(method.equalsIgnoreCase("GET")){
        //Send request to server
        outToServer.println("GET " + path + " HTTP/1.1 " + "\r\n");
        String line = inFromServer.readLine();
        System.out.println("Line: " + line);
    }

    //Returning the response
    return response;
}

}

If anyone could shed some light on this issue I'd appreciate it very much! Thanks.

New Request To Server:

outToServer.print("GET " + path+ " HTTP/1.1" + "\r\n");
outToServer.print("Host: " + host + "\r\n");
outToServer.print("\r\n");

Response:

Method: GET

line: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
line: <html><head>
line: <title>400 Bad Request</title>
line: </head><body>
line: <h1>Bad Request</h1>
line: <p>Your browser sent a request that this server could not understand.<br />
line: </p>
line: <hr>
line: <address>Apache Server at default.secureserver.net Port 80</address>
line: </body></html>
line: null
有帮助吗?

解决方案

Do not use PrintWriter. You have to write ascii characters.

 s.getOutputStream().write(("GET " + path + " HTTP/1.1\r\n\r\n").getBytes("ASCII"));

其他提示

I think you need at least to add the Host header in the request.

Example taken from http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol

GET /index.html HTTP/1.1
Host: www.example.com

After the headers are complete you also need to transfer an extra \r\n so the server knows that the request is complete.

Do not use println but print. println adds another \n to every line causing the lines to be terminated with \r\n\n.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top