我在使用HTTPService
時出錯。我寫了一個示例代碼和它看起來像這樣:Telnet到WebService - HTTP/1.1 400錯誤請求
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();
}
}
當我嘗試使用命令提示符,這句法做遠程登錄,
telnet 127.0.0.1 8000
我收到一個錯誤,說
HTTP/1.1 400 Bad Request
Connection to the host lost.
請讓我知道我在這裏提交的錯誤。萬分感謝。
您是否在您的遠程登錄中發送任何HTTP標頭?或者甚至沒有等待輸入而立即失敗?如果第二個,它是一個bug ... – fge
其實我不知道最新的測試這個簡單的Web服務的正確方法是什麼。當我做'telnet 127.0.0.1 8000'時,我得到一個空白屏幕,但是當我嘗試鍵入'GET/luminaSearchService'(它甚至在CMD提示符下不可見)時,我得到那個錯誤 – LPD
您需要指定HTTP版本,即'GET/luminaSearchService HTTP/1.0'。然後打回兩次,即留空行表示請求結束。 – Rup