2

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?

2
  • Are you making a request to a different domain? Commented Aug 1, 2013 at 20:30
  • @Fedy2 Yes, I've tried other sites but get the same result. Commented Aug 1, 2013 at 20:50

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.