amir
amir

Reputation: 2573

get url address with network class

I have wrote a simple code that is a tinny web server . i want to show a picture when the client enter loalhost:8181/pic but when i String str = in.readLine() str show only localhost:8181 how i can find out the client in his browser enter the localhost:8181/pic ? there is my simple code :

    protected void start() {
    ServerSocket s;

    System.out.println("Webserver starting up on port 80");
    System.out.println("(press ctrl-c to exit)");
    try {
        // create the main server socket
        s = new ServerSocket(8181);
    } catch (Exception e) {
        System.out.println("Error: " + e);
        return;
    }

    System.out.println("Waiting for connection");
    for (;;) {
        try {
            Socket remote = s.accept();
            System.out.println("Connection, sending data.");
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    remote.getInputStream()));
            PrintWriter out = new PrintWriter(remote.getOutputStream());
            String method = in.readLine();
            out.flush();
            remote.close();
        } catch (Exception e) {
            System.out.println("Error: " + e);
        }
    }
}

Upvotes: 1

Views: 76

Answers (3)

user4319565
user4319565

Reputation:

you should take all parameters . not just two first line ;

    String line;
while ((line = in.readLine()) != null) {
    System.out.println(line);
}

Upvotes: 0

Remy Lebeau
Remy Lebeau

Reputation: 596216

You need something more like this:

protected void start() {
    ServerSocket s;

    System.out.println("Webserver starting up on port 8181");
    System.out.println("(press ctrl-c to exit)");
    try {
        // create the main server socket
        s = new ServerSocket(8181);
    } catch (Exception e) {
        System.out.println("Error: " + e);
        return;
    }

    for (;;) {
        System.out.println("Waiting for connection");
        try {
            Socket remote = s.accept();
            System.out.println("Connection established.");
            BufferedReader in = new BufferedReader(new InputStreamReader(remote.getInputStream()));
            PrintWriter out = new PrintWriter(remote.getOutputStream());

            String tokens[] = in.readLine().split("\\s+") ;
            if (tokens.length != 3)
            {
                out.println("HTTP/1.0 400 Bad Request");
                out.println("Connection: close");
                out.println("");
                out.flush();
                remote.close();
                continue;
            }

            if ((tokens[2].compareToIgnoreCase("HTTP/1.0") != 0) &&
                (tokens[2].compareToIgnoreCase("HTTP/1.1") != 0))
            {
                out.println("HTTP/1.0 505 HTTP Version Not Supported");
                out.println("Connection: close");
                out.println("");
                out.flush();
                remote.close();
                continue;
            }

            if (tokens[0].compareToIgnoreCase("GET") != 0)
            {
                out.println("HTTP/1.0 405 Method Not Allowed");
                out.println("Connection: close");
                out.println("");
                out.flush();
                remote.close();
                continue;
            }

            String path = tokens[1];
            String query = null;

            int idx = path.indexOf('?');
            if (idx != -1)
            {
                query = path.substring(idx+1);
                file = path.substring(0, idx);
            }

            if (path != "/pic")
            {
                out.println("HTTP/1.0 404 Not Found");
                out.println("Connection: close");
                out.println("");
                out.flush();
                remote.close();
                continue;
            }

            out.println("HTTP/1.0 200 OK");
            out.println("Connection: close");
            out.println("Content-Type: ..."); // for you to fill in
            out.println("Content-Length: ..."); // for you to fill in
            out.println();

            // write out image data here...

            out.flush();
            remote.close();
        } catch (Exception e) {
            System.out.println("Error: " + e);
        }
    }
}

Upvotes: 1

epoch
epoch

Reputation: 16605

You need to retrieve all the available input, not just the first line:

String line;
while ((line = in.readLine()) != null) {
    System.out.println(line);
}

This will give you something like this:

GET /pic?hello=true HTTP/1.1
Host: localhost:8181
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36
DNT: 1
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,af;q=0.6

Now, you can see what the browser is actually requesting:

it is asking the server to GET /pic?hello=true

with a bunch of other request headers.

Upvotes: 0

Related Questions