📄 entityenclosingmethod.java
字号:
return super.getRequestCharSet(); } } /** * Sets length information about the request body. * * <p> * Note: If you specify a content length the request is unbuffered. This * prevents redirection and automatic retry if a request fails the first * time. This means that the HttpClient can not perform authorization * automatically but will throw an Exception. You will have to set the * necessary 'Authorization' or 'Proxy-Authorization' headers manually. * </p> * * @param length size in bytes or any of CONTENT_LENGTH_AUTO, * CONTENT_LENGTH_CHUNKED. If number of bytes or CONTENT_LENGTH_CHUNKED * is specified the content will not be buffered internally and the * Content-Length header of the request will be used. In this case * the user is responsible to supply the correct content length. * If CONTENT_LENGTH_AUTO is specified the request will be buffered * before it is sent over the network. * * @deprecated Use {@link #setContentChunked(boolean)} or * {@link #setRequestEntity(RequestEntity)} */ public void setRequestContentLength(long length) { LOG.trace("enter EntityEnclosingMethod.setRequestContentLength(int)"); this.requestContentLength = length; } /** * Sets whether or not the content should be chunked. * * @param chunked <code>true</code> if the content should be chunked * * @since 3.0 */ public void setContentChunked(boolean chunked) { this.chunked = chunked; } /** * Returns the length of the request body. * * @return number of bytes in the request body */ protected long getRequestContentLength() { LOG.trace("enter EntityEnclosingMethod.getRequestContentLength()"); if (!hasRequestContent()) { return 0; } if (this.chunked) { return -1; } if (this.requestEntity == null) { this.requestEntity = generateRequestEntity(); } return (this.requestEntity == null) ? 0 : this.requestEntity.getContentLength(); } /** * Populates the request headers map to with additional * {@link org.apache.commons.httpclient.Header headers} to be submitted to * the given {@link HttpConnection}. * * <p> * This implementation adds tt>Content-Length</tt> or <tt>Transfer-Encoding</tt> * headers. * </p> * * <p> * Subclasses may want to override this method to to add additional * headers, and may choose to invoke this implementation (via * <tt>super</tt>) to add the "standard" headers. * </p> * * @param state the {@link HttpState state} information associated with this method * @param conn the {@link HttpConnection connection} used to execute * this HTTP method * * @throws IOException if an I/O (transport) error occurs. Some transport exceptions * can be recovered from. * @throws HttpException if a protocol exception occurs. Usually protocol exceptions * cannot be recovered from. * * @see #writeRequestHeaders * * @since 3.0 */ protected void addRequestHeaders(HttpState state, HttpConnection conn) throws IOException, HttpException { LOG.trace("enter EntityEnclosingMethod.addRequestHeaders(HttpState, " + "HttpConnection)"); super.addRequestHeaders(state, conn); addContentLengthRequestHeader(state, conn); // only use the content type of the request entity if it has not already been // set manually if (getRequestHeader("Content-Type") == null) { RequestEntity requestEntity = getRequestEntity(); if (requestEntity != null && requestEntity.getContentType() != null) { setRequestHeader("Content-Type", requestEntity.getContentType()); } } } /** * Generates <tt>Content-Length</tt> or <tt>Transfer-Encoding: Chunked</tt> * request header, as long as no <tt>Content-Length</tt> request header * already exists. * * @param state current state of http requests * @param conn the connection to use for I/O * * @throws IOException when errors occur reading or writing to/from the * connection * @throws HttpException when a recoverable error occurs */ protected void addContentLengthRequestHeader(HttpState state, HttpConnection conn) throws IOException, HttpException { LOG.trace("enter EntityEnclosingMethod.addContentLengthRequestHeader(" + "HttpState, HttpConnection)"); if ((getRequestHeader("content-length") == null) && (getRequestHeader("Transfer-Encoding") == null)) { long len = getRequestContentLength(); if (len < 0) { if (getEffectiveVersion().greaterEquals(HttpVersion.HTTP_1_1)) { addRequestHeader("Transfer-Encoding", "chunked"); } else { throw new ProtocolException(getEffectiveVersion() + " does not support chunk encoding"); } } else { addRequestHeader("Content-Length", String.valueOf(len)); } } } /** * Sets the request body to be the specified inputstream. * * @param body Request body content as {@link java.io.InputStream} * * @deprecated use {@link #setRequestEntity(RequestEntity)} */ public void setRequestBody(InputStream body) { LOG.trace("enter EntityEnclosingMethod.setRequestBody(InputStream)"); clearRequestBody(); this.requestStream = body; } /** * Sets the request body to be the specified string. * The string will be submitted, using the encoding * specified in the Content-Type request header.<br> * Example: <code>setRequestHeader("Content-type", "text/xml; charset=UTF-8");</code><br> * Would use the UTF-8 encoding. * If no charset is specified, the * {@link org.apache.commons.httpclient.HttpConstants#DEFAULT_CONTENT_CHARSET default} * content encoding is used (ISO-8859-1). * * @param body Request body content as a string * * @deprecated use {@link #setRequestEntity(RequestEntity)} */ public void setRequestBody(String body) { LOG.trace("enter EntityEnclosingMethod.setRequestBody(String)"); clearRequestBody(); this.requestString = body; } /** * Writes the request body to the given {@link HttpConnection connection}. * * @param state the {@link HttpState state} information associated with this method * @param conn the {@link HttpConnection connection} used to execute * this HTTP method * * @return <tt>true</tt> * * @throws IOException if an I/O (transport) error occurs. Some transport exceptions * can be recovered from. * @throws HttpException if a protocol exception occurs. Usually protocol exceptions * cannot be recovered from. */ protected boolean writeRequestBody(HttpState state, HttpConnection conn) throws IOException, HttpException { LOG.trace( "enter EntityEnclosingMethod.writeRequestBody(HttpState, HttpConnection)"); if (!hasRequestContent()) { LOG.debug("Request body has not been specified"); return true; } if (this.requestEntity == null) { this.requestEntity = generateRequestEntity(); } if (requestEntity == null) { LOG.debug("Request body is empty"); return true; } long contentLength = getRequestContentLength(); if ((this.repeatCount > 0) && !requestEntity.isRepeatable()) { throw new ProtocolException( "Unbuffered entity enclosing request can not be repeated."); } this.repeatCount++; OutputStream outstream = conn.getRequestOutputStream(); if (contentLength < 0) { outstream = new ChunkedOutputStream(outstream); } requestEntity.writeRequest(outstream); // This is hardly the most elegant solution to closing chunked stream if (outstream instanceof ChunkedOutputStream) { ((ChunkedOutputStream) outstream).finish(); } outstream.flush(); LOG.debug("Request body sent"); return true; } /** * Recycles the HTTP method so that it can be used again. * Note that all of the instance variables will be reset * once this method has been called. This method will also * release the connection being used by this HTTP method. * * @see #releaseConnection() * * @deprecated no longer supported and will be removed in the future * version of HttpClient */ public void recycle() { LOG.trace("enter EntityEnclosingMethod.recycle()"); clearRequestBody(); this.requestContentLength = InputStreamRequestEntity.CONTENT_LENGTH_AUTO; this.repeatCount = 0; this.chunked = false; super.recycle(); } /** * @return Returns the requestEntity. * * @since 3.0 */ public RequestEntity getRequestEntity() { return generateRequestEntity(); } /** * @param requestEntity The requestEntity to set. * * @since 3.0 */ public void setRequestEntity(RequestEntity requestEntity) { clearRequestBody(); this.requestEntity = requestEntity; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -