Java HttpClient 4.3.1 创建 HttpClient 实例的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19834801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Issues with HttpClient 4.3.1 creating instance of HttpClient
提问by Brad Ellis
I am trying to convert Http upload to use the new HttpClient 4.3.1 classes. I'm new to Java. Everything I find online uses deprecated classes (i.e. HttpClient client = new DefaultHttpClient() or an even older method for creating an instance of HttpClient. Forgive all the extra libraries below, some will be needed in the rest of my project.
我正在尝试将 Http 上传转换为使用新的 HttpClient 4.3.1 类。我是 Java 新手。我在网上找到的所有东西都使用不推荐使用的类(即 HttpClient 客户端 = new DefaultHttpClient() 或更旧的方法来创建 HttpClient 的实例。请原谅下面所有额外的库,我的项目的其余部分将需要一些库。
I've tried umpteen different ways to create the instance, what I have below is the method I see used in org.appache documenation for 4.3.1.
Unfortunately, I'm getting an error indicating that HttpClientBuilder is not visible. I'm not even sure what not visible means...the library has been imported. Can anyone point me in the right direction for creating an HttpClient instance.
我已经尝试了无数种不同的方法来创建实例,下面是我在 org.appache 文档中看到的用于 4.3.1 的方法。
不幸的是,我收到一个错误,表明 HttpClientBuilder 不可见。我什至不确定不可见是什么意思……图书馆已被导入。任何人都可以指出我创建 HttpClient 实例的正确方向。
package newHttpApiUpload;
import org.apache.http.client.HttpClient;
import org.apache.http.HttpConnection;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.AbstractHttpEntity;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class Api {
protected static final HttpClientBuilder client = new HttpClientBuilder();
}
采纳答案by Joeri Hendrickx
The constructor HttpClientBuilder()
is protected (not public) so you cannot call it from your code. That is what not visible means.
构造函数HttpClientBuilder()
是受保护的(不是公共的),所以你不能从你的代码中调用它。这就是不可见的意思。
You invoke the builder using a static constructor method, as such:
您使用静态构造函数方法调用构建器,如下所示:
HttpClientBuilder.create();
Or, to quickly create a client (which is the whole point)
或者,快速创建一个客户端(这是重点)
HttpClient client = HttpClientBuilder.create()
.setUserAgent("MyAgent")
.setMaxConnPerRoute(4)
.build()
回答by axtavt
You need to use static factory methods from HttpClients
.
您需要使用来自HttpClients
.
You can use them to obtain preconfigured instances of HttpClient
, or use HttpClients.custom()
to apply your custom configuration using HttpClientBuilder
.
您可以使用它们来获取 的预配置实例HttpClient
,或者使用它们HttpClients.custom()
来应用您的自定义配置HttpClientBuilder
。