📄 httpmethodbase.html
字号:
<a name="632" href="#632">632</a> <em> * returns <tt>-1</tt>.</em><a name="633" href="#633">633</a> <em> */</em><a name="634" href="#634">634</a> <strong>public</strong> <strong>long</strong> getResponseContentLength() {<a name="635" href="#635">635</a> Header[] headers = getResponseHeaderGroup().getHeaders(<span class="string">"Content-Length"</span>);<a name="636" href="#636">636</a> <strong>if</strong> (headers.length == 0) {<a name="637" href="#637">637</a> <strong>return</strong> -1;<a name="638" href="#638">638</a> }<a name="639" href="#639">639</a> <strong>if</strong> (headers.length > 1) {<a name="640" href="#640">640</a> LOG.warn(<span class="string">"Multiple content-length headers detected"</span>);<a name="641" href="#641">641</a> }<a name="642" href="#642">642</a> <strong>for</strong> (<strong>int</strong> i = headers.length - 1; i >= 0; i--) {<a name="643" href="#643">643</a> Header header = headers[i];<a name="644" href="#644">644</a> <strong>try</strong> {<a name="645" href="#645">645</a> <strong>return</strong> Long.parseLong(header.getValue());<a name="646" href="#646">646</a> } <strong>catch</strong> (NumberFormatException e) {<a name="647" href="#647">647</a> <strong>if</strong> (LOG.isWarnEnabled()) {<a name="648" href="#648">648</a> LOG.warn(<span class="string">"Invalid content-length value: "</span> + e.getMessage());<a name="649" href="#649">649</a> }<a name="650" href="#650">650</a> }<a name="651" href="#651">651</a> <em class="comment">// See if we can have better luck with another header, if present</em><a name="652" href="#652">652</a> }<a name="653" href="#653">653</a> <strong>return</strong> -1;<a name="654" href="#654">654</a> }<a name="655" href="#655">655</a> <a name="656" href="#656">656</a> <a name="657" href="#657">657</a> <em>/**<em>*</em></em><a name="658" href="#658">658</a> <em> * Returns the response body of the HTTP method, if any, as an array of bytes.</em><a name="659" href="#659">659</a> <em> * If response body is not available or cannot be read, returns <tt>null</tt></em><a name="660" href="#660">660</a> <em> * </em><a name="661" href="#661">661</a> <em> * Note: This will cause the entire response body to be buffered in memory. A</em><a name="662" href="#662">662</a> <em> * malicious server may easily exhaust all the VM memory. It is strongly</em><a name="663" href="#663">663</a> <em> * recommended, to use getResponseAsStream if the content length of the response</em><a name="664" href="#664">664</a> <em> * is unknown or resonably large.</em><a name="665" href="#665">665</a> <em> * </em><a name="666" href="#666">666</a> <em> * @return The response body.</em><a name="667" href="#667">667</a> <em> * </em><a name="668" href="#668">668</a> <em> * @throws IOException If an I/O (transport) problem occurs while obtaining the </em><a name="669" href="#669">669</a> <em> * response body.</em><a name="670" href="#670">670</a> <em> */</em><a name="671" href="#671">671</a> <strong>public</strong> byte[] getResponseBody() throws IOException {<a name="672" href="#672">672</a> <strong>if</strong> (<strong>this</strong>.responseBody == <strong>null</strong>) {<a name="673" href="#673">673</a> InputStream instream = getResponseBodyAsStream();<a name="674" href="#674">674</a> <strong>if</strong> (instream != <strong>null</strong>) {<a name="675" href="#675">675</a> <strong>long</strong> contentLength = getResponseContentLength();<a name="676" href="#676">676</a> <strong>if</strong> (contentLength > Integer.MAX_VALUE) { <em class="comment">//guard below cast from overflow</em><a name="677" href="#677">677</a> <strong>throw</strong> <strong>new</strong> IOException(<span class="string">"Content too large to be buffered: "</span>+ contentLength +<span class="string">" bytes"</span>);<a name="678" href="#678">678</a> }<a name="679" href="#679">679</a> <strong>int</strong> limit = getParams().getIntParameter(HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1024*1024);<a name="680" href="#680">680</a> <strong>if</strong> ((contentLength == -1) || (contentLength > limit)) {<a name="681" href="#681">681</a> LOG.warn(<span class="string">"Going to buffer response body of large or unknown size. "</span><a name="682" href="#682">682</a> +<span class="string">"Using getResponseAsStream instead is recommended."</span>);<a name="683" href="#683">683</a> }<a name="684" href="#684">684</a> LOG.debug(<span class="string">"Buffering response body"</span>);<a name="685" href="#685">685</a> ByteArrayOutputStream outstream = <strong>new</strong> ByteArrayOutputStream(<a name="686" href="#686">686</a> contentLength > 0 ? (<strong>int</strong>) contentLength : DEFAULT_INITIAL_BUFFER_SIZE);<a name="687" href="#687">687</a> byte[] buffer = <strong>new</strong> byte[4096];<a name="688" href="#688">688</a> <strong>int</strong> len;<a name="689" href="#689">689</a> <strong>while</strong> ((len = instream.read(buffer)) > 0) {<a name="690" href="#690">690</a> outstream.write(buffer, 0, len);<a name="691" href="#691">691</a> }<a name="692" href="#692">692</a> outstream.close();<a name="693" href="#693">693</a> setResponseStream(<strong>null</strong>);<a name="694" href="#694">694</a> <strong>this</strong>.responseBody = outstream.toByteArray();<a name="695" href="#695">695</a> }<a name="696" href="#696">696</a> }<a name="697" href="#697">697</a> <strong>return</strong> <strong>this</strong>.responseBody;<a name="698" href="#698">698</a> }<a name="699" href="#699">699</a> <a name="700" href="#700">700</a> <em>/**<em>*</em></em><a name="701" href="#701">701</a> <em> * Returns the response body of the HTTP method, if any, as an {@link InputStream}. </em><a name="702" href="#702">702</a> <em> * If response body is not available, returns <tt>null</tt></em><a name="703" href="#703">703</a> <em> * </em><a name="704" href="#704">704</a> <em> * @return The response body</em><a name="705" href="#705">705</a> <em> * </em><a name="706" href="#706">706</a> <em> * @throws IOException If an I/O (transport) problem occurs while obtaining the </em><a name="707" href="#707">707</a> <em> * response body.</em><a name="708" href="#708">708</a> <em> */</em><a name="709" href="#709">709</a> <strong>public</strong> InputStream getResponseBodyAsStream() throws IOException {<a name="710" href="#710">710</a> <strong>if</strong> (responseStream != <strong>null</strong>) {<a name="711" href="#711">711</a> <strong>return</strong> responseStream;<a name="712" href="#712">712</a> }<a name="713" href="#713">713</a> <strong>if</strong> (responseBody != <strong>null</strong>) {<a name="714" href="#714">714</a> InputStream byteResponseStream = <strong>new</strong> ByteArrayInputStream(responseBody);<a name="715" href="#715">715</a> LOG.debug(<span class="string">"re-creating response stream from byte array"</span>);<a name="716" href="#716">716</a> <strong>return</strong> byteResponseStream;<a name="717" href="#717">717</a> }<a name="718" href="#718">718</a> <strong>return</strong> <strong>null</strong>;<a name="719" href="#719">719</a> }<a name="720" href="#720">720</a> <a name="721" href="#721">721</a> <em>/**<em>*</em></em><a name="722" href="#722">722</a> <em> * Returns the response body of the HTTP method, if any, as a {@link String}. </em><a name="723" href="#723">723</a> <em> * If response body is not available or cannot be read, returns <tt>null</tt></em><a name="724" href="#724">724</a> <em> * The string conversion on the data is done using the character encoding specified</em><a name="725" href="#725">725</a> <em> * in <tt>Content-Type</tt> header.</em><a name="726" href="#726">726</a> <em> * </em><a name="727" href="#727">727</a> <em> * Note: This will cause the entire response body to be buffered in memory. A</em><a name="728" href="#728">728</a> <em> * malicious server may easily exhaust all the VM memory. It is strongly</em><a name="729" href="#729">729</a> <em> * recommended, to use getResponseAsStream if the content length of the response</em><a name="730" href="#730">730</a> <em> * is unknown or resonably large.</em><a name="731" href="#731">731</a> <em> * </em><a name="732" href="#732">732</a> <em> * @return The response body.</em><a name="733" href="#733">733</a> <em> * </em><a name="734" href="#734">734</a> <em> * @throws IOException If an I/O (transport) problem occurs while obtaining the </em><a name="735" href="#735">735</a> <em> * response body.</em><a name="736" href="#736">736</a> <em> */</em><a name="737" href="#737">737</a> <strong>public</strong> String getResponseBodyAsString() throws IOException {<a name="738" href="#738">738</a> byte[] rawdata = <strong>null</strong>;<a name="739" href="#739">739</a> <strong>if</strong> (responseAvailable()) {<a name="740" href="#740">740</a> rawdata = getResponseBody();<a name="741" href="#741">741</a> }<a name="742" href="#742">742</a> <strong>if</strong> (rawdata != <strong>null</strong>) {<a name="743" href="#743">743</a> <strong>return</strong> EncodingUtil.getString(rawdata, getResponseCharSet());<a name="744" href="#744">744</a> } <strong>else</strong> {<a name="745" href="#745">745</a> <strong>return</strong> <strong>null</strong>;<a name="746" href="#746">746</a> }<a name="747" href="#747">747</a> }<a name="748" href="#748">748</a> <a name="749" href="#749">749</a> <em>/**<em>*</em></em><a name="750" href="#750">750</a> <em> * Returns an array of the response footers that the HTTP method currently has</em><a name="751" href="#751">751</a> <em> * in the order in which they were read.</em><a name="752" href="#752">752</a> <em> *</em><a name="753" href="#753">753</a> <em> * @return an array of footers</em><a name="754" href="#754">754</a> <em> */</em><a name="755" href="#755">755</a> <strong>public</strong> Header[] getResponseFooters() {<a name="756" href="#756">756</a> <strong>return</strong> getResponseTrailerHeaderGroup().getAllHeaders();<a name="757" href="#757">757</a> }<a name="758" href="#758">758</a> <a name="759" href="#759">759</a> <em>/**<em>*</em></em><a name="760" href="#760">760</a> <em> * Gets the response footer associated with the given name.</em><a name="761" href="#761">761</a> <em> * Footer name matching is case insensitive.</em><a name="762" href="#762">762</a> <em> * <tt>null</tt> will be returned if either <i>footerName</i> is</em><a name="763" href="#763">763</a> <em> * <tt>null</tt> or there is no matching footer for <i>footerName</i></em><a name="764" href="#764">764</a> <em> * or there are no footers available. If there are multiple footers</em><a name="765" href="#765">765</a> <em> * with the same name, there values will be combined with the ',' separator</em><a name="766" href="#766">766</a> <em> * as specified by RFC2616.</em><a name="767" href="#767">767</a> <em> * </em><a name="768" href="#768">768</a> <em> * @param footerName the footer name to match</em><a name="769" href="#769">769</a> <em> * @return the matching footer</em><a name="770" href="#770">770</a> <em> */</em><a name="771" href="#771">771</a> <strong>public</strong> Header getResponseFooter(String footerName) {<a name="772" href="#772">772</a> <strong>if</strong> (footerName == <strong>null</strong>) {<a name="773" href="#773">773</a> <strong>return</strong> <strong>null</strong>;<a name="774" href="#774">774</a> } <strong>else</strong> {<a name="775" href="#775">775</a> <strong>return</strong> getResponseTrailerHeaderGroup().getCondensedHeader(footerName);<a name="776" href="#776">776</a> }<a name="777" href="#777">777</a> }<a name="778" href="#778">778</a> <a name="779" href="#779">779</a> <em>/**<em>*</em></em><a name="780" href="#780">780</a> <em> * Sets the response stream.</em><a name="781" href="#781">781</a> <em> * @param responseStream The new response stream.</em><a name="782" href="#782">782</a> <em> */</em><a name="783" href="#783">783</a> <strong>protected</strong> <strong>void</strong> setResponseStream(InputStream responseStream) {<a name="784" href="#784">784</a> <strong>this</strong>.responseStream = responseStream;<a name="785" href="#785">785</a> }<a name="786" href="#786">786</a> <a name="787" href="#787">787</a> <em>/**<em>*</em></em><a name="788" href="#788">788</a> <em> * Returns a str
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -