您的位置:首页 > 理论基础 > 计算机网络

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

2018-02-07 14:38 477 查看
前者是apache提供的http连接池技术,后者是java原生http请求实现类。

发起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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: