我要做的就是下载一些JSON并将其反序列化为一个对象.我还没有下载JSON.
我能找到的几乎所有HttpClient示例,包括apache站点上的那些看起来都像......
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
public void blah() {
HttpClient client = new DefaultHttpClient();
...
}
Run Code Online (Sandbox Code Playgroud)
但是,Netbeans告诉我,这DefaultHttpClient已被弃用.我已经尝试使用谷歌搜索DefaultHttpClient deprecated和其他许多变化,我无法找到任何有用的结果,所以我显然错过了一些东西.
下载网页内容的正确Java7方式是什么?作为语言的一部分,真的没有像样的Http客户端吗?我觉得很难相信.
我对Maven的依赖是......
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>LATEST</version>
<type>jar</type>
</dependency>
Run Code Online (Sandbox Code Playgroud) 我在集成测试套件中使用commons-httpclient 3.1.HttpClient的默认日志记录非常嘈杂,我似乎无法将其关闭.我试过按照这里的说明,但没有一个有任何区别.
大多数情况下,我只需要关闭org.apache.http.wire记录器.问题的一部分是我不知道HttpClient尝试使用什么类型的记录器,大多数问题是我之前从未使用过这个库.我尝试创建一个log4j.properties文件并将其放在我的test/resources文件夹中,修改jre/lib中的master logging.properties文件,并按照日志页面上的指定将各种日志记录选项发送到Maven ,而不是它们都没有有所作为.
任何帮助表示赞赏......这让我疯狂.
更新:更正:看来有问题的输出实际上是通过jwebunit使用HttpClient而不是我自己的.无论哪种方式,这都是不可取的.
更新:感谢迄今为止的尝试.我已经尝试了下面提出的所有建议,但仍然没有运气.我在src/test/resources文件夹中有一个文件commons-logging.properties,其中包含以下内容
org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.Log4jFactory
log4j.configuration=log4j.properties
Run Code Online (Sandbox Code Playgroud)
以及具有以下内容的同一文件夹中的文件log4j.properties
log4j.rootLogger=ERROR, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%c] %m%n
#This is the line that should make httpclient shut up
log4j.logger.org.apache.http=ERROR
Run Code Online (Sandbox Code Playgroud)
但是,当我运行我的测试时,我仍然得到一堆像这样的输出:
21:57:41.413 [main] DEBUG org.apache.http.wire - << " [\r][\n]"
21:57:41.413 [main] DEBUG org.apache.http.wire - << "[\r][\n]"
21:57:41.413 [main] DEBUG org.apache.http.wire - << " [\r][\n]"
21:57:41.413 [main] DEBUG org.apache.http.wire - << " </ul>[\n]"
21:57:41.413 [main] DEBUG org.apache.http.wire - << " [\n]"
21:57:41.424 [main] DEBUG …Run Code Online (Sandbox Code Playgroud) 我知道,关于这个问题有很多不同的问题和很多答案...但我无法理解......
我有:ubuntu-9.10-desktop-amd64 + NetBeans6.7.1从"关闭"安装.代表.我需要通过HTTPS连接到某个站点.为此我使用Apache的HttpClient.
从教程我读到:
"一旦正确安装了JSSE,通过SSL进行安全的HTTP通信
就像普通的HTTP通信一样简单." 还有一些例子:
HttpClient httpclient = new HttpClient();
GetMethod httpget = new GetMethod("https://www.verisign.com/");
try {
httpclient.executeMethod(httpget);
System.out.println(httpget.getStatusLine());
} finally {
httpget.releaseConnection();
}
Run Code Online (Sandbox Code Playgroud)
到现在为止,我写道:
HttpClient client = new HttpClient();
HttpMethod get = new GetMethod("https://mms.nw.ru");
//get.setDoAuthentication(true);
try {
int status = client.executeMethod(get);
System.out.println(status);
BufferedInputStream is = new BufferedInputStream(get.getResponseBodyAsStream());
int r=0;byte[] buf = new byte[10];
while((r = is.read(buf)) > 0) {
System.out.write(buf,0,r);
}
} catch(Exception ex) {
ex.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
结果我有一组错误:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable …Run Code Online (Sandbox Code Playgroud) 我有以下内容:
final String url = "http://example.com";
final HttpClient httpClient = new HttpClient();
final PostMethod postMethod = new PostMethod(url);
postMethod.addRequestHeader("Content-Type", "application/json");
postMethod.addParameters(new NameValuePair[]{
new NameValuePair("name", "value)
});
httpClient.executeMethod(httpMethod);
postMethod.getResponseBodyAsStream();
postMethod.releaseConnection();
Run Code Online (Sandbox Code Playgroud)
它不断回来500.服务提供商说我需要发送JSON.如何使用Apache HttpClient 3.1+完成这项工作?
有一段时间,我一直在多线程环境中使用HttpClient.对于每个线程,当它启动连接时,它将创建一个全新的HttpClient实例.
最近,我发现,通过使用这种方法,它可能导致用户打开太多端口,并且大多数连接处于TIME_WAIT状态.
http://www.opensubscriber.com/message/commons-httpclient-dev@jakarta.apache.org/86045.html
因此,而不是每个线程做:
HttpClient c = new HttpClient();
try {
c.executeMethod(method);
}
catch(...) {
}
finally {
method.releaseConnection();
}
Run Code Online (Sandbox Code Playgroud)
我们计划:
[方法A]
// global_c is initialized once through
// HttpClient global_c = new HttpClient(new MultiThreadedHttpConnectionManager());
try {
global_c.executeMethod(method);
}
catch(...) {
}
finally {
method.releaseConnection();
}
Run Code Online (Sandbox Code Playgroud)
在正常情况下,global_c将同时由50个++线程访问.我想知道,这会产生任何性能问题吗?MultiThreadedHttpConnectionManager是否使用无锁机制来实现其线程安全策略?
如果10个线程正在使用global_c,那么其他40个线程是否会被锁定?
或者,如果在每个线程中我创建一个HttpClient实例,但是显式释放连接管理器会更好吗?
[方法B]
MultiThreadedHttpConnectionManager connman = new MultiThreadedHttpConnectionManager();
HttpClient c = new HttpClient(connman);
try {
c.executeMethod(method);
}
catch(...) {
}
finally {
method.releaseConnection();
connman.shutdown();
}
Run Code Online (Sandbox Code Playgroud)
connman.shutdown()会遇到性能问题吗?
对于使用50 ++线程的应用程序,我可以知道哪种方法(A或B)更好吗?
自API级别22以来,Android已经弃用了Apache模块,所以我的问题是,我如何使用,例如 HttpResponse来自Apache库,而不是Android SDK?问题是两个包都是一样的.
但是,例如,HttpGet没关系,因为它HttpGetHC4在Apache中被调用.
我需要在Android应用程序上下载一个网页,我很难决定是否使用android apache http客户端或java的URLConnection.
有什么想法吗?
首先,我不是想在这里开始一场火焰战争.我非常了解泽西岛,但很少使用httpclient.
jersey-client和Apache的httpclient之间的主要区别是什么?哪个区域比另一个好?在某处有一个很好的比较图表吗?对于较大的文件(比如2048 MB),哪一个表现更好?
非常感谢您的评论!
java apache-commons-httpclient apache-httpclient-4.x jersey-client
Duplicate files during packaging of APK app-debug-unaligned.apk放入2个jar文件时出现此错误:
httpclient-4.3.5.jar
httpmime-4.3.5.jar
进入libs文件夹后Sync with Gradle和Run.
如果用户1个jar文件 - httpmime-4.3.5.jar,我不会收到此错误.
请帮我如何避免这个错误,还可以使用上面的2个jar文件,
谢谢,
p/s:我使用Android Studio版本0.8.6.
Error Detail
错误:在APK ...\app\build\outputs\apk\app-debug-unaligned.apk打包过程中重复文件存档中的路径:META-INF/DEPENDENCIES原点1:...\app\libs\httpclient-4.3 .5.jar Origin 2:...\app\libs\httpmime-4.3.5.jar
build.gradle
android {
compileSdkVersion 20
buildToolsVersion '20.0.0'
defaultConfig {
applicationId 'com.app'
minSdkVersion 9
targetSdkVersion 20
versionCode 1
versionName '1.0'
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
packagingOptions {
exclude 'META-INF/LICENSE.txt'
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs') …Run Code Online (Sandbox Code Playgroud) 是否有一种更简单的方法来设置http客户端以进行抢先式基本身份验证,而不是此处描述的内容?
在以前的版本(3.x)中,它曾经是一个简单的方法调用(例如httpClient.getParams().setAuthenticationPreemptive(true)).
我想避免的主要是将BasicHttpContext添加到我执行的每个方法.