Tag: httpclient

无法找到所请求目标的有效证书路径 – java

我正在尝试使用HttpClient对象连接到网站。 它适用于我们通常使用的网站(如谷歌)。 但是有一个网站,当我尝试连接时,我的程序给出了这个错误.. javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1917) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:301) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:295) at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1369) ……………….. Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387) at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292) at sun.security.validator.Validator.validate(Validator.java:260) …………… Caused […]

multithreadinghttpClient

public class test { public static final int nThreads = 2; public static void main(String[] args) throws ExecutionException, InterruptedException{ // Runnable myrunnable = new myRunnable(); ExecutorService execute = Executors.newFixedThreadPool(nThreads); for (int i = 0; i = maxCalls) { break; } try { Thread.currentThread().sleep(sleepMillis); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } […]

如何在apache http客户端中为所有请求设置默认头?

例如,默认用户代理可以设置为: client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, someName); 但是如何设置“Accept”标题?

JAVA:http发布请求

我必须向web服务发送请求,以使用用户名和密码对用户进行身份validation。 我对以下post请求有疑问: public String postTest(String action, ConnectionParametrData [] parameters) { Uri.Builder builder = new Uri.Builder().scheme(scheme).authority(authority).path(action); uri = builder.build(); BufferedReader in = null; String ans = null; HttpPost request = new HttpPost(uri.toString()); HttpClient defaultClient = new DefaultHttpClient(); try { request.setHeader(“Content-Type”, “application/x-www-form-urlencoded”); request.setEntity(new UrlEncodedFormEntity(getValuePairs(parameters))); HttpResponse response = defaultClient.execute(request); in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), “UTF-8”), 8192); StringBuffer sb […]

信任过期的证书

在与具有过期证书的https服务器通信时,我的客户端失败并出现以下错误。 虽然我们正在等待通过更新来修复服务器端,但我想知道我们是否可以通过将过期的证书添加到我们自己的信任存储来传递此错误? 这样我们就可以在等待续订证书的同时获得一些测试时间。 US has an end date Thu Sep 08 19:59:59 EDT 2011 which is no longer valid. [4/17/13 19:22:55:618 EDT] 00000021 SystemOut O WebContainer : 0, SEND TLSv1 ALERT: fatal, description = certificate_unknown [4/17/13 19:22:55:620 EDT] 00000021 SystemOut O WebContainer : 0, WRITE: TLSv1 Alert, length = 2 [4/17/13 19:22:55:620 EDT] 00000021 SystemOut […]

如何在httpclient 4.3+中更新HttpClient的设置?

