1

我正在尝试向我的应用程序添加证书,以便它可以访问服务器。我已经阅读了一些博客并相应地实现了。当我测试它时,它给我带来了错误。这是我得到的logcat

09-14 11:44:48.766: W/System.err(24802): java.io.IOException: SSL handshake failure: I/O error during system call, Connection reset by peer
09-14 11:44:48.774: W/System.err(24802): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.nativeconnect(Native Method)
09-14 11:44:48.774: W/System.err(24802): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:316)
09-14 11:44:48.774: W/System.err(24802): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.getSecureSocket(HttpConnection.java:168)
09-14 11:44:48.784: W/System.err(24802): at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:399)
09-14 11:44:48.784: W/System.err(24802): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:1373)
09-14 11:44:48.784: W/System.err(24802): at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:117)
09-14 11:44:48.784: W/System.err(24802): at com.wheelz.connection.methods.GetConnectionMethod_JavaNet.send(GetConnectionMethod_JavaNet.java:176)
09-14 11:44:48.784: W/System.err(24802): at com.wheelz.connection.HttpRequest.getMethod(HttpRequest.java:182)
09-14 11:44:48.784: W/System.err(24802): at com.wheelz.connection.HttpRequest.sendRequest(HttpRequest.java:141)
09-14 11:44:48.794: W/System.err(24802): at com.wheelz.connection.HttpRequest.send(HttpRequest.java:123)
09-14 11:44:48.794: W/System.err(24802): at com.wheelz.activity.SplashScreen$2.run(SplashScreen.java:73)
09-14 11:44:48.794: W/System.err(24802): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
09-14 11:44:48.794: W/System.err(24802): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
09-14 11:44:48.804: W/System.err(24802): at java.lang.Thread.run(Thread.java:1102)
09-14 11:44:48.804: E/VERSION :(24802): UNAUTHORIZED
09-14 11:44:48.804: W/System.err(24802): org.json.JSONException: Value UNAUTHORIZED of type java.lang.String cannot be converted to JSONObject
09-14 11:44:48.814: W/System.err(24802): at org.json.JSON.typeMismatch(JSON.java:107)
09-14 11:44:48.814: W/System.err(24802): at org.json.JSONObject.<init>(JSONObject.java:158)
09-14 11:44:48.814: W/System.err(24802): at org.json.JSONObject.<init>(JSONObject.java:171)
09-14 11:44:48.814: W/System.err(24802): at com.wheelz.activity.SplashScreen$2.run(SplashScreen.java:76)
09-14 11:44:48.824: W/System.err(24802): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
09-14 11:44:48.824: W/System.err(24802): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
09-14 11:44:48.824: W/System.err(24802): at java.lang.Thread.run(Thread.java:1102)

为什么我得到“不受信任的服务器”,而我就是一切?

这是我使用的代码

public class GetConnectionMethod_JavaNet {

private String url;
private int STATUS_CODE = -1;
private String requestData;
private int isContentChanged = Constants.NO_NEW_CONTENT_AVAIL;  //304 means no change in content
private HttpsURLConnection urlConnection;
//private DefaultHttpClient httpclient;
private InputStream inputStream ;
public SSLContext sslContext;
PreferenceManagerUtil pmUtil = new PreferenceManagerUtil();
public GetConnectionMethod_JavaNet(String url, String requestData ) {

 this.url = url;
 this.requestData = requestData;

}

/**
 * setting the headers
 */
private void setHeaders () {

 String FBToken = pmUtil.getPreferenceValue(Constants.FBTOKEN, "FACEBOOKTOKEN");

 urlConnection.setRequestProperty("Connection","Keep-Alive");
 urlConnection.addRequestProperty(Constants.APIVERSION, Constants.APIVERSIONVALUE);
 urlConnection.addRequestProperty(Constants.PLATFORM, Constants.PLATFORMVALUE);
 urlConnection.addRequestProperty(Constants.FACEBOOK_TOKEN,FBToken);
 urlConnection.addRequestProperty(Constants.CONTENT_TYPE, Constants.APPLICATIONJSON );
}

/**
 * Trust every server - dont check for any certificate
 */
private static void trustAllHosts() {

 KeyStore trustStore = null;
 try {
  trustStore = KeyStore.getInstance("BKS");
 } catch (KeyStoreException e2) {
  // TODO Auto-generated catch block
  e2.printStackTrace();
 }
 try {
  trustStore.load(getApplicationContext().getResources().openRawResource(R.raw.my_certificate),
         "mypassword".toCharArray());
 } catch (NoSuchAlgorithmException e1) {
  // TODO Auto-generated catch block
  e1.printStackTrace();
 } catch (CertificateException e1) {
  // TODO Auto-generated catch block
  e1.printStackTrace();
 } catch (NotFoundException e1) {
  // TODO Auto-generated catch block
  e1.printStackTrace();
 } catch (IOException e1) {
  // TODO Auto-generated catch block
  e1.printStackTrace();
 }
        TrustManagerFactory trustMgr = null;
   try {
    trustMgr = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
   } catch (NoSuchAlgorithmException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
        try {
    trustMgr.init(trustStore);
   } catch (KeyStoreException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }


               try {
                SSLContext sc = SSLContext.getInstance("TLS");

                sc.init(null, trustMgr.getTrustManagers(), new java.security.SecureRandom());
                HttpsURLConnection
                                .setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (Exception e) {
                e.printStackTrace();
        }
}







 public Object send ( int returnType ) {

 try {
  URL url = new URL(this.url);

   System.setProperty("http.keepAlive", "false");
   trustAllHosts();
  if ( urlConnection == null ) {
   urlConnection = (HttpsURLConnection) url.openConnection();//Without Proxy
  }

urlConnection.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
  setHeaders();

  STATUS_CODE = urlConnection.getResponseCode();


  if(STATUS_CODE == 201  || STATUS_CODE == 200 ){
   inputStream = urlConnection.getInputStream();
  }else{

   inputStream = urlConnection.getErrorStream();
  }  

  switch ( returnType ) {

  case Constants.TYPE_BYTE :   return null;
  case Constants.TYPE_STREAM:  return null;
  case Constants.TYPE_STRINGBUFFER :  

   if ( inputStream != null) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream),8 * 1024);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
     sb.append(line + "\n");
    }
    return new StringBuffer ( sb.toString() );
   }
  }

  return null;
 } catch ( UnknownHostException e ) {
  e.printStackTrace();
  STATUS_CODE = 001;

 } catch (IOException e) {
  e.printStackTrace();
  return new StringBuffer("UNAUTHORIZED");  

 } catch ( Exception e ) {
  e.printStackTrace();
 }catch (Error e) {
  e.printStackTrace();
 }

 finally {
  try{
   // release the memory
   if(urlConnection!=null)
    urlConnection.disconnect();
  }catch (Exception e) {
  }
 }

 return null;
}

public int getStateCode(){

 return STATUS_CODE;
}

public int isNewContentAvaialable(){

 return isContentChanged;
}



}
4

0 回答 0