📄 httpconnection.html
字号:
<a name="475" href="#475">475</a> <a name="476" href="#476">476</a> <em>/**<em>*</em></em><a name="477" href="#477">477</a> <em> * Determines whether this connection is "stale", which is to say that either</em><a name="478" href="#478">478</a> <em> * it is no longer open, or an attempt to read the connection would fail.</em><a name="479" href="#479">479</a> <em> *</em><a name="480" href="#480">480</a> <em> * <p>Unfortunately, due to the limitations of the JREs prior to 1.4, it is</em><a name="481" href="#481">481</a> <em> * not possible to test a connection to see if both the read and write channels</em><a name="482" href="#482">482</a> <em> * are open - except by reading and writing. This leads to a difficulty when</em><a name="483" href="#483">483</a> <em> * some connections leave the "write" channel open, but close the read channel</em><a name="484" href="#484">484</a> <em> * and ignore the request. This function attempts to ameliorate that</em><a name="485" href="#485">485</a> <em> * problem by doing a test read, assuming that the caller will be doing a</em><a name="486" href="#486">486</a> <em> * write followed by a read, rather than the other way around.</em><a name="487" href="#487">487</a> <em> * </p></em><a name="488" href="#488">488</a> <em> *</em><a name="489" href="#489">489</a> <em> * <p>To avoid side-effects, the underlying connection is wrapped by a</em><a name="490" href="#490">490</a> <em> * {@link BufferedInputStream}, so although data might be read, what is visible</em><a name="491" href="#491">491</a> <em> * to clients of the connection will not change with this call.</p.</em><a name="492" href="#492">492</a> <em> *</em><a name="493" href="#493">493</a> <em> * @throws IOException if the stale connection test is interrupted.</em><a name="494" href="#494">494</a> <em> * </em><a name="495" href="#495">495</a> <em> * @return <tt>true</tt> if the connection is already closed, or a read would</em><a name="496" href="#496">496</a> <em> * fail.</em><a name="497" href="#497">497</a> <em> */</em><a name="498" href="#498">498</a> <strong>protected</strong> <strong>boolean</strong> isStale() throws IOException {<a name="499" href="#499">499</a> <strong>boolean</strong> isStale = <strong>true</strong>;<a name="500" href="#500">500</a> <strong>if</strong> (isOpen) {<a name="501" href="#501">501</a> <em class="comment">// the connection is open, but now we have to see if we can read it</em><a name="502" href="#502">502</a> <em class="comment">// assume the connection is not stale.</em><a name="503" href="#503">503</a> isStale = false;<a name="504" href="#504">504</a> <strong>try</strong> {<a name="505" href="#505">505</a> <strong>if</strong> (inputStream.available() <= 0) {<a name="506" href="#506">506</a> <strong>try</strong> {<a name="507" href="#507">507</a> socket.setSoTimeout(1);<a name="508" href="#508">508</a> inputStream.mark(1);<a name="509" href="#509">509</a> <strong>int</strong> byteRead = inputStream.read();<a name="510" href="#510">510</a> <strong>if</strong> (byteRead == -1) {<a name="511" href="#511">511</a> <em class="comment">// again - if the socket is reporting all data read,</em><a name="512" href="#512">512</a> <em class="comment">// probably stale</em><a name="513" href="#513">513</a> isStale = <strong>true</strong>;<a name="514" href="#514">514</a> } <strong>else</strong> {<a name="515" href="#515">515</a> inputStream.reset();<a name="516" href="#516">516</a> }<a name="517" href="#517">517</a> } <strong>finally</strong> {<a name="518" href="#518">518</a> socket.setSoTimeout(<strong>this</strong>.params.getSoTimeout());<a name="519" href="#519">519</a> }<a name="520" href="#520">520</a> }<a name="521" href="#521">521</a> } <strong>catch</strong> (InterruptedIOException e) {<a name="522" href="#522">522</a> <strong>if</strong> (!ExceptionUtil.isSocketTimeoutException(e)) {<a name="523" href="#523">523</a> <strong>throw</strong> e;<a name="524" href="#524">524</a> }<a name="525" href="#525">525</a> <em class="comment">// aha - the connection is NOT stale - continue on!</em><a name="526" href="#526">526</a> } <strong>catch</strong> (IOException e) {<a name="527" href="#527">527</a> <em class="comment">// oops - the connection is stale, the read or soTimeout failed.</em><a name="528" href="#528">528</a> LOG.debug(<a name="529" href="#529">529</a> <span class="string">"An error occurred while reading from the socket, is appears to be stale"</span>,<a name="530" href="#530">530</a> e<a name="531" href="#531">531</a> );<a name="532" href="#532">532</a> isStale = <strong>true</strong>;<a name="533" href="#533">533</a> }<a name="534" href="#534">534</a> }<a name="535" href="#535">535</a> <a name="536" href="#536">536</a> <strong>return</strong> isStale;<a name="537" href="#537">537</a> }<a name="538" href="#538">538</a> <a name="539" href="#539">539</a> <em>/**<em>*</em></em><a name="540" href="#540">540</a> <em> * Returns <tt>true</tt> if the connection is established via a proxy,</em><a name="541" href="#541">541</a> <em> * <tt>false</tt> otherwise.</em><a name="542" href="#542">542</a> <em> *</em><a name="543" href="#543">543</a> <em> * @return <tt>true</tt> if a proxy is used to establish the connection, </em><a name="544" href="#544">544</a> <em> * <tt>false</tt> otherwise.</em><a name="545" href="#545">545</a> <em> */</em><a name="546" href="#546">546</a> <strong>public</strong> <strong>boolean</strong> isProxied() {<a name="547" href="#547">547</a> <strong>return</strong> (!(<strong>null</strong> == proxyHostName || 0 >= proxyPortNumber));<a name="548" href="#548">548</a> }<a name="549" href="#549">549</a> <a name="550" href="#550">550</a> <em>/**<em>*</em></em><a name="551" href="#551">551</a> <em> * Set the state to keep track of the last response for the last request.</em><a name="552" href="#552">552</a> <em> *</em><a name="553" href="#553">553</a> <em> * <p>The connection managers use this to ensure that previous requests are</em><a name="554" href="#554">554</a> <em> * properly closed before a new request is attempted. That way, a GET</em><a name="555" href="#555">555</a> <em> * request need not be read in its entirety before a new request is issued.</em><a name="556" href="#556">556</a> <em> * Instead, this stream can be closed as appropriate.</p></em><a name="557" href="#557">557</a> <em> *</em><a name="558" href="#558">558</a> <em> * @param inStream The stream associated with an HttpMethod.</em><a name="559" href="#559">559</a> <em> */</em><a name="560" href="#560">560</a> <strong>public</strong> <strong>void</strong> setLastResponseInputStream(InputStream inStream) {<a name="561" href="#561">561</a> lastResponseInputStream = inStream;<a name="562" href="#562">562</a> }<a name="563" href="#563">563</a> <a name="564" href="#564">564</a> <em>/**<em>*</em></em><a name="565" href="#565">565</a> <em> * Returns the stream used to read the last response's body.</em><a name="566" href="#566">566</a> <em> *</em><a name="567" href="#567">567</a> <em> * <p>Clients will generally not need to call this function unless</em><a name="568" href="#568">568</a> <em> * using HttpConnection directly, instead of calling {@link HttpClient#executeMethod}.</em><a name="569" href="#569">569</a> <em> * For those clients, call this function, and if it returns a non-null stream,</em><a name="570" href="#570">570</a> <em> * close the stream before attempting to execute a method. Note that</em><a name="571" href="#571">571</a> <em> * calling "close" on the stream returned by this function <i>may</i> close</em><a name="572" href="#572">572</a> <em> * the connection if the previous response contained a "Connection: close" header. </p></em><a name="573" href="#573">573</a> <em> *</em><a name="574" href="#574">574</a> <em> * @return An {@link InputStream} corresponding to the body of the last</em><a name="575" href="#575">575</a> <em> * response.</em><a name="576" href="#576">576</a> <em> */</em><a name="577" href="#577">577</a> <strong>public</strong> InputStream getLastResponseInputStream() {<a name="578" href="#578">578</a> <strong>return</strong> lastResponseInputStream;<a name="579" href="#579">579</a> }<a name="580" href="#580">580</a> <a name="581" href="#581">581</a> <em class="comment">// --------------------------------------------------- Other Public Methods</em><a name="582" href="#582">582</a> <a name="583" href="#583">583</a> <em>/**<em>*</em></em><a name="584" href="#584">584</a> <em> * Returns {@link HttpConnectionParams HTTP protocol parameters} associated with this method.</em><a name="585" href="#585">585</a> <em> *</em><a name="586" href="#586">586</a> <em> * @return HTTP parameters.</em><a name="587" href="#587">587</a> <em> *</em><a name="588" href="#588">588</a> <em> * @since 3.0</em><a name="589" href="#589">589</a> <em> */</em><a name="590" href="#590">590</a> <strong>public</strong> HttpConnectionParams getParams() {<a name="591" href="#591">591</a> <strong>return</strong> <strong>this</strong>.params;<a name="592" href="#592">592</a> }<a name="593" href="#593">593</a> <a name="594" href="#594">594</a> <em>/**<em>*</em></em><a name="595" href="#595">595</a> <em> * Assigns {@link HttpConnectionParams HTTP protocol parameters} for this method.</em><a name="596" href="#596">596</a> <em> * </em><a name="597" href="#597">597</a> <em> * @since 3.0</em><a name="598" href="#598">598</a> <em> * </em><a name="599" href="#599">599</a> <em> * @see HttpConnectionParams</em><a name="600" href="#600">600</a> <em> */</em><a name="601" href="#601">601</a> <strong>public</strong> <strong>void</strong> setParams(<strong>final</strong> HttpConnectionParams params) {<a name="602" href="#602">602</a> <strong>if</strong> (params == <strong>null</strong>) {<a name="603" href="#603">603</a> <strong>throw</strong> <strong>new</strong> IllegalArgumentException(<span class="string">"Parameters may not be null"</span>);<a name="604" href="#604">604</a> }<a name="605" href="#605">605</a> <strong>this</strong>.params = params;<a name="606" href="#606">606</a> }<a name="607" href="#607">607</a> <a name="608" href="#608">608</a> <em>/**<em>*</em></em><a name="609" href="#609">609</a> <em> * Set the {@link Socket}'s timeout, via {@link Socket#setSoTimeout}. If the</em><a name="610" href="#610">610</a> <em> * connection is already open, the SO_TIMEOUT is changed. If no connection</em><a name="611" href="#611">611</a> <em> * is open, then subsequent connections will use the timeout value.</em><a name="612" href="#612">612</a> <em> * <p></em><a name="613" href="#613">613</a> <em> * Note: This is not a connection timeout but a timeout on network traffic!</em><a name="614" href="#614">614</a> <em> *</em><a name="615" href="#615">615</a> <em> * @param timeout the timeout value</em><a name="616" href="#616">616</a> <em> * @throws SocketException - if there is an error in the underlying</em><a name="617" href="#617">617</a> <em> * protocol, such as a TCP error.</em><a name="618" href="#618">618</a> <em> * </em><a name="619" href="#619">619</a> <em> * @deprecated Use {@link HttpConnectionParams#setSoTimeout(int)},</em><a name="620" href="#620">620</a> <em> * {@link HttpConnection#getParams()}.</em><a name="621" href="#621">621</a> <em> */</em><a name="622" href="#622">622</a> <strong>public</strong> <strong>void</strong> setSoTimeout(<strong>int</strong> timeout)<a name="623" href="#623">623</a> throws SocketException, IllegalStateException {<a name="624" href="#624">624</a> <strong>this</strong>.params.setSoTimeout(timeout);<a name="625" href="#625">625</a> <strong>if</strong> (<strong>this</strong>.socket != <strong>null</strong>) {<a name="626" href="#626">626</a> <strong>this</strong>.socket.setSoTimeout(timeout);<a name="627" href="#627">627</a> }<a name="628" href="#628">628</a> }<a name="629" href="#629">629</a> <a name="630" href="#630">630</a> <em>/**<em>*</em></em><a name="631" href="#631">631</a> <em> * Sets <code>SO_TIMEOUT</code> value directly on the underlying {@link Socket socket}. </em><a name="632" href="#632">632</a> <em> * This method does not change the default read timeout value set via </em><a name="633" href="#633">633</a> <em> * {@link HttpConnectionParams}.</em><a name="634" href="#634">634</a> <em> *</em><a name="635" href="#635">635</a> <em> * @param timeout the timeout value</em>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -