所有类


java.net
类 HttpURLConnection

java.lang.Object
  继承者 java.net.URLConnection
      继承者 java.net.HttpURLConnection
直接已知子类:
HttpsURLConnection

public abstract class HttpURLConnection
   
   
   
   
   
extends URLConnection

支持 HTTP 特定功能的 URLConnection。有关详细信息,请参见这里

每个 HttpURLConnection 实例都可用于生成单个请求,但是其他实例可以透明地共享连接到 HTTP 服务器的基础网络。请求后在 HttpURLConnection 的 InputStream 或 OutputStream 上调用 close() 方法可以释放与此实例关联的网络资源,但对共享的持久连接没有任何影响。如果在调用 disconnect() 时持久连接空闲,则可能关闭基础套接字。

从以下版本开始:
JDK1.1
另请参见:
disconnect()

字段摘要
protected  intchunkLength
          使用存储块编码流模式进行输出时的存储块长度。
protected  intfixedContentLength
          使用固定长度流模式时的固定内容长度。
static intHTTP_ACCEPTED
          HTTP 状态码 202:Accepted。
static intHTTP_BAD_GATEWAY
          HTTP 状态码 502:Bad Gateway。
static intHTTP_BAD_METHOD
          HTTP 状态码 405:Method Not Allowed。
static intHTTP_BAD_REQUEST
          HTTP 状态码 400:Bad Request。
static intHTTP_CLIENT_TIMEOUT
          HTTP 状态码 408:Request Time-Out。
static intHTTP_CONFLICT
          HTTP 状态码 409:Conflict。
static intHTTP_CREATED
          HTTP 状态码 201:Created。
static intHTTP_ENTITY_TOO_LARGE
          HTTP 状态码 413:Request Entity Too Large。
static intHTTP_FORBIDDEN
          HTTP 状态码 403:Forbidden。
static intHTTP_GATEWAY_TIMEOUT
          HTTP 状态码 504:Gateway Timeout。
static intHTTP_GONE
          HTTP 状态码 410:Gone。
static intHTTP_INTERNAL_ERROR
          HTTP 状态码 500:Internal Server Error。
static intHTTP_LENGTH_REQUIRED
          HTTP 状态码 411:Length Required。
static intHTTP_MOVED_PERM
          HTTP 状态码 301:Moved Permanently。
static intHTTP_MOVED_TEMP
          HTTP 状态码 302:Temporary Redirect。
static intHTTP_MULT_CHOICE
          HTTP 状态码 300:Multiple Choices。
static intHTTP_NO_CONTENT
          HTTP 状态码 204:No Content。
static intHTTP_NOT_ACCEPTABLE
          HTTP 状态码 406:Not Acceptable。
static intHTTP_NOT_AUTHORITATIVE
          HTTP 状态码 203:Non-Authoritative Information。
static intHTTP_NOT_FOUND
          HTTP 状态码 404:Not Found。
static intHTTP_NOT_IMPLEMENTED
          HTTP 状态码 501:Not Implemented。
static intHTTP_NOT_MODIFIED
          HTTP 状态码 304:Not Modified。
static intHTTP_OK
          HTTP 状态码 200:OK。
static intHTTP_PARTIAL
          HTTP 状态码 206:Partial Content。
static intHTTP_PAYMENT_REQUIRED
          HTTP 状态码 402:Payment Required。
static intHTTP_PRECON_FAILED
          HTTP 状态码 412:Precondition Failed。
static intHTTP_PROXY_AUTH
          HTTP 状态码 407:Proxy Authentication Required。
static intHTTP_REQ_TOO_LONG
          HTTP 状态码 414:Request-URI Too Large。
static intHTTP_RESET
          HTTP 状态码 205:Reset Content。
static intHTTP_SEE_OTHER
          HTTP 状态码 303:See Other。
static intHTTP_SERVER_ERROR
          已过时。 放错了位置,它不应该存在。
static intHTTP_UNAUTHORIZED
          HTTP 状态码 401:Unauthorized。
static intHTTP_UNAVAILABLE
          HTTP 状态码 503:Service Unavailable。
static intHTTP_UNSUPPORTED_TYPE
          HTTP 状态码 415:Unsupported Media Type。
static intHTTP_USE_PROXY
          HTTP 状态码 305:Use Proxy。
static intHTTP_VERSION
          HTTP 状态码 505:HTTP Version Not Supported。
protected  booleaninstanceFollowRedirects
          如果为 true,则协议自动执行重定向。
protected  Stringmethod
          HTTP 方法(GET、POST、PUT 等)。
protected  intresponseCode
          表示三位字数的 HTTP 状态码 (Status-Code) 的 int
