com.baidu.driver4j.bns.http
类 SimpleHttpRequestExecutor

java.lang.Object
  继承者 com.baidu.driver4j.bns.http.AbstractHttpRequestExecutor
      继承者 com.baidu.driver4j.bns.http.SimpleHttpRequestExecutor

public class SimpleHttpRequestExecutor
extends AbstractHttpRequestExecutor

SimpleHttpRequestExecutor implementation that uses standard J2SE facilities to execute POST requests, without support for HTTP authentication or advanced configuration options.

从以下版本开始:
1.0.0
作者:
xiemalin

字段摘要
 
从类 com.baidu.driver4j.bns.http.AbstractHttpRequestExecutor 继承的字段
CONTENT_TYPE_SERIALIZED_OBJECT, ENCODING_GZIP, HTTP_HEADER_ACCEPT_ENCODING, HTTP_HEADER_ACCEPT_LANGUAGE, HTTP_HEADER_CONTENT_ENCODING, HTTP_HEADER_CONTENT_LENGTH, HTTP_HEADER_CONTENT_TYPE, HTTP_METHOD_GET, HTTP_METHOD_POST
 
构造方法摘要
SimpleHttpRequestExecutor()
           
 
方法摘要
 String doExecuteGETRequest(String serviceUrl)
          Execute the given request through a standard J2SE HttpURLConnection.
 String doExecutePOSTRequest(String serviceUrl, String content)
          Execute the given request through a standard J2SE HttpURLConnection.
protected  boolean isGzipResponse(HttpURLConnection con)
          Determine whether the given response is a GZIP response.
protected  HttpURLConnection openConnection(String serviceUrl)
          Open an HttpURLConnection for the given remote invocation request.
protected  void prepareConnection(HttpURLConnection con, int contentLength, String method)
          Prepare the given HTTP connection.
protected  InputStream readResponseBody(HttpURLConnection con)
          Extract the response body from the given executed remote invocation request.
protected  void validateResponse(HttpURLConnection con)
          Validate the given response as contained in the HttpURLConnection object, throwing an exception if it does not correspond to a successful HTTP response.
protected  void writeRequestBody(HttpURLConnection con, ByteArrayOutputStream baos)
          Set the given serialized remote invocation as request body.
 
从类 com.baidu.driver4j.bns.http.AbstractHttpRequestExecutor 继承的方法
getConnectTimeout, getContentType, getReadTimeout, isAcceptGzipEncoding, setAcceptGzipEncoding, setConnectTimeout, setContentType, setReadTimeout
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

构造方法详细信息

SimpleHttpRequestExecutor

public SimpleHttpRequestExecutor()
方法详细信息

doExecuteGETRequest

public String doExecuteGETRequest(String serviceUrl)
                           throws IOException
Execute the given request through a standard J2SE HttpURLConnection.

This method implements the basic processing workflow: The actual work happens in this class's template methods.

参数:
invoker -
抛出:
IOException
另请参见:
openConnection(java.lang.String), prepareConnection(java.net.HttpURLConnection, int, java.lang.String), writeRequestBody(java.net.HttpURLConnection, java.io.ByteArrayOutputStream), validateResponse(java.net.HttpURLConnection), readResponseBody(java.net.HttpURLConnection)

doExecutePOSTRequest

public String doExecutePOSTRequest(String serviceUrl,
                                   String content)
                            throws IOException
Execute the given request through a standard J2SE HttpURLConnection.

This method implements the basic processing workflow: The actual work happens in this class's template methods.

参数:
invoker -
抛出:
IOException
另请参见:
openConnection(java.lang.String), prepareConnection(java.net.HttpURLConnection, int, java.lang.String), writeRequestBody(java.net.HttpURLConnection, java.io.ByteArrayOutputStream), validateResponse(java.net.HttpURLConnection), readResponseBody(java.net.HttpURLConnection)

openConnection

protected HttpURLConnection openConnection(String serviceUrl)
                                    throws IOException
Open an HttpURLConnection for the given remote invocation request.

参数:
config - the HTTP invoker configuration that specifies the target service
返回:
the HttpURLConnection for the given request
抛出:
IOException - if thrown by I/O methods
另请参见:
URL.openConnection()

prepareConnection

protected void prepareConnection(HttpURLConnection con,
                                 int contentLength,
                                 String method)
                          throws IOException
Prepare the given HTTP connection.

The default implementation specifies POST as method, "application/x-java-serialized-object" as "Content-Type" header, and the given content length as "Content-Length" header.

参数:
con - the HTTP connection to prepare
contentLength - the length of the content to send
抛出:
IOException - if thrown by HttpURLConnection methods
另请参见:
HttpURLConnection.setRequestMethod(java.lang.String), URLConnection.setRequestProperty(java.lang.String, java.lang.String)

isGzipResponse

protected boolean isGzipResponse(HttpURLConnection con)
Determine whether the given response is a GZIP response.

Default implementation checks whether the HTTP "Content-Encoding" header contains "gzip" (in any casing).

参数:
con - the HttpURLConnection to check

validateResponse

protected void validateResponse(HttpURLConnection con)
                         throws IOException
Validate the given response as contained in the HttpURLConnection object, throwing an exception if it does not correspond to a successful HTTP response.

Default implementation rejects any HTTP status code beyond 2xx, to avoid parsing the response body and trying to deserialize from a corrupted stream.

参数:
con - the HttpURLConnection to validate
抛出:
IOException - if validation failed
另请参见:
HttpURLConnection.getResponseCode()

writeRequestBody

protected void writeRequestBody(HttpURLConnection con,
                                ByteArrayOutputStream baos)
                         throws IOException
Set the given serialized remote invocation as request body.

The default implementation simply write the serialized invocation to the HttpURLConnection's OutputStream. This can be overridden, for example, to write a specific encoding and potentially set appropriate HTTP request headers.

参数:
con - the HttpURLConnection to write the request body to
baos - the ByteArrayOutputStream that contains the serialized RemoteInvocation object
抛出:
IOException - if thrown by I/O methods
另请参见:
URLConnection.getOutputStream(), URLConnection.setRequestProperty(java.lang.String, java.lang.String)

readResponseBody

protected InputStream readResponseBody(HttpURLConnection con)
                                throws IOException
Extract the response body from the given executed remote invocation request.

The default implementation simply reads the serialized invocation from the HttpURLConnection's InputStream. If the response is recognized as GZIP response, the InputStream will get wrapped in a GZIPInputStream.

参数:
con - the HttpURLConnection to read the response body from
返回:
an InputStream for the response body
抛出:
IOException - if thrown by I/O methods
另请参见:
isGzipResponse(java.net.HttpURLConnection), GZIPInputStream, URLConnection.getInputStream(), HttpURLConnection.getHeaderField(int), HttpURLConnection.getHeaderFieldKey(int)


Copyright © 2016. All rights reserved.