See: Description
Interface | Description |
---|---|
RequestCallback |
The primary interface a caller must implement to receive a response to a
Request . |
Class | Description |
---|---|
Header |
Class for describing an HTTP header.
|
Request |
An HTTP request that is waiting for a response.
|
Request.RequestImpl |
Native implementation associated with
Request . |
Request.RequestImplIE8And9 |
Special
Request.RequestImpl for IE8, IE9 to work around some IE specialities. |
RequestBuilder |
Builder for constructing
Request objects. |
RequestBuilder.Method |
HTTP request method constants.
|
Response |
Wrapper which provides access to the components of an HTTP response.
|
ResponseImpl |
A
Response implementation based on a XMLHttpRequest . |
StringValidator |
Utility class for validating strings.
|
URL |
Utility class for the encoding and decoding URLs in their entirety or by
their individual components.
|
UrlBuilder |
Utility class to build a URL from components.
|
Exception | Description |
---|---|
RequestException |
RequestException is the superclass for the HTTP request related exceptions.
|
RequestPermissionException |
Exception thrown when the
RequestBuilder attempts to make a request
to a URL which violates the Same-Origin Security
Policy. |
RequestTimeoutException |
Thrown to indicate that an HTTP request has timed out.
|
Most applications will be interested in the Request
, RequestBuilder
,
RequestCallback
and Response
classes.
www.foo.com
cannot access
content from www.bar.com
. For more details please see, Same-Origin Security
Policy.
RequestBuilder.setTimeoutMillis(int)
.
com.google.gwt.http.HTTP
module.
<module> <!-- other inherited modules, such as com.google.gwt.user.User --> <inherits name="com.google.gwt.http.HTTP"/> <!-- additional module settings --> </module>
RequestCallback
instance should be written.
public class RequestCallbackExample implements RequestCallback { private static final int STATUS_CODE_OK = 200; public void onError(Request request, Throwable exception) { if (exception instanceof RequestTimeoutException) { // handle a request timeout } else { // handle other request errors } } public void onResponseReceived(Request request, Response response) { if (STATUS_CODE_OK == response.getStatusCode()) { // handle OK response from the server } else { // handle non-OK response from the server } } }
public class GetExample implements EntryPoint { public static final int STATUS_CODE_OK = 200; public static void doGet(String url) { RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url); try { Request response = builder.sendRequest(null, new RequestCallback() { public void onError(Request request, Throwable exception) { // Code omitted for clarity } public void onResponseReceived(Request request, Response response) { // Code omitted for clarity } }); } catch (RequestException e) { // Code omitted for clarity } } public void onModuleLoad() { doGet("/"); } }
public class PostExample { public static void doPost(String url, String postData) { RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url); try { builder.setHeader("Content-Type", "application/x-www-form-urlencoded"); Request response = builder.sendRequest(postData, new RequestCallback() { public void onError(Request request, Throwable exception) { // code omitted for clarity } public void onResponseReceived(Request request, Response response) { // code omitted for clarity } }); } catch (RequestException e) { Window.alert("Failed to send the request: " + e.getMessage()); } } public void onModuleLoad() { doPost("/", "Hello World!"); } }
public class TimeoutExample implements EntryPoint { public static void doGetWithTimeout(String url) { RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url); try { /* * wait 2000 milliseconds for the request to complete */ builder.setTimeoutMillis(2000); Request response = builder.sendRequest(null, new RequestCallback() { public void onError(Request request, Throwable exception) { if (exception instanceof RequestTimeoutException) { // handle a request timeout } else { // handle other request errors } } public void onResponseReceived(Request request, Response response) { // code omitted for clarity } }); } catch (RequestException e) { Window.alert("Failed to send the request: " + e.getMessage()); } } public void onModuleLoad() { doGetWithTimeout("/"); } }
public class QueryAndFormDataExample { public static interface Entry { String getName(); String getValue(); } public static String buildQueryString(Entry[] queryEntries) { StringBuffer sb = new StringBuffer(); for (int i = 0, n = queryEntries.length; i < n; ++i) { Entry queryEntry = queryEntries[i]; if (i > 0) { sb.append("&"); } // encode the characters in the name String encodedName = URL.encodeQueryString(queryEntry.getName()); sb.append(encodedName); sb.append("="); // encode the characters in the value String encodedValue = URL.encodeQueryString(queryEntry.getValue()); sb.append(encodedValue); } return sb.toString(); } }
RequestBuilder
send a request other than GET or POST?public class RequestBuilderForAnyHTTPMethodTypeExample extends RequestBuilder { /** * Constructor that allows a developer to override the HTTP method * restrictions imposed by the RequestBuilder class. Note if you override the * RequestBuilder's HTTP method restrictions in this manner, your application * may not work correctly on Safari browsers. * * @param httpMethod any non-null, non-empty string is considered valid * @param url any non-null, non-empty string is considered valid * * @throws IllegalArgumentException if httpMethod or url are empty * @throws NullPointerException if httpMethod or url are null */ public RequestBuilderForAnyHTTPMethodTypeExample(String httpMethod, String url) { super(httpMethod, url); } }