2013-11-23 16 views
5

Sto tentando di inviare una richiesta da localhost a un server ma restituisce l'errore seguente.Come inviare richieste su ssl?

javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection? 

Codice

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.net.URL; 
import javax.net.ssl.HttpsURLConnection; 
import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.Marshaller; 


public class Test { 

public void connectMyServer(){ 

     Login auth = new Login("username", "password"); 

     JAXBContext cntx = JAXBContext.newInstance(Login.class); 
     Marshaller m = cntx.createMarshaller(); 
     m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); 

     try { 
      URL url = new URL("https://www.server.com/requests"); 
      HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); 

      con.setRequestMethod("POST"); 
      con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
      con.setDoOutput(true); 
      con.setDoInput(true); 

      OutputStream os = con.getOutputStream(); 
      m.marshal(auth, os); 
      m.marshal(auth, System.out); 

      os.flush(); 
      con.getResponseCode(); 

      BufferedReader in = new BufferedReader(new InputStreamReader(
        con.getInputStream())); 

      String inputLine; 
      while ((inputLine = in.readLine()) != null) { 
       System.out.println(inputLine); 
      } 
      in.close(); 
      con.disconnect(); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
+0

Provare HttpsURLConnection invece di HttpURLConnection – smeaggie

+0

@smeaggie L'ho modificato in https ma viene eseguito sempre nello stesso errore –

+0

Sono con Jim su questo; il server non invia quindi HTTPS. – smeaggie

risposta

1

Si sta tentando di utilizzare HttpURLConnection per SSL quando si dovrebbe utilizzare HttpsURLConnection.

Edit:

ho provato questo:

import java.net.URL; 
import java.util.List; 
import java.util.Map; 

import javax.net.ssl.HttpsURLConnection; 

public class Test 
{ 

    public static void main(String[] args) throws Exception 
    { 
     URL url = new URL("https://www.google.com"); 
     HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); 
     Map<String,List<String>> fields = con.getHeaderFields(); 
     con.disconnect(); 
    } 
} 

Corre senza alcun problema.

Il tuo messaggio di errore indica che il server remoto risponde come se parla HTTP, HTTPS e non, indipendentemente da ciò che dice il tuo team tecnico.

+0

ho cambiato in https, ma corre ancora in stesso errore –

+0

è il server remoto reindirizzamento a HTTP? Hai guardato il traffico con Wireshark per vedere cosa viene inviato e ricevuto? –

+0

team tecnico server mi ha consigliato di inviare tutti i collegamenti a https, quindi supose che non destinarli a http –

Problemi correlati