📄 httpconnection.html
字号:
<a name="636" href="#636">636</a> <em> * @throws SocketException - if there is an error in the underlying</em><a name="637" href="#637">637</a> <em> * protocol, such as a TCP error.</em><a name="638" href="#638">638</a> <em> * @throws IllegalStateException if not connected</em><a name="639" href="#639">639</a> <em> * </em><a name="640" href="#640">640</a> <em> * @since 3.0</em><a name="641" href="#641">641</a> <em> */</em><a name="642" href="#642">642</a> <strong>public</strong> <strong>void</strong> setSocketTimeout(<strong>int</strong> timeout)<a name="643" href="#643">643</a> throws SocketException, IllegalStateException {<a name="644" href="#644">644</a> assertOpen();<a name="645" href="#645">645</a> <strong>if</strong> (<strong>this</strong>.socket != <strong>null</strong>) {<a name="646" href="#646">646</a> <strong>this</strong>.socket.setSoTimeout(timeout);<a name="647" href="#647">647</a> }<a name="648" href="#648">648</a> }<a name="649" href="#649">649</a> <a name="650" href="#650">650</a> <em>/**<em>*</em></em><a name="651" href="#651">651</a> <em> * Returns the {@link Socket}'s timeout, via {@link Socket#getSoTimeout}, if the</em><a name="652" href="#652">652</a> <em> * connection is already open. If no connection is open, return the value subsequent </em><a name="653" href="#653">653</a> <em> * connection will use.</em><a name="654" href="#654">654</a> <em> * <p></em><a name="655" href="#655">655</a> <em> * Note: This is not a connection timeout but a timeout on network traffic!</em><a name="656" href="#656">656</a> <em> *</em><a name="657" href="#657">657</a> <em> * @return the timeout value</em><a name="658" href="#658">658</a> <em> * </em><a name="659" href="#659">659</a> <em> * @deprecated Use {@link HttpConnectionParams#getSoTimeout()},</em><a name="660" href="#660">660</a> <em> * {@link HttpConnection#getParams()}.</em><a name="661" href="#661">661</a> <em> */</em><a name="662" href="#662">662</a> <strong>public</strong> <strong>int</strong> getSoTimeout() throws SocketException {<a name="663" href="#663">663</a> <strong>return</strong> <strong>this</strong>.params.getSoTimeout();<a name="664" href="#664">664</a> }<a name="665" href="#665">665</a> <a name="666" href="#666">666</a> <em>/**<em>*</em></em><a name="667" href="#667">667</a> <em> * Sets the connection timeout. This is the maximum time that may be spent</em><a name="668" href="#668">668</a> <em> * until a connection is established. The connection will fail after this</em><a name="669" href="#669">669</a> <em> * amount of time.</em><a name="670" href="#670">670</a> <em> * @param timeout The timeout in milliseconds. 0 means timeout is not used.</em><a name="671" href="#671">671</a> <em> * </em><a name="672" href="#672">672</a> <em> * @deprecated Use {@link HttpConnectionParams#setConnectionTimeout(int)},</em><a name="673" href="#673">673</a> <em> * {@link HttpConnection#getParams()}.</em><a name="674" href="#674">674</a> <em> */</em><a name="675" href="#675">675</a> <strong>public</strong> <strong>void</strong> setConnectionTimeout(<strong>int</strong> timeout) {<a name="676" href="#676">676</a> <strong>this</strong>.params.setConnectionTimeout(timeout);<a name="677" href="#677">677</a> }<a name="678" href="#678">678</a> <a name="679" href="#679">679</a> <em>/**<em>*</em></em><a name="680" href="#680">680</a> <em> * Establishes a connection to the specified host and port</em><a name="681" href="#681">681</a> <em> * (via a proxy if specified).</em><a name="682" href="#682">682</a> <em> * The underlying socket is created from the {@link ProtocolSocketFactory}.</em><a name="683" href="#683">683</a> <em> *</em><a name="684" href="#684">684</a> <em> * @throws IOException if an attempt to establish the connection results in an</em><a name="685" href="#685">685</a> <em> * I/O error.</em><a name="686" href="#686">686</a> <em> */</em><a name="687" href="#687">687</a> <strong>public</strong> <strong>void</strong> open() throws IOException {<a name="688" href="#688">688</a> LOG.trace(<span class="string">"enter HttpConnection.open()"</span>);<a name="689" href="#689">689</a> <a name="690" href="#690">690</a> <strong>final</strong> String host = (proxyHostName == <strong>null</strong>) ? hostName : proxyHostName;<a name="691" href="#691">691</a> <strong>final</strong> <strong>int</strong> port = (proxyHostName == <strong>null</strong>) ? portNumber : proxyPortNumber;<a name="692" href="#692">692</a> assertNotOpen();<a name="693" href="#693">693</a> <a name="694" href="#694">694</a> <strong>if</strong> (LOG.isDebugEnabled()) {<a name="695" href="#695">695</a> LOG.debug(<span class="string">"Open connection to "</span> + host + <span class="string">":"</span> + port);<a name="696" href="#696">696</a> }<a name="697" href="#697">697</a> <a name="698" href="#698">698</a> <strong>try</strong> {<a name="699" href="#699">699</a> <strong>if</strong> (<strong>this</strong>.socket == <strong>null</strong>) {<a name="700" href="#700">700</a> usingSecureSocket = isSecure() && !isProxied();<a name="701" href="#701">701</a> <em class="comment">// use the protocol's socket factory unless this is a secure</em><a name="702" href="#702">702</a> <em class="comment">// proxied connection</em><a name="703" href="#703">703</a> <strong>final</strong> ProtocolSocketFactory socketFactory =<a name="704" href="#704">704</a> (isSecure() && isProxied()<a name="705" href="#705">705</a> ? <strong>new</strong> DefaultProtocolSocketFactory()<a name="706" href="#706">706</a> : protocolInUse.getSocketFactory());<a name="707" href="#707">707</a> <strong>this</strong>.socket = socketFactory.createSocket(<a name="708" href="#708">708</a> host, port, <a name="709" href="#709">709</a> localAddress, 0,<a name="710" href="#710">710</a> <strong>this</strong>.params);<a name="711" href="#711">711</a> }<a name="712" href="#712">712</a> <a name="713" href="#713">713</a> <em class="comment">/*</em><a name="714" href="#714">714</a> <em class="comment"> "Nagling has been broadly implemented across networks, </em><a name="715" href="#715">715</a> <em class="comment"> including the Internet, and is generally performed by default </em><a name="716" href="#716">716</a> <em class="comment"> - although it is sometimes considered to be undesirable in </em><a name="717" href="#717">717</a> <em class="comment"> highly interactive environments, such as some client/server </em><a name="718" href="#718">718</a> <em class="comment"> situations. In such cases, nagling may be turned off through </em><a name="719" href="#719">719</a> <em class="comment"> use of the TCP_NODELAY sockets option." */</em><a name="720" href="#720">720</a> <a name="721" href="#721">721</a> socket.setTcpNoDelay(<strong>this</strong>.params.getTcpNoDelay());<a name="722" href="#722">722</a> socket.setSoTimeout(<strong>this</strong>.params.getSoTimeout());<a name="723" href="#723">723</a> <a name="724" href="#724">724</a> <strong>int</strong> linger = <strong>this</strong>.params.getLinger();<a name="725" href="#725">725</a> <strong>if</strong> (linger >= 0) {<a name="726" href="#726">726</a> socket.setSoLinger(linger > 0, linger);<a name="727" href="#727">727</a> }<a name="728" href="#728">728</a> <a name="729" href="#729">729</a> <strong>int</strong> sndBufSize = <strong>this</strong>.params.getSendBufferSize();<a name="730" href="#730">730</a> <strong>if</strong> (sndBufSize >= 0) {<a name="731" href="#731">731</a> socket.setSendBufferSize(sndBufSize);<a name="732" href="#732">732</a> } <a name="733" href="#733">733</a> <strong>int</strong> rcvBufSize = <strong>this</strong>.params.getReceiveBufferSize();<a name="734" href="#734">734</a> <strong>if</strong> (rcvBufSize >= 0) {<a name="735" href="#735">735</a> socket.setReceiveBufferSize(rcvBufSize);<a name="736" href="#736">736</a> } <a name="737" href="#737">737</a> <strong>int</strong> outbuffersize = socket.getSendBufferSize();<a name="738" href="#738">738</a> <strong>if</strong> ((outbuffersize > 2048) || (outbuffersize <= 0)) {<a name="739" href="#739">739</a> outbuffersize = 2048;<a name="740" href="#740">740</a> }<a name="741" href="#741">741</a> <strong>int</strong> inbuffersize = socket.getReceiveBufferSize();<a name="742" href="#742">742</a> <strong>if</strong> ((inbuffersize > 2048) || (inbuffersize <= 0)) {<a name="743" href="#743">743</a> inbuffersize = 2048;<a name="744" href="#744">744</a> }<a name="745" href="#745">745</a> <a name="746" href="#746">746</a> <em class="comment">// START HERITRIX Change</em><a name="747" href="#747">747</a> <a href="../../../../org/archive/util/HttpRecorder.html">HttpRecorder</a> httpRecorder = HttpRecorder.getHttpRecorder();<a name="748" href="#748">748</a> <strong>if</strong> (httpRecorder == <strong>null</strong> || (isSecure() && isProxied())) {<a name="749" href="#749">749</a> <em class="comment">// no recorder, OR defer recording for pre-tunnel leg</em><a name="750" href="#750">750</a> inputStream = <strong>new</strong> BufferedInputStream(<a name="751" href="#751">751</a> socket.getInputStream(), inbuffersize);<a name="752" href="#752">752</a> outputStream = <strong>new</strong> BufferedOutputStream(<a name="753" href="#753">753</a> socket.getOutputStream(), outbuffersize);<a name="754" href="#754">754</a> } <strong>else</strong> {<a name="755" href="#755">755</a> inputStream = httpRecorder.inputWrap((InputStream)<a name="756" href="#756">756</a> (<strong>new</strong> BufferedInputStream(socket.getInputStream(),<a name="757" href="#757">757</a> inbuffersize)));<a name="758" href="#758">758</a> outputStream = httpRecorder.outputWrap((OutputStream)<a name="759" href="#759">759</a> (<strong>new</strong> BufferedOutputStream(socket.getOutputStream(), <a name="760" href="#760">760</a> outbuffersize)));<a name="761" href="#761">761</a> }<a name="762" href="#762">762</a> <em class="comment">// END HERITRIX change.</em><a name="763" href="#763">763</a> <a name="764" href="#764">764</a> isOpen = <strong>true</strong>;<a name="765" href="#765">765</a> } <strong>catch</strong> (IOException e) {<a name="766" href="#766">766</a> <em class="comment">// Connection wasn't opened properly</em><a name="767" href="#767">767</a> <em class="comment">// so close everything out</em><a name="768" href="#768">768</a> closeSocketAndStreams();<a name="769" href="#769">769</a> <strong>throw</strong> e;<a name="770" href="#770">770</a> }<a name="771" href="#771">771</a> }<a name="772" href="#772">772</a> <a name="773" href="#773">773</a> <em>/**<em>*</em></em><a name="774" href="#774">774</a> <em> * Instructs the proxy to establish a secure tunnel to the host. The socket will </em><a name="775" href="#775">775</a> <em> * be switched to the secure socket. Subsequent communication is done via the secure </em><a name="776" href="#776">776</a> <em> * socket. The method can only be called once on a proxied secure connection.</em><a name="777" href="#777">777</a> <em> *</em><a name="778" href="#778">778</a> <em> * @throws IllegalStateException if connection is not secure and proxied or</em><a name="779" href="#779">779</a> <em> * if the socket is already secure.</em><a name="780" href="#780">780</a> <em> * @throws IOException if an attempt to establish the secure tunnel results in an</em><a name="781" href="#781">781</a> <em> * I/O error.</em><a name="782" href="#782">782</a> <em> */</em><a name="783" href="#783">783</a> <strong>public</strong> <strong>void</strong> tunnelCreated() throws IllegalStateException, IOException {<a name="784" href="#784">784</a> LOG.trace(<span class="string">"enter HttpConnection.tunnelCreated()"</span>);<a name="785" href="#785">785</a> <a name="786" href="#786">786</a> <strong>if</strong> (!isSecure() || !isProxied()) {<a name="787" href="#787">787</a> <strong>throw</strong> <strong>new</strong> IllegalStateException(<a name="788" href="#788">788</a> <span class="string">"Connection must be secure "</span><a name="789" href="#789">789</a> + <span class="string">"and proxied to use this feature"</span>);<a name="790" href="#790">790</a> }<a name="791" href="#791">791</a> <a name="792" href="#792">792</a> <strong>if</strong> (usingSecureSocket) {<a name="793" href="#793">793</a> <strong>throw</strong> <strong>new</strong> IllegalStateException(<span class="string">"Already using a sec
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -