标签: apache-httpcomponents

java.lang.NoClassDefFoundError:解析失败:Lorg/apache/http/ProtocolVersion

当我使用Android Studio 3.1构建Android P的应用程序时,我遇到了这样的错误,可以制作apk,但是当我在Android P模拟器上使用它时,它会崩溃并抛出以下信息,更多细节请参见图片.

java.lang.NoClassDefFoundError:解析失败:Lorg/apache/http/ProtocolVersion

在此输入图像描述

App模块下的build.gradle的一部分在下面,有人见过这个吗?并给出一些建议?非常感谢.

android {

     compileSdkVersion 'android-P'
     buildToolsVersion '28-rc1'

    useLibrary 'org.apache.http.legacy'

    //for Lambda
    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
        sourceCompatibility JavaVersion.VERSION_1_8
    }

    packagingOptions {

        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }
    defaultConfig {
        applicationId "xxx.xxx.xxx"
        minSdkVersion 17
        targetSdkVersion 27
        versionCode xxxx
        versionName "Vx.x.x"

        multiDexEnabled true


     //other setting required
        ndk {

            abiFilters 'armeabi', 'armeabi-v7a', 'armeabi-v8a', 'x86', 'x86_64', 'mips', 'mips64'

        }
Run Code Online (Sandbox Code Playgroud)

android google-maps gradle apache-httpcomponents android-9.0-pie

275
推荐指数
11
解决办法
8万
查看次数

忽略Apache HttpClient 4.3中的SSL证书

如何忽略Apache HttpClient 4.3的 SSL证书(全部信任)?

我在SO上找到的所有答案都会处理以前的版本,并且API已更改.

有关:

编辑:

  • 它仅用于测试目的.孩子们,不要在家里(或在生产中)尝试

java ssl apache-httpcomponents

97
推荐指数
8
解决办法
16万
查看次数

Apache HttpClient Android(Gradle)

我已将此行添加到build.gradle中

compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5'
Run Code Online (Sandbox Code Playgroud)

我想在我的代码中使用MultipartEntityBuilder.但Android Studio不会将库添加到我的代码中.谁能帮我这个?

android gradle apache-httpcomponents multipartentity android-studio

52
推荐指数
4
解决办法
13万
查看次数

Apache HttpComponents HttpClient超时

如何在httpcomponents httpclient中设置连接超时?我在http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html找到了文档,但不清楚这些参数是如何实际设置的.

此外,之间的区别的解释SO_TIMEOUTCONNECTION_TIMEOUT将是有益的.

java http apache-httpcomponents apache-httpclient-4.x

43
推荐指数
4
解决办法
5万
查看次数

Apache HttpClient(4.1及更新版):如何进行基本身份验证?

如何为httpClient库的默认客户端添加基本身份验证?我已经看到了他们使用的示例client.getCredentialProvider(),但我认为所有这些方法都适用于库版本4.0.1或3.x. 有没有一个如何做到这一点的新例子?非常感谢.

java apache-httpcomponents apache-httpclient-4.x

40
推荐指数
4
解决办法
7万
查看次数

使用Spring REST模板,创建太多连接或缓慢

我有一个非常快速的RESTful服务.我在localhost上测试它.客户端正在使用Spring REST模板.我开始使用一种天真的方法:

RestTemplate restTemplate = new RestTemplate(Collections.singletonList(new GsonHttpMessageConverter()));

Result result = restTemplate.postForObject(url, payload, Result.class);
Run Code Online (Sandbox Code Playgroud)

当我提出很多这些请求时,我收到以下异常:

Caused by: org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost:8080/myservice":No buffer space available (maximum connections reached?): connect; nested exception is java.net.SocketException: No buffer space available (maximum connections reached?): connect
Run Code Online (Sandbox Code Playgroud)

这是由连接未关闭并挂在TIME_WAIT状态引起的.当短暂的端口耗尽时,异常开始发生.然后执行等待端口再次空闲.我看到长时间休息的高峰表现.我得到的速度几乎是我所需要的,但当然,这些TIME_WAIT连接并不好.在Linux(Ubuntu 14)和Windows(7)上进行了测试,由于端口的不同范围,在不同时间得到了类似的结果.

为了解决这个问题,我尝试使用HttpClient和Apache Http Components库中的HttpClientBuilder.

RestTemplate restTemplate = new RestTemplate(Collections.singletonList(new GsonHttpMessageConverter()));
HttpClient httpClient = HttpClientBuilder.create()
        .setMaxConnTotal(TOTAL)
        .setMaxConnPerRoute(PER_ROUTE)
        .build();
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));

Result result = restTemplate.postForObject(url, payload, Result.class);
Run Code Online (Sandbox Code Playgroud)

有了这个客户,我看到没有例外.客户端现在只使用非常有限数量的短暂端口.但无论我使用什么设置(TOTAL和PER_ROUTE),我都无法获得所需的性能.

使用该netstat命令,我发现服务器连接的次数不多.我尝试将数字设置为数千,但似乎客户端从未使用过那么多.

