Per quello dovresti studiarti un po' il protocollo http, giusto per capire come funziona una richiesta http.
A livello di codice ti sconsiglio di implementare tutto da capo soprattutto quando ci sono le (ottime) librerie di Apache (che non richiedono grosse conoscenze di quello che sta sotto):
http://hc.apache.org/httpcomponents-...uickstart.html
http://hc.apache.org/httpcomponents-.../examples.html
http://hc.apache.org/httpcomponents-...tml/index.html
Se guardi ci sono degli esempi anche con i cookie![]()
SpringSource Certified Spring Professional | Pivotal Certified Enterprise Integration Specialist
Di questo libro e degli altri (blog personale di recensioni libri) | NO M.P. TECNICI
Di nulla
Se hai qualche domanda specifica chiedi pure![]()
SpringSource Certified Spring Professional | Pivotal Certified Enterprise Integration Specialist
Di questo libro e degli altri (blog personale di recensioni libri) | NO M.P. TECNICI
Sto provando ad effettuare la connesssione http:
Putroppo mi restituisce:codice:import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import javax.net.ssl.HttpsURLConnection; public class HttpURLConnectionExample { private final String USER_AGENT = "Mozilla/5.0"; public static void main(String[] args) throws Exception { HttpURLConnectionExample http = new HttpURLConnectionExample(); System.out.println("Testing 1 - Send Http GET request"); http.sendGet(); System.out.println("\nTesting 2 - Send Http POST request"); http.sendPost(); } // HTTP GET request private void sendGet() throws Exception { String url = "https://areaclienti3.tre.it/133/profilo.jsp"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // optional default is GET con.setRequestMethod("GET"); //add request header con.setRequestProperty("User-Agent", USER_AGENT); int responseCode = con.getResponseCode(); System.out.println("\nSending 'GET' request to URL : " + url); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); //print result System.out.println(response.toString()); } // HTTP POST request private void sendPost() throws Exception { String url = "https://sso.tre.it/selfcare-sso/login"; URL obj = new URL(url); HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); //add reuqest header con.setRequestMethod("POST"); con.setRequestProperty("User-Agent", USER_AGENT); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); String urlParameters = "username1=347123456789&password=pippo&_eventId=submit<=LT-36179913-9gvxufN7fMV9HbZZU7Siw2dNSMuBSB&execution=e11s1&usertype=MSISDN"; // Send post request con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + urlParameters); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); //print result System.out.println(response.toString()); } }
Cosa č che sbaglio ?codice:Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: https://sso.tre.it/selfcare-sso/login at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source) at HttpURLConnectionExample.sendPost(HttpURLConnectionExample.java:87) at HttpURLConnectionExample.main(HttpURLConnectionExample.java:23) Caused by: java.io.IOException: Server returned HTTP response code: 500 for URL: https://sso.tre.it/selfcare-sso/login at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at java.net.HttpURLConnection.getResponseCode(Unknown Source) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source) at HttpURLConnectionExample.sendPost(HttpURLConnectionExample.java:81) ... 1 more
Ultima modifica di LeleFT; 19-11-2013 a 10:53 Motivo: Correzione tag QUOTE con tag CODE
Tony