Class 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 HttpMethods for 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.
    Abstracts the concept of a 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, see Net.HttpMethods.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String getContent()
      Returns the content string to be used for the HTTP request.
      long getContentLength()
      Returns the content length in case content is a stream.
      java.io.InputStream getContentStream()
      Returns the content stream.
      boolean getFollowRedirects()
      Returns whether 301 and 302 redirects are followed.
      java.util.Map<java.lang.String,​java.lang.String> getHeaders()
      Returns a Map with the headers of the HTTP request.
      boolean getIncludeCredentials()
      Returns whether a cross-origin request will include credentials.
      java.lang.String getMethod()
      Returns the HTTP method of the HttpRequest.
      int getTimeOut()
      Returns the timeOut of the HTTP request.
      java.lang.String getUrl()
      Returns the URL of the HTTP request.
      void reset()
      Resets the object for reuse.
      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.
      void setContent​(java.lang.String content)
      Sets the content to be used in the HTTP request.
      void setFollowRedirects​(boolean followRedirects)
      Sets whether 301 and 302 redirects are followed.
      void setHeader​(java.lang.String name, java.lang.String value)
      Sets a header to this HTTP request, see HttpRequestHeader.
      void setIncludeCredentials​(boolean includeCredentials)
      Sets whether a cross-origin request will include credentials.
      void setMethod​(java.lang.String httpMethod)
      Sets the HTTP method of the HttpRequest.
      void setTimeOut​(int timeOut)
      Sets the time to wait for the HTTP request to be processed, use 0 block until it is done.
      void setUrl​(java.lang.String url)
      Sets the URL of the HTTP request.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • HttpRequest

        public HttpRequest()
      • HttpRequest

        public HttpRequest​(java.lang.String httpMethod)
        Creates a new HTTP request with the specified HTTP method, see Net.HttpMethods.
        Parameters:
        httpMethod - This is the HTTP method for the request, see Net.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, see HttpRequestHeader.
        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.IllegalArgumentException
        Sets 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 Map with 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.Poolable
        Resets the object for reuse. Object references should be nulled and fields may be set to default values.
        Specified by:
        reset in interface Pool.Poolable