标签: httpsurlconnection

HttpsURLconnection发布并获取Android

我正在开发一个简单的应用程序,它使用https协议从服务器发布和获取数据.我在互联网上搜索但是资源很少,我尝试了大部分资源但是无法成功完成.

我尝试使用HttpClient它是成功的,但我想用HttpsURLconnection来做

我是否需要从设备中获取公共RSA密钥,如果是这样,我该怎么做呢.

有人能告诉我如何才能实现这一目标httpsURLconnection.

protected String doInBackground(String... arg0) {     
  try {
    ByteArrayInputStream derInputStream = new ByteArrayInputStream(app.certificateString.getBytes());
    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509","BC");
    X509Certificate cert = (X509Certificate) certificateFactory.generateCertificate(derInputStream);
    String alias = "alias";//cert.getSubjectX500Principal().getName();

    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    trustStore.load(null);
    trustStore.setCertificateEntry(alias, cert);
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
    kmf.init(trustStore, null);
    KeyManager[] keyManagers = kmf.getKeyManagers();

    TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
    tmf.init(trustStore);
    TrustManager[] trustManagers = tmf.getTrustManagers();

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(keyManagers, trustManagers, null);
    URL url = new URL("MY HTTPS URL");
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); …
Run Code Online (Sandbox Code Playgroud)

sockets android ssl-certificate httpsurlconnection

11
推荐指数
1
解决办法
592
查看次数

异常后使用java http连接实例

以下代码是否安全:

try {
    URL url = new URL(urlRequest);
    conn = (HttpURLConnection)url.openConnection();
    conn.setConnectTimeout(30000);
    conn.setReadTimeout(30000);
    conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
    String encoding = conn.getContentEncoding();
    return Utils.wrapCompressedStream(conn.getInputStream(), encoding);
} catch (IOException e) {
    if(conn != null) {
        conn.getContentEncoding();
        conn.getErrorStream();
        conn.whateverOtherMethodThere();
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

特别是,在InterruptedIOException(例如,读取超时)的情况下调用类似的方法getContentEncoding()是否安全?据我所知,这种方法需要实时连接来读取HTTP(S)头.

更新(附加信息):

这个问题源于真实的系统体验.我相信,系统是在Oracle/Sun JVM 1.6上运行的.代码几乎相同:

...    
} catch (IOException e) {
    if(conn != null) {
         try {
             String response = tryGetResponse(conn);
...
Run Code Online (Sandbox Code Playgroud)

在这个问题发生tryGetResponseHTTPS请求:

 private static String tryGetResponse(HttpURLConnection conn) {
    if(conn == null) return "(failed to get)"; …
Run Code Online (Sandbox Code Playgroud)

java urlconnection httpurlconnection httpsurlconnection

9
推荐指数
1
解决办法
1409
查看次数

HttpsURLConnection和Cookies

基本上我只想从服务器获取cookie.这里是简单的代码,但我想这并不足以获得每个cookie,例如sessionID等.

import java.net.*;
import java.util.*;

    public class Cookie {

    public static void main(String[] args) {

        try {
            URL url = new URL("http://www.google.com/");
            URLConnection conn = url.openConnection();
            System.out.println(con.getHeaderField("Set-Cookie"));

        } catch (Exception e) { }
    }
}
Run Code Online (Sandbox Code Playgroud)

结果显示我只获得一个cookie:

NID=61=nNSsAl4g7DfxqHE7t__ghfoCc_J-RmY7PaTNoPOd_khLdvvuqI-fnteHgnQXMzmxj_C5HdMozcGfOTt8PvePLKpbDdUlzdVZiRKwNpQIej6_T69jt9C7Oi6E4F-gWPHD; expires=Mon, 14-Jan-2013 17:16:40 GMT; path=/; domain=.google.pl; HttpOnly
Run Code Online (Sandbox Code Playgroud)

Firefox获得NID和PREF,因此无法正常工作.

有没有办法获得PREF cookie?

任何人都可以告诉我这是发送POST的正确方法(下面的代码).

import java.io.*;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;

public class Cookie_Example {

    public static void main(String[] args){

        try{
            String https_url = "https://ssl.filmweb.pl/j_login";
            URL url = new URL(https_url);
            String …
Run Code Online (Sandbox Code Playgroud)

java cookies http-post httpsurlconnection

8
推荐指数
1
解决办法
1万
查看次数

Android 4.x中的SSL客户端身份验证

我想创建一个连接到服务器的应用程序.此服务器使用SSL客户端身份验证 应用程序的用户应该能够选择证书并允许使用它,就像它在浏览器应用程序中实现一样.

在浏览器应用程序中,身份验证按预期工作,因此我使用的证书是有效的.

当我尝试在我的应用程序中连接时,我收到以下错误:

javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException:
SSL handshake terminated: ssl=0x2a2d3b38:
Failure in SSL library, usually a protocol error
error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure
(external/openssl/ssl/s3_pkt.c:1290 0x2a2df880:0x00000003)
Run Code Online (Sandbox Code Playgroud)

我试着按照我的实现的android文档.

这是我的示例活动的代码:

public class ClientCertificateActivity extends Activity implements
    KeyChainAliasCallback {

protected static final String TAG = "CERT_TEST";
private String alias;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    choseCertificate();
    LinearLayout layout = new LinearLayout(this);
    Button connectToServer = new Button(this);
    connectToServer.setText("Try to connect to Server");
    connectToServer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    connectToServer.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View …
Run Code Online (Sandbox Code Playgroud)

authentication https android ssl-certificate httpsurlconnection

7
推荐指数
1
解决办法
6184
查看次数

Android自签名证书:未找到证书路径的信任锚

我知道这个主题在很多地方都有讨论,但在我经历了几乎所有这些之后,我决定创建我的第一个StackOverflow问题......

问题如下:

我想连接到使用证书来限制访问的安全Web服务(https),以及用于验证用户身份的用户名/密码.所以我有一个客户端证书(p12文件)和一个服务器证书(pem或der文件).我尝试使用HttpURLConnection类,因为从我所听到的,Android上将不再支持Apache库.

所以这是我的实现(serverCert和clientCert是我的文件的完整路径):

        // Load CAs from our reference to the file
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        InputStream caInput = new BufferedInputStream(new FileInputStream(serverCert));
        X509Certificate serverCertificate;

        try {
            serverCertificate = (X509Certificate)cf.generateCertificate(caInput);
            System.out.println("ca=" + serverCertificate.getSubjectDN());
        } finally {
            caInput.close();
        }
        Log.d(TAG, "Server Cert: " + serverCertificate);

        // Create a KeyStore containing our trusted CAs
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null);
        trustStore.setCertificateEntry("my ca", serverCertificate);

        //Load the Client certificate in the keystore
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        FileInputStream fis = new FileInputStream(clientCert);
        keyStore.load(fis,CLIENT_PASSWORD);

        // Create a …
Run Code Online (Sandbox Code Playgroud)

ssl android certificate sslhandshakeexception httpsurlconnection

7
推荐指数
1
解决办法
4398
查看次数

使用Android的证书进行SSL客户端身份验证

我正在编写一个Android应用程序,它连接到服务器以调用一些Web服务.此服务器使用自签名SSL证书,并需要客户端证书进行身份验证.

当我使用Android Chrome浏览器或使用Iphone的Safari浏览器连接到服务器时,它可以完美运行.SSL连接正在建立,客户端证书身份验证正在成功并重定向到我的Web服务.但是当我与其他浏览器连接时,例如股票android浏览器,海豚等,它会导致网络超时.

此外,当我连接我的Android应用程序时,我收到以下错误:

javax.net.ssl.SSLException: Read error: ssl=0x12746b8: I/O error during system call, Connection reset by peer
at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_read(Native Method)
at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:671)
at libcore.io.Streams.readSingleByte(Streams.java:41)
at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:655)
at libcore.io.Streams.readAsciiLine(Streams.java:201)
at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:544)
Run Code Online (Sandbox Code Playgroud)

我关注了chariotsolutions的博客:http: //chariotsolutions.com/blog/post/https-with-client-certificates-on/

我还使用portecle GUI将客户端证书转换为BKS格式.

这是我的代码:

private static final String USER_AGENT = "Mozilla/5.0 (Linux; U; Android 4.0.3; et-ee; GT-P5100 Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30";

....

@Override
protected Object doInBackground(String... urlString) {
    ....
    URL url = new URL(urlString[0]);
    HttpsURLConnection urlConnection =(HttpsURLConnection)url.openConnection();
    urlConnection.setSSLSocketFactory(getSSLContext().getSocketFactory());
    urlConnection.addRequestProperty("User-Agent", USER_AGENT);
    urlConnection.setDoInput(true);   
    urlConnection.connect();
    ....
} …
Run Code Online (Sandbox Code Playgroud)

authentication https android httpsurlconnection

7
推荐指数
0
解决办法
9531
查看次数

系统调用期间的I/O错误,管道损坏

我知道这个问题引起了很多,但我没有找到任何解决方案.我正在尝试使用https上传大文件,但我得到异常"系统调用期间的I/O错误,断管"当我使用http上传它时,相同的代码工作.

我读到这个问题是在Android 2.3上发现的,我使用的是Android 4.3但它仍然会发生......

当我上传小文件时也会发生这种情况.如果删除con.setChunkedStreamingMode(1024),它不会发生在小文件中; 并将文件上传到一个块(我不能用大文件).

android httpurlconnection httpsurlconnection

7
推荐指数
1
解决办法
1603
查看次数

信任存储中的根证书是否足以建立连接?

信任库中的根证书足以建立与网站的连接吗?如果是这样,只是为了测试,我已将谷歌的根证书导入到我创建的新信任存储中并指向该信任存储。即使这样我也遇到了以下异常。 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

但如果我尝试使用默认的 java 信任存储连接到谷歌,那么它工作正常。有人可以帮我解决这个问题吗?TIA。

java ssl httpsurlconnection

7
推荐指数
2
解决办法
8902
查看次数

从URL连接请求获取SSL证书详细信息

在android中,我想建立一个到HTTPS URL的URL连接.连接打开后,我希望能够从服务器读取证书并获取已发布的通用名称和组织字符串?

这可能吗?如果是这样,最好的方法是什么?

到目前为止,我一直在使用HttpsUrlConnection:

HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.getInputStream();
X509Certificate cert = (X509Certificate) urlConnection.getServerCertificates()[0];
Run Code Online (Sandbox Code Playgroud)

但在此之后,我不确定如何获得具体的证书细节?如果这是正确的方法,我甚至不确定.

android urlconnection httpclient httpsurlconnection

6
推荐指数
0
解决办法
761
查看次数

获取对Java的默认http(s)URLStreamHandler的引用

我有一个我需要在我的一个项目中使用的库,不幸的是它注册了自己URLStreamHandler来处理http- URLs.有没有办法获得对Java的默认http-和https-的引用URLStreamHandlers,所以我可以在URL构造函数中指定其中一个来打开标准的http连接而不使用库覆盖的协议?

java urlconnection httpurlconnection httpsurlconnection

6
推荐指数
1
解决办法
1万
查看次数