java - 我可以使用 GWT HTTP 请求从网站读取文本文件吗?

标签 java gwt httprequest

我创建了一个GWT Starter Project我想使用 HTTP Request从另一个站点读取文本文件。

我将此代码放在入口点方法onModuleLoad()中:

String url = "http://www.textfiles.com/100/apples.txt";
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);

try {
    Request myrequest = builder.sendRequest(null, new RequestCallback() {

        public void onError(Request request, Throwable exception) {
        // Couldn't connect to server (could be
        // timeout, SOP violation, etc.)
        }
        public void onResponseReceived(Request request, Response response) {
            if (200 == response.getStatusCode()) {
                Label l = new Label();
                l.setText("Response: " + response.getText());
                RootPanel.get().add(l);
            }
            else {
                // Handle the error. Can get the status
                // text from response.getStatusText()
                Label l = new Label();
                l.setText("Error. getText() = " + response.getText() 
                    + " Status text = "
                    + response.getStatusText()
                    + ". Code: "
                    + response.getStatusCode());
                RootPanel.get().add(l);
            }
        }
    });
}
catch (RequestException e) {
    // Couldn't connect to server
}

错误 block 始终执行,并且 response.getStatusCode() 为 0。response.getText()response.getStatusText()返回空白文本。 GWT HTTP Request 可以用于读取文本文件吗?还是应该使用其他方法?

最佳答案

由于Same Origin Policy,您无法向不同的域发出 HTTP 请求。 .

关于java - 我可以使用 GWT HTTP 请求从网站读取文本文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18003695/

相关文章:

尝试访问 Request.Url 时,ASP.NET HttpModule 因 NullReference 异常而失败

java - Spring自定义 validator 注入(inject)NullPointerException

java - 使用 Maven 编译时遇到问题

javascript - J2EE 项目上的纯 javascript 框架可能吗?

在 Tomcat 上运行时在 Eclipse 中调试 GWT 客户端(多个 WAR)

javascript - CSRF/CSRF 安全 REST 调用的最佳实践?

java - Resteasy JAX-RS方法中获取请求

ajax - 如何在没有调试工具的情况下查看从浏览器发送的所有 HTTP Get 请求?

java - 在java中获取当前月份的第一个日期

java - 这 ? boolean 禅宗运算符