我编写了以下代码:
public static void handleRequest(HttpServerRequest request, Vertx vertx) throws FileNotFoundException {
if (request.method() == HttpMethod.GET) {
if (request.path().equals("/healthcheck")) {
returnResponse(request, "I'm alive!!!\n", true);
System.out.println("OK");
return;
}
...
}
returnResponse(request, "Not Valid Request", false);
System.out.println("This request cannot be handled");
}奇怪的是,一旦我收到带有路径"/healthcheck“的get请求,我就会同时获得控制台:
好的
和
无法处理此请求。
我希望只得到"OK",然后这个方法必须返回。你知道怎么让它发生吗?
发布于 2017-05-10 18:37:49
您可能收到多个请求,其中一个请求不是get请求。您能通过插入日志语句来监视服务器吗?
发布于 2017-05-10 20:58:02
最后,我发现我的浏览器确实发送了两个请求。第一个请求是GET localhost:8080/healthcheck,第二个请求是GET localhost:8080/ is。
GET localhost:8080/fa图标不满足条件,代码打印“此请求无法处理”。
发布于 2017-05-10 19:49:51
你正试图处理一个预先准备好的请求。
根据MDN
预置请求不像简单的请求(上面讨论过),“预置请求”请求首先向另一个域上的资源发送HTTP选项请求头,以确定实际请求是否安全发送。跨站点请求是这样预先设定的,因为它们可能会对用户数据产生影响。
试一试这个
public static void handleRequest(HttpServerRequest request, Vertx vertx) throws FileNotFoundException {
if(request.method() == HttpMethod.OPTIONS) {
return;
}
if (request.method() == HttpMethod.GET) {
if (request.path().equals("/healthcheck")) {
returnResponse(request, "I'm alive!!!\n", true);
System.out.println("OK");
return;
}
...
}
returnResponse(request, "Not Valid Request", false);
System.out.println("This request cannot be handled");
}https://stackoverflow.com/questions/43900252
复制相似问题