📄 httpurlconnection.java
字号:
private static boolean isRedirect(Response response) { int sc = response.getCode(); return (sc != 304 && (sc / 100) == 3); } /** * Returns a connection, from the pool if necessary. */ HTTPConnection getConnection(String host, int port, boolean secure) throws IOException { HTTPConnection connection; if (keepAlive) { Object key = HTTPConnection.getPoolKey(host, port, secure); synchronized (connectionPool) { connection = (HTTPConnection) connectionPool.remove(key); if (connection == null) { connection = new HTTPConnection(host, port, secure); connection.setPool(connectionPool); } } } else { connection = new HTTPConnection(host, port, secure); } return connection; } public void disconnect() { if (connection != null) { try { connection.close(); } catch (IOException e) { } } } public boolean usingProxy() { return (proxyHostname != null); } /** * Overrides the corresponding method in HttpURLConnection to permit * arbitrary methods, as long as they're valid ASCII alphabetic * characters. This is to permit WebDAV and other HTTP extensions to * function. * @param method the method */ public void setRequestMethod(String method) throws ProtocolException { if (connected) { throw new ProtocolException("Already connected"); } // Validate method = method.toUpperCase(); int len = method.length(); if (len == 0) { throw new ProtocolException("Empty method name"); } for (int i = 0; i < len; i++) { char c = method.charAt(i); if (c < 0x41 || c > 0x5a) { throw new ProtocolException("Illegal character '" + c + "' at index " + i); } } // OK this.method = method; requestMethodSetExplicitly = true; } public String getRequestProperty(String key) { return requestHeaders.getValue(key); } public Map getRequestProperties() { return requestHeaders; } public void setRequestProperty(String key, String value) { requestHeaders.put(key, value); } public void addRequestProperty(String key, String value) { String old = requestHeaders.getValue(key); if (old == null) { requestHeaders.put(key, value); } else { requestHeaders.put(key, old + "," + value); } } public OutputStream getOutputStream() throws IOException { if (connected) { throw new ProtocolException("Already connected"); } if (!doOutput) { throw new ProtocolException("doOutput is false"); } else if (!requestMethodSetExplicitly) { /* * Silently change the method to POST if no method was set * explicitly. This is due to broken applications depending on this * behaviour (Apache XMLRPC for one). */ method = "POST"; } if (requestSink == null) { requestSink = new ByteArrayOutputStream(); } return requestSink; } // -- Response -- public InputStream getInputStream() throws IOException { if (!connected) { connect(); } if (!doInput) { throw new ProtocolException("doInput is false"); } return responseSink; } public InputStream getErrorStream() { return errorSink; } public Map getHeaderFields() { if (!connected) { try { connect(); } catch (IOException e) { return null; } } Headers headers = response.getHeaders(); LinkedHashMap ret = new LinkedHashMap(); ret.put(null, Collections.singletonList(getStatusLine(response))); for (Iterator i = headers.entrySet().iterator(); i.hasNext(); ) { Map.Entry entry = (Map.Entry) i.next(); String key = (String) entry.getKey(); String value = (String) entry.getValue(); ret.put(key, Collections.singletonList(value)); } return Collections.unmodifiableMap(ret); } String getStatusLine(Response response) { return "HTTP/" + response.getMajorVersion() + "." + response.getMinorVersion() + " " + response.getCode() + " " + response.getMessage(); } public String getHeaderField(int index) { if (!connected) { try { connect(); } catch (IOException e) { return null; } } if (index == 0) { return getStatusLine(response); } Iterator i = response.getHeaders().entrySet().iterator(); Map.Entry entry; int count = 1; do { if (!i.hasNext()) { return null; } entry = (Map.Entry) i.next(); count++; } while (count <= index); return (String) entry.getValue(); } public String getHeaderFieldKey(int index) { if (!connected) { try { connect(); } catch (IOException e) { return null; } } if (index == 0) { return null; } Iterator i = response.getHeaders().entrySet().iterator(); Map.Entry entry; int count = 1; do { if (!i.hasNext()) { return null; } entry = (Map.Entry) i.next(); count++; } while (count <= index); return (String) entry.getKey(); } public String getHeaderField(String name) { if (!connected) { try { connect(); } catch (IOException e) { return null; } } return (String) response.getHeader(name); } public long getHeaderFieldDate(String name, long def) { if (!connected) { try { connect(); } catch (IOException e) { return def; } } Date date = response.getDateHeader(name); return (date == null) ? def : date.getTime(); } public String getContentType() { return getHeaderField("Content-Type"); } public int getResponseCode() throws IOException { if (!connected) { connect(); } return response.getCode(); } public String getResponseMessage() throws IOException { if (!connected) { connect(); } return response.getMessage(); } // -- HTTPS specific -- public String getCipherSuite() { if (!connected) { throw new IllegalStateException("not connected"); } return handshakeEvent.getCipherSuite(); } public Certificate[] getLocalCertificates() { if (!connected) { throw new IllegalStateException("not connected"); } return handshakeEvent.getLocalCertificates(); } public Certificate[] getServerCertificates() throws SSLPeerUnverifiedException { if (!connected) { throw new IllegalStateException("not connected"); } return handshakeEvent.getPeerCertificates(); } // HandshakeCompletedListener public void handshakeCompleted(HandshakeCompletedEvent event) { handshakeEvent = event; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -