badjr
badjr

Reputation: 2286

Can I use GWT HTTP request to read a text file from a website?

I have created a GWT Starter Project and I wanted to use an HTTP Request to read a text file from another site.

I put this code in the entry point method 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
}

The error block is always executed and response.getStatusCode() is 0. response.getText() and response.getStatusText() return blank text. Can GWT HTTP Request be used to read text files, or should I use another method?

Upvotes: 2

Views: 622

Answers (1)

Marconius
Marconius

Reputation: 713

You cannot make HTTP requests to different domains because of the Same Origin Policy.

Upvotes: 3

Related Questions