我一直在试着弄清楚我的头发:我正在制作一个HttpsURLConnection并使用java.net.cookiemanager来管理我的cookie(根据我的理解,没有办法使用android.webkit.cookiemanager来HttpUrlConnection/HttpsUrlConnection? ).我需要将我的长时间cookie保存到以后的连接中.
遗憾的是,我无法使用http://loopj.com/android-async-http/并且它是PersistentCookieStore,因为我需要允许不受信任的证书(使用http://abhinavasblog.blogspot.se/2011/07/allow-untrusted -certificate-for-https.html).我已经尝试过单独使用他们的PersistentCookieStore但是他们正在使用apache cookie而我正在使用java.net cookies ...
这就是我尝试过的:
cManager = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
private void setSharedPreferences(){
List<HttpCookie> cookies = cManager.getCookieStore().getCookies();
if (cookies.isEmpty()) {
Log.d(tag,"no cookies received");
} else {
for (int i = 0; i < cookies.size(); i++) {
if(cookies.get(i).getName().equals("rememberMe")) {
editor.putString(
"rememberMe", cookies.get(i).toString());
editor.commit();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我在下次应用启动时检索cookie时:
SharedPreferences sharedPreferences = context.getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
String rememberString = sharedPreferences.getString("rememberMe", "none");
if (!rememberString.equals("none")) {
Log.d("rememberME är inte", "none!");
URI uriToCookie = null;
try { …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用由其他人编写的GUI Java程序,这种程序不像我期望的那样.在程序运行过程中,它与服务器联系以提取数据,但它没有显示正确的数据.我正在尝试确定我遇到的问题是否与网络相关 - 即正确的位从未进入客户端.
查看Java程序的源代码,它似乎使用HttpsURLConnection类通过SSL从服务器提取数据.我希望能够做的是使用Wireshark检查线路上发生的事情.我的理解是,如果你有相关的密钥,Wireshark支持解密一些SSL流量.我不控制服务器,因此无法访问其私钥.但是我控制程序运行的客户端.我的问题是,在实际中我如何配置wireshark来解密我刚才描述的场景的SSL流量?是否有一个特定的密钥HttpsURLConnection使用我可以添加到Wireshark?还有别的吗?
我应该注意到我已经考虑过简单地将日志记录添加到Java代码库中,但最终会更喜欢数据包捕获的基本事实而不是日志记录,因为我可能会错过/忽略在记录代码库时重要的事情我不完全了解.
我正在使用 HttpsURLConnection 向服务器发出服务请求,如下面的代码:
URL url = new URL("service/url");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setConnectionTimeout(300000);
connection.setReadTimeout(300000);
parse (connection.getInputStream());
Run Code Online (Sandbox Code Playgroud)
服务有时可能需要更长的时间,所以理想情况下我应该期待超时异常,但客户端正在重试并再次发送相同的请求。有没有办法明确禁用任何类型的重试?而且我什至不确定设置的超时方法是否有任何区别。
我正在使用 Java 1.6
更新
我尝试了 connect() 和 getResponseCode() 而不是 getInputStream() 但相同的行为:
URL url = new URL("service/url");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setConnectionTimeout(300000);
connection.setReadTimeout(300000);
connection.connect();
connection.getResponseCode();
Run Code Online (Sandbox Code Playgroud)
即使这是提出 2 个请求。
更新
HttpClient修复了该问题。在 HttpClient 中,您可以明确地将重试设置为 false
我有这样的代码:
// configure the SSLContext with a TrustManager
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(new KeyManager[0],
new TrustManager[] {new DefaultTrustManager()},
new SecureRandom());
SSLContext.setDefault(ctx);
URL url = new URL(urlString); // https://abc.myhost.com
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
System.out.println("verify:" + arg0);
return true;
}
});
System.out.println("HTTP status: " + conn.getResponseCode());
Certificate[] certs = conn.getServerCertificates();
int c=0;
for (Certificate cert : certs){
String t = cert.getType();
System.out.println(String.format("\ncert[%d]: %s",c,t));
c++;
if (pi.verbose) {
System.out.println(cert);
}
else …Run Code Online (Sandbox Code Playgroud) 我有一个JEditorPane通过 SSL/TLS 加载网站的程序。我的目标是(通过反射)获取 SSL 连接的特定信息,例如 SSL 握手的完成消息。我从中得到的唯一东西JEditorPane是URL,因此HttpsURLConnection.
但我如何获得SSLSocket的HttpsURLConnection?有任何想法吗?
那里.我需要与https://free.temafon.ru建立https连接,但我在Android 2.3及更低版本上有CertPathValidatorException.我做了什么.
Init ssl上下文:
final KeyStore keystore = KeyStore.getInstance("BKS");
keystore.load(getResources().openRawResource(R.raw.temafon),
"W0d3Uoa5PkED".toCharArray());
final TrustManager trustManager = new TemafonTrustManager(keystore);
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[] { trustManager }, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext
.getSocketFactory());
Run Code Online (Sandbox Code Playgroud)
在这里,我使用自定义TrustManager,因为服务器以错误的顺序发送证书.
这段代码在Android 4.0上工作正常,但2.3在 java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.我正在做的事情上失败了吗?
我已经创建了一个测试项目,可以在这里找到.
我正在测试一个代码示例,但它总是出错 connection.setDoInput(true);
HttpsURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String urlServer = "https://www.myurl.com/upload.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead = 0;
int bytesAvailable = 0;
int bufferSize = 0;
byte[] buffer = null;
int maxBufferSize = 1*1024*1024;
try {
FileInputStream fileInputStream = new FileInputStream(new File(params[0]));
URL url = new URL(urlServer);
connection = (HttpsURLConnection) url.openConnection();
connection.setConnectTimeout(1000);
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method …Run Code Online (Sandbox Code Playgroud) 所以我目前正在编写一个 android 应用程序,它将使用 Twitter 的 1.1 Application-only-auth从特定用户获取推文,这需要一个 POST HTTP 请求来获取承载令牌。我设置了一个使用 anHttpsURLConnection来打开连接的方法,但是
conn.setDoOutput(true); 方法不起作用conn.doOutput 停留 falseGET。难道我做错了什么?
这个方法在 anAsyncTask的方法中调用doInBackground():
public String getRequestBearerToken(String endUrl) throws IOException {
String encodedCred = encodeKeys("API-key", "API-secret");
HttpsURLConnection connection = null;
try {
URL url = new URL(endUrl);
connection = (HttpsURLConnection) url.openConnection();
connection.setInstanceFollowRedirects(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
// POST request properties
connection.setRequestProperty("Host", "api.twitter.com");
connection.setRequestProperty("User-Agent", getResources()
.getString(R.string.app_name));
connection.setRequestProperty("Authorization", "Basic "
+ encodedCred);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
connection.setRequestProperty("Content-Length", "29");
connection.setUseCaches(false);
...
Run Code Online (Sandbox Code Playgroud) 这是我到目前为止使用的代码:
URL request = new URL(url);
HttpsURLConnection urlConnection = (HttpsURLConnection) request.openConnection();
urlConnection.setHostnameVerifier(new StrictHostnameVerifier());
Run Code Online (Sandbox Code Playgroud)
但是,根据API级别22 ,不推荐使用StrictHostnameVerifier.
API级别22中不推荐使用此接口.
请改用openConnection().请访问此网页了解更多详情.
事情是我已经在使用openConnection了StrictHostnameVerifier.我仍然可以使用它,但我看到一个不赞成的警告就行了
urlConnection.setHostnameVerifier(new StrictHostnameVerifier());
Run Code Online (Sandbox Code Playgroud)
错误在哪里或缺少什么?
PS:重新发明轮子并实施StrictHostnameVerifier显然是不可能的.
编辑:有关新API的新内容吗?
更新到android 5.0棒棒糖后,HttpsURLConnection发生握手错误.
05-05 14:54:08.821 10855-11793/com.soonoo.mobilecampus E/INFO? javax.net.ssl.SSLHandshakeException: Handshake failed
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:374)
at com.android.okhttp.Connection.upgradeToTls(Connection.java:238)
at com.android.okhttp.Connection.connect(Connection.java:158)
at com.android.okhttp.Connection.connect(Connection.java:170)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:309)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:242)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:388)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:220)
at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
at com.soonoo.mobilecampus.User.login(User.java:72)
at com.soonoo.mobilecampus.LoginView$Login.doInBackground(LoginView.java:112)
at com.soonoo.mobilecampus.LoginView$Login.doInBackground(LoginView.java:84)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xaaaf8a00: Failure in SSL library, usually a protocol error
error:1407743E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert inappropriate fallback (external/openssl/ssl/s23_clnt.c:765 0xa9295b25:0x00000000)
at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:302)
... 19 …Run Code Online (Sandbox Code Playgroud)