Package com.badlogic.gdx
Class Net.HttpRequest
- java.lang.Object
-
- com.badlogic.gdx.Net.HttpRequest
-
- All Implemented Interfaces:
Pool.Poolable
- Enclosing interface:
- Net
public static class Net.HttpRequest extends java.lang.Object implements Pool.Poolable
Contains getters and setters for the following parameters:- httpMethod: GET or POST are most common, can use
HttpMethodsfor static references - url: the url
- headers: a map of the headers, setter can be called multiple times
- timeout: time spent trying to connect before giving up
- content: A string containing the data to be used when processing the HTTP request.
Map
parameters = new HashMap (); parameters.put("user", "myuser"); HttpRequest httpGet = new HttpRequest(HttpMethods.Get); httpGet.setUrl("http://somewhere.net"); httpGet.setContent(HttpParametersUtils.convertHttpParameters(parameters)); ... Gdx.net.sendHttpRequest (httpGet, new HttpResponseListener() { public void handleHttpResponse(HttpResponse httpResponse) { status = httpResponse.getResultAsString(); //do stuff here based on response } public void failed(Throwable t) { status = "failed"; //do stuff here based on the failed attempt } });
-
-
Constructor Summary
Constructors Constructor Description HttpRequest()HttpRequest(java.lang.String httpMethod)Creates a new HTTP request with the specified HTTP method, seeNet.HttpMethods.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringgetContent()Returns the content string to be used for the HTTP request.longgetContentLength()Returns the content length in case content is a stream.java.io.InputStreamgetContentStream()Returns the content stream.booleangetFollowRedirects()Returns whether 301 and 302 redirects are followed.java.util.Map<java.lang.String,java.lang.String>getHeaders()Returns a Mapwith the headers of the HTTP request. booleangetIncludeCredentials()Returns whether a cross-origin request will include credentials.java.lang.StringgetMethod()Returns the HTTP method of the HttpRequest.intgetTimeOut()Returns the timeOut of the HTTP request.java.lang.StringgetUrl()Returns the URL of the HTTP request.voidreset()Resets the object for reuse.voidsetContent(java.io.InputStream contentStream, long contentLength)Sets the content as a stream to be used for a POST for example, to transmit custom data.voidsetContent(java.lang.String content)Sets the content to be used in the HTTP request.voidsetFollowRedirects(boolean followRedirects)Sets whether 301 and 302 redirects are followed.voidsetHeader(java.lang.String name, java.lang.String value)Sets a header to this HTTP request, seeHttpRequestHeader.voidsetIncludeCredentials(boolean includeCredentials)Sets whether a cross-origin request will include credentials.voidsetMethod(java.lang.String httpMethod)Sets the HTTP method of the HttpRequest.voidsetTimeOut(int timeOut)Sets the time to wait for the HTTP request to be processed, use 0 block until it is done.voidsetUrl(java.lang.String url)Sets the URL of the HTTP request.
-
-
-
Constructor Detail
-
HttpRequest
public HttpRequest()
-
HttpRequest
public HttpRequest(java.lang.String httpMethod)
Creates a new HTTP request with the specified HTTP method, seeNet.HttpMethods.- Parameters:
httpMethod- This is the HTTP method for the request, seeNet.HttpMethods
-
-
Method Detail
-
setUrl
public void setUrl(java.lang.String url)
Sets the URL of the HTTP request.- Parameters:
url- The URL to set.
-
setHeader
public void setHeader(java.lang.String name, java.lang.String value)Sets a header to this HTTP request, seeHttpRequestHeader.- Parameters:
name- the name of the header.value- the value of the header.
-
setContent
public void setContent(java.lang.String content)
Sets the content to be used in the HTTP request.- Parameters:
content- A string encoded in the corresponding Content-Encoding set in the headers, with the data to send with the HTTP request. For example, in case of HTTP GET, the content is used as the query string of the GET while on a HTTP POST it is used to send the POST data.
-
setContent
public void setContent(java.io.InputStream contentStream, long contentLength)Sets the content as a stream to be used for a POST for example, to transmit custom data.- Parameters:
contentStream- The stream with the content data.
-
setTimeOut
public void setTimeOut(int timeOut)
Sets the time to wait for the HTTP request to be processed, use 0 block until it is done. The timeout is used for both the timeout when establishing TCP connection, and the timeout until the first byte of data is received.- Parameters:
timeOut- the number of milliseconds to wait before giving up, 0 or negative to block until the operation is done
-
setFollowRedirects
public void setFollowRedirects(boolean followRedirects) throws java.lang.IllegalArgumentExceptionSets whether 301 and 302 redirects are followed. By default true. Can't be changed in the GWT backend because this uses XmlHttpRequests which always redirect.- Parameters:
followRedirects- whether to follow redirects.- Throws:
java.lang.IllegalArgumentException- if redirection is disabled on the GWT backend.
-
setIncludeCredentials
public void setIncludeCredentials(boolean includeCredentials)
Sets whether a cross-origin request will include credentials. Only used on GWT backend to allow cross-origin requests to include credentials such as cookies, authorization headers, etc...
-
setMethod
public void setMethod(java.lang.String httpMethod)
Sets the HTTP method of the HttpRequest.
-
getTimeOut
public int getTimeOut()
Returns the timeOut of the HTTP request.- Returns:
- the timeOut.
-
getMethod
public java.lang.String getMethod()
Returns the HTTP method of the HttpRequest.
-
getUrl
public java.lang.String getUrl()
Returns the URL of the HTTP request.
-
getContent
public java.lang.String getContent()
Returns the content string to be used for the HTTP request.
-
getContentStream
public java.io.InputStream getContentStream()
Returns the content stream.
-
getContentLength
public long getContentLength()
Returns the content length in case content is a stream.
-
getHeaders
public java.util.Map<java.lang.String,java.lang.String> getHeaders()
Returns a Mapwith the headers of the HTTP request.
-
getFollowRedirects
public boolean getFollowRedirects()
Returns whether 301 and 302 redirects are followed. By default true. Whether to follow redirects.
-
getIncludeCredentials
public boolean getIncludeCredentials()
Returns whether a cross-origin request will include credentials. By default false.
-
reset
public void reset()
Description copied from interface:Pool.PoolableResets the object for reuse. Object references should be nulled and fields may be set to default values.- Specified by:
resetin interfacePool.Poolable
-
-