0

前几天我问了一个问题,因为我已经阅读了很多关于如何将自签名证书添加到应用程序的信息,但我不知道如何使用 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 连接还是凭据部分以及如何解决?谢谢大家


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

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

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

4

2 回答 2

0

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

于 2013-06-07T16:13:14.250 回答
0

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

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

于 2013-06-26T13:04:17.070 回答