Tag: apache httpclient 4.x

HttpClient 4.3.x,修复已弃用的代码以使用当前的HttpClient实现

我有以下代码,仍然编译,但他们都被弃用了: SSLSocketFactory sslSocketFactory = new SSLSocketFactory(context, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); ClientConnectionManager clientConnectionManager = base.getConnectionManager(); SchemeRegistry schemeRegistry = clientConnectionManager.getSchemeRegistry(); schemeRegistry.register(new Scheme(“https”, 443, sslSocketFactory)); return new DefaultHttpClient(clientConnectionManager, base.getParams()); 我尽力用这部分代码替换它: HttpClientBuilder builder = HttpClientBuilder.create(); SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(context, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); builder.setConnectionManager(new BasicHttpClientConnectionManager()); builder.setSSLSocketFactory(sslConnectionFactory); return builder.build(); 正如您所看到的,顶部post中几行代码我不知道如何包含在新部分中。 如何添加所需的代码,例如备用的SchemeRegistry ?

DefaultHttpClient在多个请求上保持活动连接

我正在使用DefaultHttpClient通过基本身份validation向同一URL发出大量请求。 像这样的东西: for (String json: listOfItems) { DefaultHttpClient client = new DefaultHttpClient(); try { client.getCredentialsProvider().setCredentials( new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, “basic”), new UsernamePasswordCredentials(user, pass)); HttpPost request = new HttpPost(path); setHeaders(request); StringEntity se = new StringEntity(json, HTTP.UTF_8); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON)); request.setEntity(se); client.execute(request); } finally { // close/release connection client.getConnectionManager().shutdown(); } } 我的问题是在这样做的同时保持连接的最佳方法是什么。 所以我不需要关闭每个post请求的连接。

握手在棒棒糖上失败

我正在尝试进行HTTP POST,但我得到两个不同的错误: javax.net.ssl.SSLHandshakeException: Handshake failed net.ssl.SSLPeerUnverifiedException: No peer certificate 我通过此处指定的解决方法修复了无对等证书错误: https : //stackoverflow.com/a/4837230/4254419 但是虽然它修复了错误,但它会抛出一个新错误,这就是握手失败 我知道这不安全而且我不在乎,它不适合生产,所以我更不关心安全性。 这个问题有解决方法吗?

从Java中的特定IP生成HttpRequest

我正在使用Apache HttpClient生成Post请求并提交数据。 由于远程应用程序在提交数据的用户的IP地址上进行中继,因此我想发送用户指定IP地址的发布请求。 我该如何配置? public static void loginUser(String username, String password, String ip) throws Exception{ try { HttpClient client = new DefaultHttpClient(); HttpPost httppost = new HttpPost(“http://login.myapp.com/”); // Request parameters and other properties. List params = new ArrayList(2); params.add(new BasicNameValuePair(“username”,username)); params.add(new BasicNameValuePair(“password”, password)); httppost.setEntity(new UrlEncodedFormEntity(params, “UTF-8”)); // Execute and get the response. HttpResponse response = […]

Android JAVA到SharePoint 2013 NTLM 401取消授权

我是Java的新手。 因为我的公司需要创建一个App for SharePoint 2013.我继续得到错误401.我搜索了很多网站,我只是无法让它工作。 我正在使用的代码如下: StringBuilder sb = new StringBuilder(); SSLContext sslContext = null; // Error Handling try { sslContext = SSLContext.getInstance(“SSL”); sslContext.init(null, new TrustManager[]{new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { […]

httpclient版本与Apache Spark之间的冲突

我正在使用Apache Spark开发Java应用程序。 我用这个版本: org.apache.spark spark-core_2.10 1.2.2 在我的代码中,有一个过渡依赖: org.apache.httpcomponents httpclient 4.5.2 我将我的应用程序打包到一个JAR文件中。 当使用spark-submit在EC2实例上部署它时,我收到此错误。 Caused by: java.lang.NoSuchFieldError: INSTANCE at org.apache.http.conn.ssl.SSLConnectionSocketFactory.(SSLConnectionSocketFactory.java:144) at com.amazonaws.http.apache.client.impl.ApacheConnectionManagerFactory.getPreferredSocketFactory(ApacheConnectionManagerFactory.java:87) at com.amazonaws.http.apache.client.impl.ApacheConnectionManagerFactory.create(ApacheConnectionManagerFactory.java:65) at com.amazonaws.http.apache.client.impl.ApacheConnectionManagerFactory.create(ApacheConnectionManagerFactory.java:58) at com.amazonaws.http.apache.client.impl.ApacheHttpClientFactory.create(ApacheHttpClientFactory.java:50) at com.amazonaws.http.apache.client.impl.ApacheHttpClientFactory.create(ApacheHttpClientFactory.java:38) 此错误清楚地表明SparkSubmit已加载相同Apache httpclient库的旧版本,因此发生此冲突。 解决这个问题的好方法是什么? 出于某种原因,我不能在我的Java代码上升级Spark。 但是,我可以轻松地使用EC2群集。 是否可以在具有更高版本的1.6.1版本的集群上部署我的Java代码?

org.apache.http.impl.io.DefaultHttpRequestWriterFactory中的NoSuchFieldError INSTANCE

java version “1.7.0_71” Gradle 2.1 你好, UPDATE: 依赖关系 gradle dependencies | grep httpcore | +— org.apache.httpcomponents:httpcore:4.3.3 | +— org.apache.httpcomponents:httpcore:4.3.3 | +— org.apache.httpcomponents:httpcore:4.3.3 | +— org.apache.httpcomponents:httpcore:4.3.3 | | | | | +— org.apache.httpcomponents:httpcore:4.1 -> 4.3.3 | +— org.apache.httpcomponents:httpcore:4.3.3 | | | | | +— org.apache.httpcomponents:httpcore:4.1 -> 4.3.3 这是否意味着我有4.1这是4.3.3的软链接? 看起来很奇怪,因为当我打印出类加载器正在加载它时,似乎加载4.3.3: /home/steve/.gradle/caches/modules-2/files-2.1/org.apache.httpcomponents/httpcore/4.3.3/f91b7a4aadc5cf486df6e4634748d7dd7a73f06d/httpcore-4.3.3.jar似乎很奇怪。 当我尝试运行我的httpClient时,我不断收到此错误。 一切都编好了。 java.lang.NoSuchFieldError: INSTANCE at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.(DefaultHttpRequestWriterFactory.java:52 […]

如何使Unirest(java)忽略证书错误

我正在使用Unirest(java版本)来发出GET和POST请求。但是我在访问SSL加密站点时遇到问题,因为我的程序在公司网络后面,网络管理员为我设置了防火墙映射。 例如, foobar.com映射到56.1.89.12:4444 。 但是当我向地址发出请求时,我会收到以下ssl证书错误: com.mashape.unirest.http.exceptions.UnirestException: javax.net.ssl.SSLException: hostname in certificate didn’t match: != at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:131) at com.mashape.unirest.request.BaseRequest.asString(BaseRequest.java:52) 我看到Unirest已经提前配置使用自定义httpclient 。所以我使用 Unirest.setHttpClient(MyHttpClient.makeClient()); HttpResponse res = null; try { res = Unirest.get(urlstr).asString(); } catch (UnirestException e) { e.printStackTrace(); } String jsonstr = res.getBody(); makeClient方法是: public static HttpClient makeClient(){ SSLContextBuilder builder = new SSLContextBuilder(); CloseableHttpClient httpclient = null; try […]

从NameValuePairs列表创建UrlEncodedFormEntity会抛出NullPointerException

我正在创建一个unit testing来试用我刚刚创建的servlet。 @Test public void test() throws ParseException, IOException { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(“http://localhost:8080/WebService/MakeBaby”); List nameValuePairs = new ArrayList(); nameValuePairs.add(new BasicNameValuePair(“father_name”, “Foo”)); nameValuePairs.add(new BasicNameValuePair(“mother_name”, “Bar”)); post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = null; try { response = client.execute(post); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } […]

LG G3手机的Android 6.0 HTTPClient问题

您好我使用DefaultHTTPClient类来创建http请求。 适用于所有手机的执行代码execute()方法适用于所有手机,包括带有Android 6.0的Nexus和Samsung。 但是当我在Android手机上测试Android 6.0更新时,我收到错误,如下所示 Java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 03-28 17:21:17.040: E/xx_SDK(14035): at org.apache.http.impl.auth.DigestScheme.isGbaScheme(DigestScheme.java:210) 03-28 17:21:17.040: E/xx_SDK(14035): at org.apache.http.impl.auth.DigestScheme.processChallenge(DigestScheme.java:176) 03-28 17:21:17.040: E/xx_SDK(14035): at org.apache.http.impl.client.DefaultRequestDirector.processChallenges(DefaultRequestDirector.java:1097) 03-28 17:21:17.040: E/xx_SDK(14035): at org.apache.http.impl.client.DefaultRequestDirector.handleResponse(DefaultRequestDirector.java:980) 03-28 17:21:17.040: E/xx_SDK(14035): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:490) 03-28 17:21:17.040: E/xx_SDK(14035): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560) 03-28 17:21:17.040: E/xx_SDK(14035): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492) 03-28 17:21:17.040: E/xx_SDK(14035): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470) 03-28 17:21:17.040: E/xx_SDK(14035): at java.lang.Thread.run(Thread.java:818) 我试图了解什么问题,它谈到了摘要式身份validation。 我从Android […]