Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 15
  1. #1
    Utente di HTML.it
    Registrato dal
    Dec 2010
    Messaggi
    181

    Server returned HTTP response code: 500 for URL

    Ciao a tutti, qualcuno saprebbe aiutarmi riguardo questa eccezione?

    Server returned HTTP response code: 500 for URL:

    java.io.IOException: Server returned HTTP response code: 500 for URL:

    Ho creato un java project e ho utilizzato questo codice:

    Codice PHP:

    package restfull
    ;


    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.Serializable;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;

    import javax.net.ssl.HostnameVerifier;
    import javax.net.ssl.HttpsURLConnection;
    import javax.net.ssl.SSLSession;
    /**
     * Main.java
     *
     * @author [url]www.javadb.com[/url]
     */
    public class Main implements Serializable{

        
    /**
         *
         */
        
    private static final long serialVersionUID 1L;

        
    /**
         * Extends the size of an array.
         */
        
    public void sendPostRequest() {

            
    //Build parameter string
            
    String data "nome=pluto&cognome=pippo";
            try {

                
    // Send the request
                
    URL url = new URL("https://ip");
                
    //URLConnection conn = url.openConnection();
                //HttpsURLConnection.setDefaultHostnameVerifier ( new NullHostnameVerifier ()) ;
                
    HttpsURLConnection conn = ( HttpsURLConnection url.openConnection () ;

                
    conn.setDoOutput(true);
                
    conn.setRequestProperty("Accept-Charset""UTF-8");
                
    conn.setRequestProperty("Content-Type""application/x-www-form-urlencoded;charset=" "UTF-8");


                
    conn.setHostnameVerifier ( new NullHostnameVerifier ()) ;
                
    conn.setDoOutput(true);
                
    conn.setUseCaches(false);
                
    conn.setRequestProperty("Content-Type""text/plain");
                
    OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());


                
    //write parameters
                
    writer.write(data);
                
    writer.flush();

                
    // Get the response
                
    StringBuffer answer = new StringBuffer();
                
    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                
    String line;
                while ((
    line reader.readLine()) != null) {
                    
    answer.append(line);
                }
                
    writer.close();
                
    reader.close();

                
    //Output the response
                
    System.out.println(answer.toString());

            } catch (
    MalformedURLException ex) {
                
    ex.printStackTrace();
            } catch (
    IOException ex) {
                
    ex.printStackTrace();
            }
        }
        
    /**
         * Starts the program
         *
         * @param args the command line arguments
         */
        
    public static void main(String[] args) {
            new 
    Main().sendPostRequest();
        }

        private static class 
    NullHostnameVerifier implements HostnameVerifier {
            public 
    boolean verify String hostnameSSLSession session ) {
                return 
    true ;
            }
       }

    grazie mille.

  2. #2
    Utente di HTML.it L'avatar di Alex'87
    Registrato dal
    Aug 2001
    residenza
    Verona
    Messaggi
    5,802

    Re: Server returned HTTP response code: 500 for URL

    Se non mi è sfuggito qualcosa, mi pare che qui manchi un pezzo:

    URL url = new URL("https://ip");
    HttpsURLConnection conn = ( HttpsURLConnection ) url.openConnection () ;

    Verso cosa la stai aprendo la connessione? https://ip non mi pare un indirizzo valido
    SpringSource Certified Spring Professional | Pivotal Certified Enterprise Integration Specialist
    Di questo libro e degli altri (blog personale di recensioni libri) | ​NO M.P. TECNICI

  3. #3
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: Server returned HTTP response code: 500 for URL

    Originariamente inviato da Sasuccio87
    codice:
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + "UTF-8");
    
    conn.setRequestProperty("Content-Type", "text/plain");
    Perché imposti due diversi content-type? (e il secondo, che sovrascrive il primo, non è certo per un POST di dati di un "form").
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  4. #4
    Utente di HTML.it
    Registrato dal
    Dec 2010
    Messaggi
    181
    Ho tolto il secondo content type...li non mi ero accorto di averne messi due. Per quanto riguarda l'indirizzo, ho messo l'ip della macchina sulla quale m'interessa l'invocazione...però ancora niente, stesso errore

  5. #5
    Utente di HTML.it
    Registrato dal
    Dec 2010
    Messaggi
    181
    comunque l'eccezione avviene in questa riga:

    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

  6. #6
    Utente di HTML.it L'avatar di Alex'87
    Registrato dal
    Aug 2001
    residenza
    Verona
    Messaggi
    5,802
    Originariamente inviato da Sasuccio87
    Per quanto riguarda l'indirizzo, ho messo l'ip della macchina sulla quale m'interessa l'invocazione...però ancora niente, stesso errore
    Ok, ma cosa vorresti leggere? Cosa c'è a https://198.156.23.34? (esempio)
    SpringSource Certified Spring Professional | Pivotal Certified Enterprise Integration Specialist
    Di questo libro e degli altri (blog personale di recensioni libri) | ​NO M.P. TECNICI

  7. #7
    Utente di HTML.it
    Registrato dal
    Dec 2010
    Messaggi
    181
    è un webservices che dovrebbe restituirmi un xml, infatti se lo metto nel browser con i parametri mi restituisce l'xml, ma dalla classe java che ho fatto nada de nada

  8. #8
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da Sasuccio87
    è un webservices che dovrebbe restituirmi un xml, infatti se lo metto nel browser con i parametri mi restituisce l'xml
    Ahhhh, potevi (dovevi) dirlo prima ....
    Perché allora è una richiesta in GET .. non in POST. E la query string va passata nel URL, non nel "body" della request.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  9. #9
    Utente di HTML.it
    Registrato dal
    Dec 2010
    Messaggi
    181
    si trasformerebbe così:
    Codice PHP:
        /**
         *
         */
        
    private static final long serialVersionUID 1L;

        
    /**
         * Extends the size of an array.
         */
        
    public void sendPostRequest() {

            
    //Build parameter string
            //String data = "/id_sra=2/id_file=1";
            
    try {

                
    // Send the request
                
    URL url = new URL("https://ip?nome=pippo&cognome=pluto);
                HttpsURLConnection conn = ( HttpsURLConnection ) url.openConnection () ;

                conn.setRequestMethod("
    GET");
                conn.setDoOutput(true);
                conn.setDoInput(true);
                conn.setUseCaches(false);
                conn.setAllowUserInteraction(false);

                conn.setHostnameVerifier ( new NullHostnameVerifier ()) ;
                conn.setRequestProperty("
    Content-Type", "text/plain");



                //write parameters
               //** writer.write(data);
                //** writer.flush();

                // Get the response
                StringBuffer answer = new StringBuffer();
                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    answer.append(line);
                }

                reader.close();

                //Output the response
                System.out.println(answer.toString());

            } catch (MalformedURLException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        /**
         * Starts the program
         *
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            new Main().sendPostRequest();
        }

        private static class NullHostnameVerifier implements HostnameVerifier {
            public boolean verify ( String hostname, SSLSession session ) {
                return true ;
            }
       }

    però mi da lo stesso errore

  10. #10
    Utente di HTML.it
    Registrato dal
    Dec 2010
    Messaggi
    181
    il problema e che cerco di invocare un link asp composto così:

    Codice PHP:
    [url]https://ip/home/nome=pippo/cognome=pluto[/url] 
    La cosa strana che i parametri vengono passati con lo / e non con la &. QUindi se metto il link per intero nella url connection mi dice che non trova il file...però dal browser funge ...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.