【问题标题】:Android Ignoring HTTPS conexionAndroid忽略HTTPS连接
【发布时间】:2013-06-07 16:03:24
【问题描述】:

前几天我问了一个问题,因为我已经阅读了很多关于如何将自签名证书添加到应用程序的信息,但我不知道如何使用 Verisign 签名的证书来做到这一点。他们告诉我,我不需要向应用程序添加任何证书,只需使用 https 即可。

问题是现在做一个https请求,Android手机(2.3版本)忽略了https conexion,只使用http一个。

我在 LogCat 上收到此消息:

I/APACHE HTTP (thCr=10) - NafHttpAuthStrategyDefault(17917): (thUse=10) NafHttpAuthStrategyDefault()
I/APACHE HTTP (thCr=10) - KeeperManager(17917): (thUse=10) INITIALIZATION of shared resources
I/APACHE HTTP (thCr=10) - AndroidContextProviderImpl(17917): (thUse=10)    currentActivityThread=null
I/APACHE HTTP (thCr=10) - NafHttpAuthStrategyDefault(17917): (thUse=10)    cached value : gbaSupportIsPossible=null
I/APACHE HTTP (thCr=10) - NafHttpAuthStrategyDefault(17917): (thUse=10)    The current context is NOT a context of GBA service.
I/APACHE HTTP (thCr=10) - GbaSupportPermissionRequestCheckerImpl(17917): (thUse=10) isCurrentProcessRequestedGba()#finished   result=false
I/APACHE HTTP (thCr=10) - GbaSupportPermissionRequestCheckerImpl(17917): (thUse=10) isCurrentProcessAllowedToUseGba()#started   result=false
I/APACHE HTTP (thCr=10) - NafHttpAuthStrategyDefault(17917): (thUse=10)    The GBA permission wasn't requested for this process.
I/APACHE HTTP (thCr=10) - NafHttpAuthStrategyDefault(17917): (thUse=10) It is impossible to support GBA now (many possible reasons: no Android Context, current client is GBA service, etc.), then it will be just usual HTTP.
I/APACHE HTTP (thCr=10) - NafRequestExecutorWrapperRedirectionHandler(17917): (thUse=10)    It isn't GBA flow, redirection responses are not handled.

同时,我必须在 https 请求上放一些凭据,所以我真的不知道真正的问题是什么以及如何解决它。

我的代码:

String url="https://"+server;
CredentialsProvider credProvider = new BasicCredentialsProvider();
credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),new UsernamePasswordCredentials(username,password));

DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.setCredentialsProvider(credProvider);
HttpPost mRequest = new HttpPost(url);
mRequest.setEntity(new StringEntity(request));
HttpResponse httpResponse= httpclient.execute(mRequest);

任何人都可以告诉我问题是 SSL 连接还是凭据部分以及如何解决? 谢谢大家


编辑: 正如 Alvin 告诉我的,现在我正在使用这个:

    URL url;
    HttpURLConnection conn;

    try{
        url=new URL("https://"+server);
        String param=params;
        Authenticator.setDefault(new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("username", "password".toCharArray());             
            };
        });
        conn=(HttpURLConnection)url.openConnection();
        conn.setDoOutput(true);
        conn.setFixedLengthStreamingMode(param.getBytes().length);
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        PrintWriter out = new PrintWriter(conn.getOutputStream());
        out.print(param);
        out.close();
        String response= "";
        Scanner inStream = new Scanner(conn.getInputStream());
        while(inStream.hasNextLine())
            response+=(inStream.nextLine());
        Log.d("Response",response);
    }
    //catch some error
    catch(MalformedURLException ex){
        ex.printStackTrace();
    }catch(IOException ex){
        ex.printStackTrace();
    }catch(Exception e){
        e.printStackTrace();
    }

现在程序给了我这个例外:

06-11 08:57:47.396: W/System.err(341): java.lang.StringIndexOutOfBoundsException
06-11 08:57:47.396: W/System.err(341):  at java.lang.String.substring(String.java:1579)
06-11 08:57:47.396: W/System.err(341):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getAuthorizationCredentials(HttpURLConnectionImpl.java:1769)
06-11 08:57:47.396: W/System.err(341):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequestInternal(HttpURLConnectionImpl.java:1701)
06-11 08:57:47.407: W/System.err(341):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequest(HttpURLConnectionImpl.java:1649)
06-11 08:57:47.407: W/System.err(341):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1153)
06-11 08:57:47.415: W/System.err(341):  at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:253)
06-11 08:57:47.426: W/System.err(341):  at com.example.ssltest.MainActivity.onClick(MainActivity.java:89)
06-11 08:57:47.426: W/System.err(341):  at android.view.View.performClick(View.java:2408)
06-11 08:57:47.426: W/System.err(341):  at android.view.View$PerformClick.run(View.java:8816)
06-11 08:57:47.426: W/System.err(341):  at android.os.Handler.handleCallback(Handler.java:587)
06-11 08:57:47.426: W/System.err(341):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-11 08:57:47.436: W/System.err(341):  at android.os.Looper.loop(Looper.java:123)
06-11 08:57:47.436: W/System.err(341):  at android.app.ActivityThread.main(ActivityThread.java:4627)
06-11 08:57:47.446: W/System.err(341):  at java.lang.reflect.Method.invokeNative(Native Method)
06-11 08:57:47.446: W/System.err(341):  at java.lang.reflect.Method.invoke(Method.java:521)
06-11 08:57:47.446: W/System.err(341):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-11 08:57:47.446: W/System.err(341):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-11 08:57:47.446: W/System.err(341):  at dalvik.system.NativeStart.main(Native Method)

当我阅读消息时,问题是 StringIndexOutOFBound。首先,我只需要向服务器发送一个字符串,使用 HttpClien 我通常使用这个:

mRequest.setEntity(new StringEntity(request));

并且服务器返回其他字符串。可能我做错了请求,或者我读错了响应。

【问题讨论】:

  • 嗨费尔南多,你解决了这个问题吗?
  • 对不起,我还不能尝试,我现在要尝试:P.
  • 它似乎连接已完成,但给我一个 FIleNotFoundException 并且我知道我在哪里执行请求。也许问题出在身份验证上,因为我读过它,但不知道如何表达。
  • 改写问题,错误并粘贴代码
  • 好的,看看编辑,是实际的代码和问题。我覆盖了所有的编辑,只放了一个。

标签: android https credentials


【解决方案1】:

这不是解决方案,但对于 Android 2.3 及更高版本,您可能需要查看 http://developer.android.com/reference/java/net/HttpURLConnection.html,因为它有承租人错误并且还支持 HTTPS...除非您当然需要这样做。

【讨论】:

  • 那么使用 HttpURLConnection 应该可以吗?我要试一试,看看会发生什么。
  • 我不确定它是否会起作用...但建议在 Android 2.3 及更高版本中使用此方法
  • 我不会因为尝试而失去任何东西:P
【解决方案2】:

真正的问题是服务器需要 NTLM 凭据,所以当它收到响应时,我的程序不理解它并发送 StringIndexOutOfBound

我已将 e 凭据更改为基本凭据,现在可以完美运行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 2023-03-03
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 2020-01-20
    • 2020-12-02
    相关资源
    最近更新 更多