Telnet到WebService - HTTP/1.1 400错误请求

LPD*_*LPD 3 java web-services

我使用时出错HTTPService.我写了一个示例代码,它看起来像这样:

import java.net.InetSocketAddress;

import com.kivar.lumina.services.handlers.LuminaSearchService;
import com.sun.net.httpserver.HttpServer;

public class LuminaWebService {

    public static void main( String[] args ) throws Exception {
        HttpServer server = HttpServer.create( new InetSocketAddress( 8000 ), 0 );
        server.createContext( "/luminaSearchService ", new LuminaSearchService() );
        server.start();
    }

}
Run Code Online (Sandbox Code Playgroud)

和我的处理程序类

import java.io.IOException;
import java.io.OutputStream;

import com.kivar.lumina.services.interfaces.SearchService;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

public class LuminaSearchService extends Thread implements SearchService, HttpHandler {

    @Override
    public void handle( HttpExchange arg0 ) throws IOException {
        setDaemon( true );
        String response = "This is the response";
        arg0.sendResponseHeaders( 200, response.length() );
        OutputStream os = arg0.getResponseBody();
        os.write( response.getBytes() );
        os.close();
    }

}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用cmd提示符和此语法进行telnet时,

telnet 127.0.0.1 8000

我说错了

HTTP/1.1 400 Bad Request
Connection to the host lost.
Run Code Online (Sandbox Code Playgroud)

请让我知道我在这里犯的错误.万分感谢.

Cod*_*odo 5

您的Web服务器似乎不再支持HTTP 1.0.对于HTTP 1.1,您必须指定版本号和主机名:

GET /luminaSearchService HTTP/1.1
Host: 127.0.0.1
Run Code Online (Sandbox Code Playgroud)

使用Windows命令窗口,您看不到正在键入的内容是正常的.

如果您将Web浏览器指向此URL会不会更容易,因为它是一个简单的GET请求?