我创建了一个Web服务器,允许我的网站通过API访问我的游戏数据。我的Web服务器看起来像这样(我正在使用框架)me.apachenick
public class WebServer {
public static void initialize() {
ServerController server = new ServerController("192.168.0.21", 8080, 8443);
server.register(new APIVirtualHost());
server.inititialize();
}
}
我的APIVirtualHost看起来像这样
public class APIVirtualHost extends AbstractVirtualHost {
public static final String HOST_NAME = "XX.X.XX.XXX";
public APIVirtualHost() {
super(HOST_NAME);
setDirectoryListingEnabled(false);
}
}
显示在线玩家的示例路由如下所示
@RouteManifest(template="/online/players", method="GET")
public class APIHighscoresRoute extends RouteHttpHandler<JadePreprocessor> {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
Gson builder = new GsonBuilder().setPrettyPrinting().create();
JsonArray arr = new JsonArray();
World.getPlayers().forEach(o -> {
JsonObject obj = new JsonObject();
obj.addProperty("username", o.getUsername());
arr.add(obj);
});
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/html");
exchange.getResponseSender().send(builder.toJson(arr));
}
}
当我现在运行Web服务器()并从我的浏览器转到时 - 这工作正常!我能够看到世界上所有的球员。WebServer.initialize()XX.X.XX.XX:8080/online/players
但是,当我像这样从PHP发送cURL请求时
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://XX.X.XX.XXX:8080/online/players');
curl_setopt($ch, CURLOPT_PORT, 8088);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
var_dump($response);
echo curl_getinfo($ch) . '<br/>';
echo curl_errno($ch) . '<br/>';
echo curl_error($ch) . '<br/>';
curl_close($ch);
我收到此错误:
bool(false)
数组
7
无法连接到 XX.X.XX.XXX 端口 8088:连接被拒绝
这告诉我,在请求的资源上未启用跨源。如何在我的 Java Web 服务器上启用跨域?
胡子哥哥
红颜莎娜
相关分类