在httpclient 4.3中,如果您尊重所有弃用,则必须使用HttpClientBuilder ( 此处提供文档)构建和配置HttpClient 。 这些方法是显式的,它们看起来很容易使用,并且HttpClient的界面很清晰。 但也许有点太多了。 在我自己的情况下,我必须inheritanceSpring的HttpComponentsHttpInvokerRequestExecutor ( 此处的文档)。 因此,我可以轻松获取HttpClient ,但我对此对象的所有了解都是它实现了接口。 由于客户端已经构建,并且我对它的实现一无所知,我无法访问AbstractHttpClient的setHttpRequestRetryHandler或addRequestInterceptor (虽然是的,我知道,它们已被弃用)。 那么,更新这个HttpClient的设置最简洁的方法是什么(重试处理程序和请求拦截器是我目前最关心的那些)? 我是不是该… …野蛮地将我的客户端投射到AbstractHttpClient ,希望我会一直收到这个实现? …在我的HttpInvokerRequestExecutor的构造函数中创建一个新的HttpClient ,并获得类似下面的示例? 我可能会补充说Spring的构造函数(至少在3.2.4中)也使用了httpclient 4.3中不推荐使用的方法。 使用这种策略会不会有任何副作用? ……做一些我还没有提出的建议吗? 自定义构造函数示例: public CustomHttpInvokerRequestExecutor() { super(); // will create an HttpClient // Now overwrite the client the super constructor created setHttpClient(HttpClientBuilder.custom(). … .build()); }

使用Java org.apache.http.client获取重定向URL

在向服务器发帖后,我需要帮助弄清楚如何获取重定向。 首先,我需要从服务器获取一些cookie。 然后我用cookie和其他参数执行post。 然后服务器以302重定向回答。 如何获取该重定向的url? 代码如下所示: HttpGet get = new HttpGet(urlOne); try { //Creating a local instance of cookie store. CookieStore cookieJar = new BasicCookieStore(); // Creating a local HTTP context HttpContext localContext = new BasicHttpContext(); // Bind custom cookie store to the local context localContext.setAttribute(ClientContext.COOKIE_STORE, cookieJar); HttpResponse response = httpClient.execute(get, localContext); HttpEntity entity = […]

将参数添加到Apache HttpPost

我正在尝试将文件发送到Servlet。 除了这个文件,我还必须发送一些参数(即名称/ ID,日期和其他几个)。 我在客户端使用HttpClient,在服务器端使用ServerFileUpload。 这是客户端代码:… String url = “http://localhost:8080/RicezioneServlet/RicezioneServlet”; HttpClient httpclient = new DefaultHttpClient(); HttpPost postMethod = new HttpPost(url); MultipartEntity mpe = new MultipartEntity(); //I’m sending a .zip file ContentBody cb = new FileBody(fileToSend,”application/zip”); mpe.addPart(“file”, cb); postMethod.setEntity(mpe); HttpResponse resp = httpclient.execute(postMethod); HttpEntity respEntity = resp.getEntity(); System.out.println(resp.getStatusLine()); … 在服务器端,我们有: ServletFileUpload sup = new ServletFileUpload(); FileItemIterator it […]

Java服务器自签名证书+客户端证书和SSL – 连接重置

(我已经问过类似的问题了 ,事实certificate我的客户端密钥没有被加载,但我只有一个例外,所以我发布了另一个问题。) 我正在连接到成功之前使用的Web服务,但现在他们已经更改了主机名并向我发送了两个.pem文件; 一个是CA,另一个是我的新客户证书。 (我正在使用Java 1.5,Spring + Spring Web Services和Apache httpclient,但我怀疑我的问题是证书,密钥和SSL本身。) 我已经导入了两个.pem文件,以及我从Firefox导出到我的cacerts的主机.crt。 但是,由于我遇到这个exception,我显然做错了什么: org.springframework.ws.client.WebServiceIOException: I/O error: Connection reset; nested exception is java.net.SocketException: Connection reset Caused by: java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:168) at com.sun.net.ssl.internal.ssl.InputRecord.readFully(InputRecord.java:284) at com.sun.net.ssl.internal.ssl.InputRecord.readV3Record(InputRecord.java:396) at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:348) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:720) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1025) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:619) at com.sun.net.ssl.internal.ssl.AppOutputStream.write(AppOutputStream.java:59) at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65) at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123) at org.apache.commons.httpclient.methods.EntityEnclosingMethod.writeRequestBody(EntityEnclosingMethod.java:502) at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:1973) at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:993) […]

使用SSL加密和NTLM身份validation的HttpClient失败

我正在尝试在使用SSL加密(https)的Sharepoint 2010服务器上执行简单的REST调用,以及NTLM身份validation。 当服务器设置为不需要SSL(仅用于测试,服务器将需要生产中的SSL)时,我的NTLM身份validation和后续REST调用可以正常使用HttpClient。 但是,启用SSL后,身份validation将不再有效。 这是SSL处理的代码(设置为接受所有证书): SSLContext sslContext = SSLContext.getInstance(“TLS”); // set up a TrustManager that trusts everything sslContext.init(null, new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) { } public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }, new SecureRandom()); SSLSocketFactory sf = new […]