Simple server development (six) server intercepts the request URI

Start processing the client's request, first of all we need to know which clients can access a resource, how do we know which resources clients to access it?

The answer of course is to get URI requested by the client on the server side, the request URI in the HTTP request on the request line protocol. So we need to read the first line of data request protocol,

Then parse out the URI from these data to arrive at customers want to access which resources on the server side.

What (1) URL and and URI is? What is the relationship between them?

A, URL (Uniform Resource Locator) is a uniform resource locator, such as requesting a path on the browser address bar directly http://127.0.0.1:8080/oa/index.html is a URL, which may be positioned by the network in a resource.

B, URI (Uniform Resource Identifier) ​​is the uniform resource identifier, but on behalf of the name of a network resource, does not have the positioning function, or URI is part of the URL. For example, the URL above /oa/index.html is a URI.

The request line (2) HTTP protocol request protocol consists of three parts: the request method + URI + version of the agreement , we need to get the request URI is on line

(3) writing programs in the read request HandlerRequest.java rows
A, string parsing client requests intercepted the URI
B, acquiring request message BufferedReader

Br = the BufferedReader new new the BufferedReader ( new new the InputStreamReader (clientSocket.getInputStream ()));
 // Get requestURI:. String requestURI = br.readLine ( ) split ( "") [1]; 
printout to the console requestURI

(4) start httpserver, open the browser, enter the URL: http: //127.0.0.1: 8080 / oa / index.html access test

 

package com.zda.httpserver.core;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

import com.zda.httpserver.util.Logger;

/**
 * 处理客户端请求
 * @author zda
 * @version 1.0
 * @since    1.0
 */
public class HandlerRequest implements Runnable {
    public Socket clientSocket;
    public HandlerRequest(Socket clientSocket){
        this= .clientSocket the clientSocket,; 
    } 

    @Override 
    public  void RUN () {
         // a client request 
        the BufferedReader br = null ; 
        Logger.log ( "HTTPServer Thread:" + Thread.currentThread () getName ().);
         the try {
             // receiving the client message 
            br = new new the BufferedReader ( new new the InputStreamReader (clientSocket.getInputStream ()));
             // print client message 
            / * String TEMP = null; 
            the while (! (br.readLine TEMP = ()) = null) {  
                the System .out.println (temp);
            } * / 
            //Acquisition request protocol request line 
            String requestLine br.readLine = (); // the GET /oa/index.html the HTTP / 1.1
             // Get URI -> request line (requestLine) -> embodiment Request URI request protocol version -> three between who is connected through a space 
            String requestURI requestLine.split = ( "") [. 1]; // { "the GET", "/ OA / index.html", "the HTTP / 1.1"} 
            System.out.println (requestURI); 
            
            
        } the catch (Exception E) {
             // the TODO Auto-Generated Block the catch 
            e.printStackTrace (); 
        } the finally {
             // Close the resource 
            IF (br =! null ) {
                 the try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(clientSocket != null){
                try {
                    clientSocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
    }

}

 

 

 Export

 

 The above plurality of requests:

This is the browser's own mechanisms underlying many times to send a request to ensure that resources must be requested by the user to get.

Guess you like

Origin www.cnblogs.com/zhaideang/p/12302292.html