Android忽略HTTPS连接

Android忽略HTTPS连接,android,https,credentials,Android,Https,Credentials,前几天我问了一个问题,因为我读了很多关于如何在应用程序中添加自签名证书的信息,但我不知道如何使用Verisign签名的证书。他们告诉我,我不需要向应用程序添加任何证书,只需使用https即可 问题是,现在Android手机(2.3版)在执行https请求时,会忽略https连接,而只使用http连接 我在LogCat上看到这样的信息: I/APACHE HTTP (thCr=10) - NafHttpAuthStrategyDefault(17917): (thUse=10) NafHttpAu

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

问题是,现在Android手机(2.3版)在执行https请求时,会忽略https连接,而只使用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连接还是凭据部分,以及如何解决它? 谢谢大家


编辑: 正如阿尔文告诉我的,现在我用的是:

    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));

服务器返回另一个字符串。可能是我的请求做错了,或者是我读到了不好的回复

这不是一个解决方案,但对于Android 2.3及更高版本,您可能希望看到它有漏洞,并且还支持HTTPS…当然,除非您需要这样做。

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


我已将e凭据更改为basic,现在可以完美地工作

,因此使用HttpURLConnection应该可以工作吗?我要试试,看看会发生什么。我不确定它会不会起作用。。。但是这种方法是推荐给Android 2.3和upi的,我不会因为尝试而失去任何东西:PHi Fernando,你解决了问题吗?对不起,我还不能尝试,我现在就要尝试:P.连接似乎完成了,但是给我一个FIleNotFoundException,我知道我做请求的地方存在。也许问题在于身份验证,因为我读过,但不知道怎么说。重写问题,错误并粘贴代码OK,看编辑,是实际的代码和问题。我覆盖所有编辑,只放一个。