Java 如何引入 Apache HttpClient Lookup 加载器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24585901/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:12:59  来源:igfitidea点击:

How to bring in a Apache HttpClient Lookup loader?

javaapache-httpclient-4.xapache-httpcomponents

提问by will

I have trouble running the Quick Start example from Apache HttpComponents:

我在运行 Apache HttpComponents 的快速入门示例时遇到问题:

Initially I was using it as a guide. When that failed, I downloaded the example Java file and still with no luck. I believe there's some missing information on the page. After all a quick-start should run, and get you started.

最初我用它作为指南。当失败时,我下载了示例 Java 文件,但仍然没有运气。我相信页面上缺少一些信息。毕竟,应该运行快速入门,并让您开始。

I think this is like the problem described here.

我认为这就像这里描述的问题。

I believe the answer is in this question (also indicated by the comment below). I've tested the suggested answer, and it works for me!

我相信答案就在这个问题中(也由下面的评论指出)。我已经测试了建议的答案,它对我有用!

The Quick Start example code fails on the first line basically. I split it up to check things step by step.

快速入门示例代码基本上在第一行失败。我把它拆开一步一步检查。

public class QuickStart {

    public static void quickStart() throws IOException { // String[] args) throws Exception {

        CloseableHttpClient httpclient;
        httpclient = HttpClients.createDefault();

        try {
            HttpGet httpGet;
                :

This line throws an error:

此行引发错误:

  • httpclient = HttpClients.createDefault();
  • Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/config/Lookup
  • httpclient = HttpClients.createDefault();
  • 线程“main”中的异常 java.lang.NoClassDefFoundError: org/apache/http/config/ Lookup

I appreciate the comment that the look-up class may be in the HttpCore module. However the quick start and other beginner tutorials do not explain HOW to bring in the Core component into my example project? There's is no mention of HttpCore or Lookup -- I did a string find on the page.

我很欣赏查找类可能在 HttpCore 模块中的评论。然而,快速入门和其他初学者教程没有解释如何将核心组件引入我的示例项目中?没有提到 HttpCore 或 Lookup——我在页面上找到了一个字符串。

There are a couple of questions really.

确实有几个问题。

  1. What is needed to fix the Quick Start example for it to run as written?
    • An implication here is that the Quick Start page is broken because the example does not start, quick or otherwise.
  2. How does one "induce" the HttpCore to integrate with a simple one Java file program illustrated by the HttpClient Quick Start?
  3. Is there or was there some intention for the HttpCore to be brought in optionally?
    • I looked in the POM file, the HttpClient has-a dependency on HttpCore for the build.
    • Do I need to add something here to bring the HttpCore along into the JAR or something?
  4. Is there a better way to use the HttpClient to make a small Java program similar to the Quick Start?
  5. More in the spirit of improving the web: Please TEST examples and Quick Starts before posting them...
    • This happens more often than folk would believe. Generally there's small things which I just fix. I think it would be better if we can post stuff that runs.
  1. 需要什么来修复 Quick Start 示例以使其按编写的方式运行?
    • 这里的含义是 Quick Start 页面已损坏,因为示例没有启动,无论是快速还是其他方式。
  2. 如何“诱导” HttpCore 与HttpClient 快速入门所示的简单 Java 文件程序集成?
  3. 是否有或是否有意选择引入 HttpCore?
    • 我查看了 POM 文件,HttpClient 依赖于 HttpCore 进行构建。
    • 我是否需要在此处添加一些内容以将 HttpCore 带入 JAR 或其他内容?
  4. 有没有更好的方法使用HttpClient来制作一个类似于Quick Start的Java小程序?
  5. 更多本着改进网络的精神:请在发布之前测试示例和快速入门......
    • 这种情况发生的频率比人们想象的要高。通常有一些小事情我只是修复。我认为如果我们可以发布运行的东西会更好。

Concerning this issue. The call: HttpClients.createDefault(), should be expected to work based on the quick start code, otherwise the name "createDefault" appears to be an error. Many advance thanks ...

关于这个问题。调用:HttpClients。createDefault(),应该是基于快速启动代码的预期工作,否则名称“ createDefault”似乎是一个错误。许多提前感谢...

update:

更新

I thought I should show what Netbeans put in the POM for the HttpComponents ...

我想我应该展示 Netbeans 为 HttpComponents 放入 POM 中的内容......

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3.4</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.3.2</version>
        <type>jar</type>
    </dependency>

I've looked at some other examples. These also just call defaultClient(). So it seems like a bit more of a mystery than just there being a problem in the quick-start example. Other examples:

我看过其他一些例子。这些也只是调用 defaultClient()。因此,这似乎比快速启动示例中存在问题更神秘。其他例子:

Still no explicit mention for Lookup or linking with HttpCore. Should these examples work? If so what 'extra' is needed in a (Netbeans) Maven build to make it happen?

仍然没有明确提及 Lookup 或与 HttpCore 的链接。这些例子应该有效吗?如果是这样,在(Netbeans)Maven 构建中需要什么“额外”来实现它?

回答by sschrass

defaultHttpClientist deprecated source, as you already found out. Use a HttpClientBuilderinstead source. You will get a CloseableHttpClient, which is handy with Java 8 since you can skip some finally-blocks.

defaultHttpClientist 已弃用source,正如您已经发现的那样。使用HttpClientBuilder替代来源。你会得到一个CloseableHttpClient,它在 Java 8 中很方便,因为你可以跳过一些finally-blocks。