📄 protocol.java
字号:
/** * Get the Content-Encoding for the current response. * * @return encoding type of data to be transmitted after the * response headers */ public String getEncoding() { try { return getHeaderField("content-encoding"); } catch (IOException x) { return null; } } /** * Get the Expires header for the current response. * * @return expiration data for the transmitted data * * @exception IOException is thrown if a network error ocurrs */ public long getExpiration() throws IOException { return getHeaderFieldDate("expires", 0); } /** * Get the Date header for the current response. * * @return timestamp for the data transmission event * * @exception IOException is thrown if a network error ocurrs */ public long getDate() throws IOException { return getHeaderFieldDate("date", 0); } /** * Get the Last-Modified date header for the current response. * * @return timestamp for the transmitted data last modification * * @exception IOException is thrown if a network error ocurrs */ public long getLastModified() throws IOException { return getHeaderFieldDate("last-modified", 0); } /** * Get the named header field for the current response. * * @param name header field to be examined * @return value of requested header, if found, otherwise null * * @exception IOException is thrown if a network error ocurrs */ public String getHeaderField(String name) throws IOException { ensureOpen(); sendRequest(); return (headerFields.getProperty(name.toLowerCase())); } /** * Get the indexed header field for the current response. * * @param index header field offset to be examined * @return key name of requested header, if found, otherwise null * * @exception IOException is thrown if a network error ocurrs */ public String getHeaderField(int index) throws IOException { ensureOpen(); sendRequest(); if (index >= headerFields.size()) { return null; } return (headerFields.getValueAt(index)); } /** * Get the indexed header field value for the current response. * * @param index header field value offset to be examined * @return value of requested header, if found, otherwise null * * @exception IOException is thrown if a network error ocurrs */ public String getHeaderFieldKey(int index) throws IOException { ensureOpen(); sendRequest(); if (index >= headerFields.size()) return null; return ((String)(headerFields.getKeyAt(index))); } /** * Get the named header field for the current response and return a * numeric value for the parsed field, with a supplied default value * if the field does not exist or can not be parsed cleanly. * * @param name of the field to be examined * @param def default value to use, if field is not parsable * @return numeric value of requested header, if found, otherwise * supplied default is returned * * @exception IOException is thrown if a network error ocurrs */ public int getHeaderFieldInt(String name, int def) throws IOException { ensureOpen(); sendRequest(); try { return Integer.parseInt(getHeaderField(name)); } catch (IllegalArgumentException iae) { // fall through } catch (NullPointerException npe) { // fall through } return def; } /** * Get the named header field for the current response and return a date * value for the parsed field,with a supplied default value if the field * does not exist or can not be parsed cleanly. * * @param name of the field to be examined * @param def default value to use, if field is not parsable * @return date value of requested header, if found, otherwise * supplied default is returned * * @exception IOException is thrown if a network error ocurrs */ public long getHeaderFieldDate(String name, long def) throws IOException { ensureOpen(); sendRequest(); try { return DateParser.parse(getHeaderField(name)); } catch (NumberFormatException nfe) { // fall through } catch (IllegalArgumentException iae) { // fall through } catch (NullPointerException npe) { // fall through } return def; } /** * If not connected, connect to the underlying socket transport * and send the HTTP request and get the response header. * <P> * If an http_proxy was specified the socket connection will be made to * the proxy server and the requested URL will include the full http URL. * <P> * On output the Content-Length header is included in the request based * on the size of the buffered output array. * <P> * This routine inserts the Host header needed for HTTP 1.1 virtual host * addressing. * <P> * This routine also receives the reply response and parses the headers * for easier access. After the headers are parsed the application has * access to the raw data from the socket stream connection. * * @exception IOException is thrown if the connection cannot be opened */ protected void sendRequest() throws IOException { sendRequest(false, true); } /** * If not connected, connect to the underlying socket transport * and send the HTTP request and get the response header. * <P> * If an http_proxy was specified the socket connection will be made to * the proxy server and the requested URL will include the full http URL. * <P> * On output the Content-Length header is included in the request based * on the size of the buffered output array. * <P> * This routine inserts the Host header needed for HTTP 1.1 virtual host * addressing. * <P> * This routine also receives the reply response and parses the headers * for easier access. After the headers are parsed the application has * access to the raw data from the socket stream connection. * * @param chunkData if true chunk data sent to the server * @param readResponseHeader if true, read the response header * * @exception IOException is thrown if the connection cannot be opened */ private void sendRequest(boolean chunkData, boolean readResponseHeader) throws IOException { int bytesToRetry; if (sendingRequest || requestFinished) { return; } sendingRequest = true; try { if (chunkData) { chunkedOut = true; } bytesToRetry = bytesToWrite; try { startRequest(); sendRequestBody(); if (readResponseHeader) { finishRequestGetResponseHeader(); } } catch (IOException ioe) { if (!(streamConnection instanceof StreamConnectionElement)) { /* * This was a connection opened during this transaction. * So do not try to recover. */ throw ioe; } try { connectionPool.remove( (StreamConnectionElement)streamConnection); } catch (Exception e) { // do not over throw the previous exception } if (firstChunkSent) { // can't retry since we do not have the previous chunk throw new IOException("Persistent connection dropped " + "after first chunk sent, cannot retry"); } streamConnection = null; streamInput = null; streamOutput = null; bytesToWrite = bytesToRetry; startRequest(); sendRequestBody(); if (readResponseHeader) { finishRequestGetResponseHeader(); } } if (chunkedOut) { firstChunkSent = true; } } finally { sendingRequest = false; } } /** * If not connected, connect to the underlying socket transport * and send the HTTP request headers. * <P> * If an http_proxy was specified the socket connection will be made to * the proxy server and the requested URL will include the full http URL. * <P> * On output the Content-Length header is included in the request based * on the size of the buffered output array. * <P> * This routine inserts the Host header needed for HTTP 1.1 virtual host * addressing. * <P> * This routine also receives the reply response and parses the headers * for easier access. After the headers are parsed the application has * access to the raw data from the socket stream connection. * * @exception IOException is thrown if the connection cannot be opened */ private void startRequest() throws IOException { if (streamConnection != null) { return; } streamConnect(); sendRequestHeader(); } /** * Find a previous connection in the pool or try to connect to the * underlying stream transport. * * @exception IOException is thrown if the connection cannot be opened */ private void streamConnect() throws IOException { verifyPermissionCheck(); streamConnection = connectionPool.get(protocol, url.host, url.port); if (streamConnection == null) { streamConnection = connect(); } /* * Because StreamConnection.open*Stream cannot be called twice * the HTTP connect method may have already open the streams * to connect to the proxy and saved them in the field variables * already. */ if (streamOutput != null) { return; } streamOutput = streamConnection.openDataOutputStream(); streamInput = streamConnection.openDataInputStream(); } /** * Gets the underlying stream connection. * * @return underlying stream connection */ protected StreamConnection getStreamConnection() { return streamConnection; } /** * Simplifies the sendRequest() method header functionality into one method * this is extremely helpful for persistent connection support and * retries. * * @exception IOException is thrown if the connection cannot be opened */ private void sendRequestHeader() throws IOException { StringBuffer reqLine; String filename; int numberOfKeys; // HTTP 1.0 requests must contain content length for proxies if (getRequestProperty("Content-Length") == null) { setRequestField("Content-Length", Integer.toString(bytesToWrite)); } reqLine = new StringBuffer(256); /* * HTTP RFC and bug#4402149, * if there is no path then add a slash ("/"). */ filename = url.path; if (filename == null) { filename = "/"; } /* * Note: the "ref" or fragment, is not sent to the server. */ reqLine.append(method); reqLine.append(" "); /* * Since we now use a tunnel instead of a proxy, we do not have * to send an absolute URI. The difference is that a proxy will * strip scheme and authority from the URI and a tunnel will not. * * For HTTPS purposes we will use the relative URI. * * Some HTTPS server's do not like to see "https" as the scheme of an * URI and only recognize "http". * examples: www.wellsfargo.com sends back html with not HTTP headers, * e-banking.abbeynational.co.uk sends back a 404 to all requests. * * It is better to not use the absolute URL, than to hardcode the * the scheme to "http" all the time since that did not work with * e-banking.abbeynational.co.uk. * * if (http_proxy != null) { * reqLine.append(protocol); * reqLine.append("://"); * reqLine.append(url.authority); * } */ reqLine.append(filename); if (url.query != null) { reqLine.append("?"); reqLine.append(url.query); } reqLine.append(" ");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -