Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

Android Ignoring HTTPS conexion

The other day i asked a question because i had read a lot of information about how to add self/signed certificatates to an app, but i didnt know how to do it with one Signed by Verisign for example. They told me that i don't need to add any certificate to the app, only use https and that's all.

The problem is that now doing a https request, the Android phone (2.3 version) ignore the https conexion and use only http one.

I get this message on the 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.

At the same time, i have to put some credentials on the https request, so i don't really know what is the real problem and how to solve it.

my code:

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

Anyone can told me if the problem is the SSL connection or the Credentials part and how to fix it? Thanks all


EDIT: As Alvin told me, now i am using this:

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

Now the program give me this exception:

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)

The problem is a StringIndexOutOFBound when I read the message. First of all, i have to send only a String, to the server, with HttpClien i usually use this:

mRequest.setEntity(new StringEntity(request));

And the server return other String. Probably i am doing wrong the request, or i am reading bad the response.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This isn't a solution but for Android 2.3 and up you might want to look at http://developer.android.com/reference/java/net/HttpURLConnection.html as it has lessee bugs and also supports HTTPS...unless of course you have a need to do so.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...