前几天我问了一个问题,因为我已经阅读了很多关于如何将自签名/签名证书添加到应用程序的信息,但我不知道如何使用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));
服务器返回其他String。可能我做错了请求,或者我读错了回复。
答案 0 :(得分:0)
这不是一个解决方案,但对于Android 2.3及更高版本,您可能需要查看http://developer.android.com/reference/java/net/HttpURLConnection.html,因为它有丢失的错误并且还支持HTTPS ...除非您当然需要这样做。< / p>
答案 1 :(得分:0)
真正的问题是服务器需要NTLM Credentials,所以当它收到响应时,我的程序不理解它发送StringIndexOutOfBound
我已将e凭据更改为基本,现在可以完美运行