2011-11-16 60 views
0

任何人都可以請幫忙閱讀使用套接字連接的內容?如何使用套接字連接讀取內容?

我用下面的代碼行:

ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); 
String message = (String) ois.readObject(); 
System.out.println("Message Received: " + message); 

問候, Monali

+0

對象 – ingsaurabh

+0

使用什麼..請建議 – Monali

+0

什麼是問題? – user370305

回答

0

試試這個代碼:

private static void trustAllHosts() 
{ 
    // Create a trust manager that does not validate certificate chains 
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() 
    { 
     public java.security.cert.X509Certificate[] getAcceptedIssuers() 
     { 
      return new java.security.cert.X509Certificate[] {}; 
     } 

     public void checkClientTrusted(X509Certificate[] chain, 
       String authType) throws CertificateException 
     {} 

     public void checkServerTrusted(X509Certificate[] chain, 
       String authType) throws CertificateException 
     {} 
    } 

    // Install the all-trusting trust manager 
    try 
    { 
     SSLContext sc = SSLContext.getInstance("TLS"); 
     sc.init(null, trustAllCerts, new java.security.SecureRandom()); 
     HttpsURLConnection 
     .setDefaultSSLSocketFactory(sc.getSocketFactory()); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 
+0

答案,你發佈的是先信任所有主機,並且不通過身份驗證而不通過socket讀取。我不想給這個-1。 – user370305

+0

你能解釋一下代碼嗎? –

0

你爲什麼要使用嘗試以下片斷

BufferedReader r = new BufferedReader(new InputStreamReader(inputStream)); 
StringBuilder total = new StringBuilder(); 
String line; 
while ((line = r.readLine()) != null) { 
    total.append(line); 
}