Class THttpClient


  • public class THttpClient
    extends TTransport
    HTTP implementation of the TTransport interface. Used for working with a Thrift web services implementation (using for example TServlet). This class offers two implementations of the HTTP transport. One uses HttpURLConnection instances, the other HttpClient from Apache Http Components. The chosen implementation depends on the constructor used to create the THttpClient instance. Using the THttpClient(String url) constructor or passing null as the HttpClient to THttpClient(String url, HttpClient client) will create an instance which will use HttpURLConnection. When using HttpClient, the following configuration leads to 5-15% better performance than the HttpURLConnection implementation: http.protocol.version=HttpVersion.HTTP_1_1 http.protocol.content-charset=UTF-8 http.protocol.expect-continue=false http.connection.stalecheck=false Also note that under high load, the HttpURLConnection implementation may exhaust the open file descriptor limit.
    See Also:
    THRIFT-970
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  THttpClient.Factory  
    • Constructor Summary

      Constructors 
      Constructor Description
      THttpClient​(java.lang.String url)  
      THttpClient​(java.lang.String url, org.apache.http.client.HttpClient client)  
    • Method Detail

      • setConnectTimeout

        public void setConnectTimeout​(int timeout)
      • setReadTimeout

        public void setReadTimeout​(int timeout)
      • setCustomHeaders

        public void setCustomHeaders​(java.util.Map<java.lang.String,​java.lang.String> headers)
      • setCustomHeader

        public void setCustomHeader​(java.lang.String key,
                                    java.lang.String value)
      • open

        public void open()
        Description copied from class: TTransport
        Opens the transport for reading/writing.
        Specified by:
        open in class TTransport
      • close

        public void close()
        Description copied from class: TTransport
        Closes the transport.
        Specified by:
        close in class TTransport
      • isOpen

        public boolean isOpen()
        Description copied from class: TTransport
        Queries whether the transport is open.
        Specified by:
        isOpen in class TTransport
        Returns:
        True if the transport is open.
      • read

        public int read​(byte[] buf,
                        int off,
                        int len)
                 throws TTransportException
        Description copied from class: TTransport
        Reads up to len bytes into buffer buf, starting at offset off.
        Specified by:
        read in class TTransport
        Parameters:
        buf - Array to read into
        off - Index to start reading at
        len - Maximum number of bytes to read
        Returns:
        The number of bytes actually read
        Throws:
        TTransportException - if there was an error reading data
      • write

        public void write​(byte[] buf,
                          int off,
                          int len)
        Description copied from class: TTransport
        Writes up to len bytes from the buffer.
        Specified by:
        write in class TTransport
        Parameters:
        buf - The output data buffer
        off - The offset to start writing from
        len - The number of bytes to write