我在tomcat中启用了https,并为服务器身份验证设置了自签名证书.我使用Apache httpClient创建了一个http客户端.我已经设置了一个加载服务器证书的信任管理器.http客户端可以与服务器连接没问题.要查看发生了什么,我启用了调试:
System.setProperty("javax.net.debug", "ssl");
Run Code Online (Sandbox Code Playgroud)
我看到以下根本无法理解的内容:
***
adding as trusted cert:
Subject: CN=Me, OU=MyHouse, O=Home, L=X, ST=X, C=BB
Issuer: CN=Me, OU=MyHouse, O=Home, L=X, ST=X, C=BB
Algorithm: RSA; Serial number: 0x4d72356b
Valid from Sat Mar 05 15:06:51 EET 2011 until Fri Jun 03 16:06:51 EEST 2011
Run Code Online (Sandbox Code Playgroud)
我的证书会显示并添加到信任库(我看到).然后:
trigger seeding of SecureRandom
done seeding SecureRandom
Run Code Online (Sandbox Code Playgroud)
这是我没有得到的调试跟踪的部分:
trustStore is: C:\Program Files\Java\jre6\lib\security\cacerts
trustStore type is : jks
trustStore provider is :
init truststore
adding as trusted cert:
Subject: CN=SwissSign Platinum CA - G2, O=SwissSign AG, …Run Code Online (Sandbox Code Playgroud) 我需要为我们对服务(而不是Web服务)发出的Http请求设置时间.我们正在使用Apache HTTP Client.我添加了这两行代码来设置请求和响应服务的超时时间.
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 10000);
Run Code Online (Sandbox Code Playgroud)
1)目前我已将10秒设置为超时,因为我几乎立即看到来自服务的响应.我应该增加还是减少时间?
2)响应时间超过10秒会发生什么?它会抛出异常,会有什么异常吗?在下面的代码中是否还需要添加任何其他内容来设置超时.
public HashMap<String, Object> getJSONData(String url) throw Exception{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 10000);
HttpHost proxy = new HttpHost(getProxy(), getProxyPort());
ConnRouteParams.setDefaultProxy(params, proxy);
URI uri;
InputStream data = null;
try {
uri = new URI(url);
HttpGet method = new HttpGet(uri);
HttpResponse response = httpClient.execute(method);
data = response.getEntity().getContent();
}
catch (Exception e) {
e.printStackTrace();
}
Reader r = new InputStreamReader(data);
HashMap<String, Object> jsonObj = (HashMap<String, Object>) GenericJSONUtil.fromJson(r); …Run Code Online (Sandbox Code Playgroud) 我正在使用HttpClient的流畅API来发出GET请求:
String jsonResult = Request.Get(requestUrl)
.connectTimeout(2000)
.socketTimeout(2000)
.execute().returnContent().asString();
Run Code Online (Sandbox Code Playgroud)
但是对于每个请求,我收到以下警告:
apr 07, 2016 12:26:46 PM org.apache.http.client.protocol.ResponseProcessCookies processCookies
WARNING: Invalid cookie header: "Set-Cookie: WMF-Last-Access=07-Apr-2016;Path=/;HttpOnly;Expires=Mon, 09 May 2016 00:00:00 GMT". Invalid 'expires' attribute: Mon, 09 May 2016 00:00:00 GMT
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题并继续使用流畅的界面?理想情况下,我想要一个正确的方法来解决它,但因为我并不真正关心我的用例中的cookie,任何只允许我停止显示警告的解决方案(除了重定向stderr,因为我需要它)是受欢迎的.
我正在开发一个Android应用程序来访问一些battle.net(https://eu.battle.net)帐户数据(对于魔兽世界),我正在使用org.apache.http.client.HttpClient这样做.
这是我正在使用的代码:
public static final String USER_AGENT = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 (.NET CLR 3.5.30729)";
public static class MyHttpClient extends DefaultHttpClient {
final Context context;
public MyHttpClient(Context context) {
super();
this.context = context;
}
@Override
protected ClientConnectionManager createClientConnectionManager() {
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
// Register for port 443 our SSLSocketFactory with our keystore
// to the ConnectionManager
registry.register(new Scheme("https", newSslSocketFactory(), 443));
return new SingleClientConnManager(getParams(), registry); …Run Code Online (Sandbox Code Playgroud) 我正在使用Apache Commons HttpClient PostMethod 3.1.
在PostMethod类中,还有三种设置POST方法请求体的方法:
setRequestBody(InputStream body)
setRequestBody(String body)
setRequestBody(NameValuePair[] parametersBody);
Run Code Online (Sandbox Code Playgroud)
NameValuePair API
不推荐使用前两种方法.有人知道为什么吗?因为如果我想将XML放到请求体中,NameValuePair对我没有帮助.
有人知道解决方法或解决方案吗?
要在我使用的普通Java应用程序中启用apache commons HttpClient的日志记录:
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "debug");
Run Code Online (Sandbox Code Playgroud)
但在android上我没有看到LogCat中的日志.
我错过了什么吗?
我发现设置http.proxyHost并且http.proxyPort对httpClient没用.如何通过环境变量或VM参数或类似的东西强制httpClient使用代理而不更改代码?
我的gradle文件:
apply plugin: 'com.android.application'
android {
useLibrary 'org.apache.http.legacy'
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
applicationId "com.skripsi.irwanto.paud"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProauardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.11
}
Run Code Online (Sandbox Code Playgroud)
我明白了 Warning:Unable to find optional library: org.apache.http.legacy
Apache Commons HttpClient库是否支持Gzip?我们想在我们的Apache服务器上使用enable gzip压缩来加速客户端/服务器通信(我们有一个php页面,允许我们的Android应用程序与服务器同步文件).
java gzip apache-commons apache-commons-httpclient apache-httpclient-4.x
我试图将一些参数POST到服务器,但我需要设置代理.你可以帮我把它代码"设置代理"作为我的代码的一部分吗?
HttpHost proxy = new HttpHost("xx.x.x.xx");
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter("3128",proxy);
HttpPost httpost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("aranan", song));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
System.out.println("Request Handled?: " + response.getStatusLine());
in = entity.getContent();
httpclient.getConnectionManager().shutdown();
Run Code Online (Sandbox Code Playgroud)