我使用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();
}
}
还有我的处理程序类
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();
}
}
当我尝试使用 cmd 提示符和此语法进行 telnet 时,
远程登录 127.0.0.1 8000
我收到一条错误消息
HTTP/1.1 400 Bad Request
Connection to the host lost.
请让我知道我在这里犯的错误。万分感谢。