如果不打开太多连接,我能做些什么来提高性能?


更新:我已经尝试将总路由和每个路由连接的数量设置为5000和2500,但它仍然看起来客户端不会创建超过一百(从判断netstat -n | wc …

java rest spring http apache-httpcomponents

30
推荐指数
1
解决办法
3万
查看次数

在apache http客户端设置超时

我正在使用Apache http客户端4.3.2发送get请求.我所做的是:

private final RequestConfig requestConfig = RequestConfig.custom()
        .setConnectTimeout(1000)
        .setConnectionRequestTimeout(1000)
        .setSocketTimeout(1000)
        .build();
private final HttpClient client = HttpClientBuilder.create()
        .disableAuthCaching()
        .disableAutomaticRetries()
        .disableConnectionState()
        .disableContentCompression()
        .disableCookieManagement()
        .disableRedirectHandling()
        .setDefaultRequestConfig(requestConfig)
        .build(); 
Run Code Online (Sandbox Code Playgroud)

并在发送请求时:

HttpGet request = null;

try {
    request = new HttpGet(url);
    if (client.execute(request).getStatusLine().getStatusCode() == 200) {
        /* do some work here */
    }
} catch (Exception e) {
    Logger.error(e);
} finally {
    if (request != null) {
        request.releaseConnection();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我的一些请求仍然需要很长时间才能超时.这是异常的堆栈跟踪:

java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:152)
    at java.net.SocketInputStream.read(SocketInputStream.java:122)
    at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:136) …
Run Code Online (Sandbox Code Playgroud)

java timeout apache-httpcomponents

20
推荐指数
3
解决办法
3万
查看次数

如何发送并行GET请求并等待结果响应?

我在spring mvc 3.2.2中使用apache http client来同步发送5个get请求,如图所示.

如何异步(并行)发送所有这些并等待请求返回以便从所有GET请求返回已解析的有效负载字符串?

public String myMVCControllerGETdataMethod()
{
   // Send 1st request 
   HttpClient httpclient = new DefaultHttpClient();
   HttpGet httpget = new HttpGet("http://api/data?type=1");   
   ResponseHandler<String> responseHandler = new BasicResponseHandler();
   String responseBody = httpclient.execute(httpget, responseHandler);

   // Send 2st request 
   HttpClient httpclient2 = new DefaultHttpClient();
   HttpGet httpget2 = new HttpGet("http://api/data?type=2");   
   ResponseHandler2<String> responseHandler2 = new BasicResponseHandler();
   String responseBody2 = httpclient.execute(httpget, responseHandler2);

   // o o o more gets here

   // Perform some work here...and wait for all requests to return
   // Parse info out …
Run Code Online (Sandbox Code Playgroud)

java spring-mvc apache-httpcomponents apache-httpclient-4.x

19
推荐指数
3
解决办法
5万
查看次数

连接和连接请求超时

我正在使用Http Apache Components来执行http交互.我需要调整我的http客户端.为此,我有两个参数:连接超时和连接请求超时.在库文档和源代码中(没有找到评论)我没有找到这个术语的定义.我需要知道它们究竟意味着什么.可能是他们在HTTP协议文档中定义但我找不到它.所以,我的问题是这两个术语是什么意思以及它们如何彼此区分.

java http apache-httpcomponents apache-httpclient-4.x

19
推荐指数
3
解决办法
2万
查看次数

当EntityUtils.toString()返回异常时,有没有办法获取HttpEntity的String值?

我一直遇到这种情况,我收回了一个错误的HTTP响应(比如400),但是无法查看HttpResponse对象中的HttpEntity.当我逐步使用调试器时,我可以看到实体有内容(长度> 0),我甚至可以查看内容,但我看到的只是一个数字数组(我想是ASCII码?)很有帮助.我将在实体上调用EntityUtils.toString(),但是我得到了一个异常 - IOException或某种"对象处于无效状态"异常.这真是令人沮丧!有没有办法以人类可读的形式获取这些内容?

这是我的代码:

    protected JSONObject makeRequest(HttpRequestBase request) throws ClientProtocolException, IOException, JSONException, WebRequestBadStatusException {

    HttpClient httpclient = new DefaultHttpClient();

    try {
        request.addHeader("Content-Type", "application/json");
        request.addHeader("Authorization", "OAuth " + accessToken);
        request.addHeader("X-PrettyPrint", "1");

        HttpResponse response = httpclient.execute(request);
        int statusCode = response.getStatusLine().getStatusCode();

        if (statusCode < 200 || statusCode >= 300) {
            throw new WebRequestBadStatusException(statusCode);
        }

        HttpEntity entity = response.getEntity();

        if (entity != null) {
            return new JSONObject(EntityUtils.toString(entity));
        } else {
            return null;
        }

    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}
Run Code Online (Sandbox Code Playgroud)

看看我抛出异常的位置?我想做的是吮吸HttpEntity的内容并将其置于异常中.

java apache-httpcomponents apache-httpclient-4.x

17
推荐指数
3
解决办法
4万
查看次数