- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想从 Commons HttpClient (3.x) 迁移到 HttpComponents Client (4.x),但很难处理重定向。该代码在 Commons HttpClient 下正常工作,但在迁移到 HttpComponents Client 时中断。一些链接得到了不需要的重定向,但是当我将“http.protocol.handle-redirects”设置为“false”时,大量链接完全停止工作。
Commons HttpClient 3.x:
private static HttpClient httpClient = null;
private static MultiThreadedHttpConnectionManager connectionManager = null;
private static final long MAX_CONNECTION_IDLE_TIME = 60000; // milliseconds
static {
//HttpURLConnection.setFollowRedirects(true);
CookieManager manager = new CookieManager();
manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(manager);
connectionManager = new MultiThreadedHttpConnectionManager();
connectionManager.getParams().setDefaultMaxConnectionsPerHost(1000); // will need to set from properties file
connectionManager.getParams().setMaxTotalConnections(1000);
httpClient = new HttpClient(connectionManager);
}
/*
* Retrieve HTML
*/
public String fetchURL(String url) throws IOException{
if ( StringUtils.isEmpty(url) )
return null;
GetMethod getMethod = new GetMethod(url);
HttpClient httpClient = new HttpClient();
//configureMethod(getMethod);
//ObjectInputStream oin = null;
InputStream in = null;
int code = -1;
String html = "";
String lastModified = null;
try {
code = httpClient.executeMethod(getMethod);
in = getMethod.getResponseBodyAsStream();
//oin = new ObjectInputStream(in);
//html = getMethod.getResponseBodyAsString();
html = CharStreams.toString(new InputStreamReader(in));
}
catch (Exception except) {
}
finally {
try {
//oin.close();
in.close();
}
catch (Exception except) {}
getMethod.releaseConnection();
connectionManager.closeIdleConnections(MAX_CONNECTION_IDLE_TIME);
}
if (code <= 400){
return html.replaceAll("\\s+", " ");
} else {
throw new Exception("URL: " + url + " returned response code " + code);
}
}
HttpComponents 客户端 4.x:
private static HttpClient httpClient = null;
private static HttpParams params = null;
//private static MultiThreadedHttpConnectionManager connectionManager = null;
private static ThreadSafeClientConnManager connectionManager = null;
private static final int MAX_CONNECTION_IDLE_TIME = 60000; // milliseconds
static {
//HttpURLConnection.setFollowRedirects(true);
CookieManager manager = new CookieManager();
manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(manager);
connectionManager = new ThreadSafeClientConnManager();
connectionManager.setDefaultMaxPerRoute(1000); // will need to set from properties file
connectionManager.setMaxTotal(1000);
httpClient = new DefaultHttpClient(connectionManager);
// HTTP parameters stores header etc.
params = new BasicHttpParams();
params.setParameter("http.protocol.handle-redirects",false);
}
/*
* Retrieve HTML
*/
public String fetchURL(String url) throws IOException{
if ( StringUtils.isEmpty(url) )
return null;
InputStream in = null;
//int code = -1;
String html = "";
// Prepare a request object
HttpGet httpget = new HttpGet(url);
httpget.setParams(params);
// Execute the request
HttpResponse response = httpClient.execute(httpget);
// The response status
//System.out.println(response.getStatusLine());
int code = response.getStatusLine().getStatusCode();
// Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
try {
//code = httpClient.executeMethod(getMethod);
//in = getMethod.getResponseBodyAsStream();
in = entity.getContent();
html = CharStreams.toString(new InputStreamReader(in));
}
catch (Exception except) {
throw new Exception("URL: " + url + " returned response code " + code);
}
finally {
try {
//oin.close();
in.close();
}
catch (Exception except) {}
//getMethod.releaseConnection();
connectionManager.closeIdleConnections(MAX_CONNECTION_IDLE_TIME, TimeUnit.MILLISECONDS);
connectionManager.closeExpiredConnections();
}
}
if (code <= 400){
return html;
} else {
throw new Exception("URL: " + url + " returned response code " + code);
}
}
我不想要重定向,但是在 HttpClient 4.x 下,如果我启用重定向,那么我会得到一些不需要的,例如http://www.walmart.com/ => http://mobile.walmart.com/ .在 HttpClient 3.x 下,不会发生此类重定向。
我需要做什么才能在不破坏代码的情况下将 HttpClient 3.x 迁移到 HttpClient 4.x?
最佳答案
这不是 HttpClient 4.x 的问题,可能是目标服务器处理请求的方式,因为用户代理是 httpclient,它可能被处理为移动(目标服务器可能会考虑可用浏览器以外的其他浏览器,例如, chrome、mozilla 等作为移动设备。)
请使用以下代码手动设置
httpclient.getParams().setParameter(
org.apache.http.params.HttpProtocolParams.USER_AGENT,
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"
);
关于java - 从 Commons HttpClient 迁移到 HttpComponents 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10431561/
我需要一些帮助,澄清如何将 HttpAsyncClient 与多部分实体一起使用。基本上,我正在尝试使用 HttpAsyncClient 将文件 (byte[]) 的内容上传为 MultiPart B
我有一个包含 HTTP header 的字符串。我想将其转换为 Apache HttpComponents HttpRequest 对象。有没有一种方法可以做到这一点,而无需自己拆开字符串? 本教程:
如何使 http 组件中的调试日志静音? 09:23:22.145 [main] DEBUG o.a.h.c.protocol.RequestAddCookies - CookieSpec selec
使用我在这里找到的代码:http://hc.apache.org/httpcomponents-client-ga/quickstart.html我创建了一个快速 HTTP 客户端应用程序。这是一个代
我正在尝试学习如何使用 Apache HttpComponents 发送 HttpResponse。我发现some examples在 Apache 网站上,但它们非常困惑且不清楚。 在下面的代码中,
我找不到任何关于 httpclient 4.1 的默认 httpParams 的文档? 执行 GET 时的默认套接字超时是多少? 最佳答案 根据documentation ,http.socket.t
我正在尝试将 Apache httpcomponents-client 库添加到我的 Maven 项目中。我已经向 pom.xml 添加了一个依赖项(在 http://mvnrepository.co
只是尝试在Android应用中发出一些POST请求。我正在用独立脚本中的HttpClient 4.5.3(https://hc.apache.org/downloads.cgi)测试。一切正常,但是当
我当前正在运行一个程序,该程序将使用 Apache HttpComponents 从网站下载源代码。我将下载很多(10,000 个),因此我使用多个线程来执行此操作。 有时所有线程都会死亡(加入),有
我最近从 java.net 切换过来至org.apache.http.client ,我设置了一个 ClosableHttpClient与 HttpClientBuilder 。作为连接管理器,我使用
我已经构建了一个 Storm 拓扑,它通过 kafka-spout 从 Apache-Kafka 接收元组,将此数据(使用另一个 Bolt)作为字符串写入本地系统上的 .txt 文件中,并从之后是我的
在 Android 上使用和选择发出 HTTP(S) 请求的方式是一个长期的痛苦。 虽然 Google 希望我们使用 HttpURLConnection,但有些人更喜欢使用 HttpClient。 有
我遇到了有关使用 apaches httpcomponents 进行基于证书的身份验证的问题。每次我启动请求时,我都会收到响应代码 403。如果我关闭 ssl 加密和身份验证,则可以成功建立连接。 我
根据 http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client
我正在编写一个 Java 程序,它使用 Apache-HttpComponents 加载页面并将其 HTML 打印到控制台;但是,该程序在抛出此错误之前仅打印了部分 HTML:每次我运行该程序时,在这
我想用 Apache http-components (4.1.2) 执行这个命令 curl --data "strings[]=testOne&string-keys[]=test.one&st
已结束。此问题不符合 Stack Overflow guidelines .它目前不接受答案。 我们不允许提出有关书籍、工具、软件库等方面的建议的问题。您可以编辑问题,以便用事实和引用来回答它。 关闭
我想获取网站的 html,就像使用浏览器浏览一样。 当我尝试获取网站的 html 时,它与我使用 chrome 时的情况不同,我想知道是否可以使用 apache 的 HttpComponents 发出
我目前正在尝试让 HttpComponents 发送 HttpRequest 并检索响应。在大多数 URL 上,这都可以正常工作,但是当我尝试获取 phpBB 论坛的 URL 即 http://www
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我是一名优秀的程序员,十分优秀!