protected  StringresponseMessage
          HTTP 响应消息。
 
从类 java.net.URLConnection 继承的字段
allowUserInteraction, connected, doInput, doOutput, ifModifiedSince, url, useCaches
 
构造方法摘要
protected HttpURLConnection(URL u)
          HttpURLConnection 的构造方法。
 
方法摘要
abstract  voiddisconnect()
          指示近期服务器不太可能有其他请求。
 InputStreamgetErrorStream()
          如果连接失败但服务器仍然发送了有用数据,则返回错误流。
static booleangetFollowRedirects()
          返回指示是否应该自动执行 HTTP 重定向 (3xx) 的 boolean 值。
 StringgetHeaderField(int n)
          返回 nth 头字段的值。
 longgetHeaderFieldDate(String name, long Default)
          返回解析为日期的指定字段的值。
 StringgetHeaderFieldKey(int n)
          返回 nth 头字段的键。
 booleangetInstanceFollowRedirects()
          返回此 HttpURLConnectioninstanceFollowRedirects 字段的值。
 PermissiongetPermission()
          返回一个权限对象,其代表建立此对象表示的连接所需的权限。
 StringgetRequestMethod()
          获取请求方法。
 intgetResponseCode()
          从 HTTP 响应消息获取状态码。
 StringgetResponseMessage()
          获取与来自服务器的响应代码一起返回的 HTTP 响应消息(如果有)。
 voidsetChunkedStreamingMode(int chunklen)
          此方法用于在预先知道内容长度时启用没有进行内部缓冲的 HTTP 请求正文的流。
 voidsetFixedLengthStreamingMode(int contentLength)
          此方法用于在预先已知内容长度时启用没有进行内部缓冲的 HTTP 请求正文的流。
static voidsetFollowRedirects(boolean set)
          设置此类是否应该自动执行 HTTP 重定向(响应代码为 3xx 的请求)。
 voidsetInstanceFollowRedirects(boolean followRedirects)
          设置此 HttpURLConnection 实例是否应该自动执行 HTTP 重定向(响应代码为 3xx 的请求)。
 voidsetRequestMethod(String method)
          设置 URL 请求的方法, GET POST HEAD OPTIONS PUT DELETE TRACE 以上方法之一是合法的,具体取决于协议的限制。
abstract  booleanusingProxy()
          指示连接是否通过代理。
 
从类 java.net.URLConnection 继承的方法
addRequestProperty, connect, getAllowUserInteraction, getConnectTimeout, getContent, getContent, getContentEncoding, getContentLength, getContentType, getDate, getDefaultAllowUserInteraction, getDefaultRequestProperty, getDefaultUseCaches, getDoInput, getDoOutput, getExpiration, getFileNameMap, getHeaderField, getHeaderFieldInt, getHeaderFields, getIfModifiedSince, getInputStream, getLastModified, getOutputStream, getReadTimeout, getRequestProperties, getRequestProperty, getURL, getUseCaches, guessContentTypeFromName, guessContentTypeFromStream, setAllowUserInteraction, setConnectTimeout, setContentHandlerFactory, setDefaultAllowUserInteraction, setDefaultRequestProperty, setDefaultUseCaches, setDoInput, setDoOutput, setFileNameMap, setIfModifiedSince, setReadTimeout, setRequestProperty, setUseCaches, toString
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

字段详细信息

method

protected String method
HTTP 方法(GET、POST、PUT 等)。


chunkLength

protected int chunkLength
使用存储块编码流模式进行输出时的存储块长度。值 -1 表示对输出禁用存储块编码。


fixedContentLength

protected int fixedContentLength
使用固定长度流模式时的固定内容长度。值 -1 表示对输出禁用固定长度流模式。


responseCode

protected int responseCode
表示三位字数的 HTTP 状态码 (Status-Code) 的 int
  • 1xx:Informational
  • 2xx:Success
  • 3xx:Redirection
  • 4xx:Client Error
  • 5xx:Server Error


responseMessage

protected String responseMessage
HTTP 响应消息。


instanceFollowRedirects

protected boolean instanceFollowRedirects
如果为 true,则协议自动执行重定向。如果为 false,则协议不会自动执行重定向。

此字段由 setInstanceFollowRedirects 方法设置。其值由 getInstanceFollowRedirects 方法返回。

其默认值基于构造 HttpURLConnection 时静态 followRedirects 的值。

另请参见:
setInstanceFollowRedirects(boolean), getInstanceFollowRedirects(), setFollowRedirects(boolean)

HTTP_OK

public static final int HTTP_OK
HTTP 状态码 200:OK。

另请参见:
常量字段值