HTTP协议请求实现类CloseableHttpClient和HttpsURLConnection及HttpURLConnection

前者是apache提供的http连接池技术,后者是java原生http请求实现类。

Java发HTTP请求,最常见的无非是两种,一种是用java.net.URLConnection,另一种则是大名鼎鼎的HttpClient。使用java.net.URLConnection比较麻烦,而HttpClient则比较简便。

发起HTTP协议的交互请求:
想发起就能发起(符合万维网系统制定的HTTP协议即可,规定格式的请求头,请求体等),但能不能返回和返回什么由目标接口决定。如果返回有问题,生成的状态码是万维网系统检测并返回吗?为什么后台开发者也能设定状态码?

一:apache

package org.apache.http.impl.client;

 


import java.io.Closeable;
import java.io.IOException;
import java.net.URI;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.annotation.ThreadSafe;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.Args;
import org.apache.http.util.EntityUtils;


/**
 * Base implementation of {@link HttpClient} that also implements {@link Closeable}.
 *
 * @since 4.3
 */
@ThreadSafe

 

 

public abstract class CloseableHttpClient implements HttpClient, Closeable {

 

CloseableHttpClient是否就是http协议规则的实现?

 

二:java原生类

原生类URL:

Returns a {@link java.net.URLConnection URLConnection} instance that
represents a connection to the remote object referred to by the
{@code URL}.


<P>A new instance of {@linkplain java.net.URLConnection URLConnection} is
created every time when invoking the
{@linkplain java.net.URLStreamHandler#openConnection(URL)
URLStreamHandler.openConnection(URL)} method of the protocol handler for
this URL.</P>


<P>It should be noted that a URLConnection instance does not establish
the actual network connection on creation. This will happen only when
calling {@linkplain java.net.URLConnection#connect() URLConnection.connect()}.

此方法并没有建立连接!

public URLConnection openConnection() throws java.io.IOException {
        return handler.openConnection(this);
    }

 

原生的http实现类使用getOutputstream()向服务器发送数据和getInputstream和从服务器读取数据

原生http实现类的connect()方法:继承自下面的抽象类

AbstractDelegateHttpsURLConnection类

public void connect() throws IOException {
        if (!this.connected) {
            this.plainConnect();
            if (this.cachedResponse == null) {
                if (!this.http.isCachedConnection() && this.http.needsTunneling()) {
                    this.doTunneling();
                }
                ((HttpsClient)this.http).afterConnect();
            }
        }
    }

Opens a communications link to the resource referenced by this URL, if such a connection has not already been established.


If the {@code connect} method is called when the connection has already been opened (indicated by the {@code connected} field having the value {@code true}), the call is ignored.


URLConnection objects go through two phases: first they are created, then they are connected.  After being created, and before being connected, various options can be specified(e.g., doInput and UseCaches).  
After connecting, it is an error to try to set them.  
Operations that depend on being connected, like getContentLength, will implicitly perform the connection, if necessary.
throws SocketTimeoutException if the timeout expires before the connection can be established
exception  IOException  if an I/O error occurs while opening the connection.

如果此连接尚未建立,则打开此url引用的资源的通信链路。

如果在连接已经打开时调用{@code connect}方法({@code connected}字段有值{@code true}),则忽略该调用。

URLConnection对象经过两个阶段:首先创建它们,然后连接它们。在创建之后,在连接之前,可以指定各种选项(例如:,doInput UseCaches)。

连接后,试图设置它们是一个错误。

依赖于连接的操作,比如getContentLength,如果必要,将隐式地执行连接。

如果在建立连接之前超时过期,则抛出SocketTimeoutException。

当打开连接时发生I/O错误时例外IOException。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值