HttpConnection 类

定义

A wrapper for the Java SE class HttpsURLConnection. Used to avoid compatibility issues when testing with the mocking framework JMockit, as well as to avoid some undocumented side effects when using HttpsURLConnection.

The underlying HttpsURLConnection is transparently managed by Java. To reuse connections, for each time connect() is called, the input streams (input stream or error stream, if input stream is not accessible) must be completely read. Otherwise, the data remains in the stream and the connection will not be reusable.

public class HttpConnection
继承
java.lang.Object
HttpConnection

构造函数

HttpConnection()
HttpConnection(URL url, HttpMethod method)

Constructor. Opens a connection to the given URL.

HttpConnection(URL url, HttpMethod method, Proxy proxy)

Constructor. Opens a connection to the given URL.

字段

body

The body. HttpsURLConnection silently calls connect() when the output stream is written to. We buffer the body and defer writing to the output stream until connect() is called.

connection

The underlying HTTPS connection.

方法

connect()

Sends the request to the URL given in the constructor.

getResponseHeaders()

Returns the response headers as a Map, where the key is the header field name and the values are the values associated with the header field name.

getResponseStatus()

Returns the response status code.

readError()

Reads from the error stream and returns the error reason.

readInput()

Reads from the input stream (response stream) and returns the response.

readInputStream(InputStream stream)

Reads the input stream until the stream is empty.

setConnectTimeoutMillis(int timeout)

Sets the connect timeout in milliseconds. The connect timeout is the allowed amount of time for the http connection to be established.

setReadTimeoutMillis(int timeout)

Sets the read timeout in milliseconds. The read timeout is the number of milliseconds after the server receives a request and before the server sends data back.

setRequestHeader(String field, String value)

Sets the request header field to the given value.

setRequestMethod(HttpMethod method)

Sets the request method (i.e. POST).

writeOutput(byte[] body)

Saves the body to be sent with the request.

适用于