IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Java Discussion :

Gestion POST java.


Sujet :

Langage Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    Développeur Web
    Inscrit en
    Mars 2006
    Messages
    111
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2006
    Messages : 111
    Par défaut Gestion POST java.
    Voila je me crée une class pour récupéré des info sur internet dans un joli petit applet.
    ( cours de mes action en bourse, meteo ... quel st on fête .... Inscription a valider dans mon site .... bref )

    Voici la class que j'ai crée pour y arrivé :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    import java.net.*;
    import java.io.*;
     
    public class http {
    	/* Variables */
    	private String post_txt = "";
    	private String cookies_txt = "";
     
    	/* Résultat */
    	public String source = "";
     
    	/* Fonctions */
    	public static void main(String[] args) {
    	}
    	public void connect_get(String url_txt){
    		 BufferedReader reader = null;
    		 OutputStreamWriter writer = null;
    		 try {
    			 //encodage des paramètres de la requête
     
    			 //création de la connection
    			 URL url = new URL(url_txt);
    			 URLConnection conn = url.openConnection();
    			 conn.setDoOutput(true);
     
    			 //envoi de la requête
    			 writer = new OutputStreamWriter(conn.getOutputStream());
    			 writer.write(this.cookies_txt);
    			 writer.flush();
     
    			 //	lecture de la réponse
    			 reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    			 String ligne;
    			 this.source="";
    			 while ((ligne = reader.readLine()) != null) {
    				 this.source += ligne + "\n";
    			 }
    		 }catch (Exception e) {
    			 e.printStackTrace();
    		 }finally{
    			 try{writer.close();}catch(Exception e){}
    			 try{reader.close();}catch(Exception e){}
    		 }
    		 System.out.print("http_get : ");
    		 System.out.println(url_txt);
    	}
    	public void connect_post(String url_txt){
    		try {
    			// Décomposition de l'url
    			URL url = new URL(url_txt);
     
    	        // Create a socket to the host
    	        String hostname = url.getHost();
    	        int port = 80;
    	        InetAddress addr = InetAddress.getByName(hostname);
    	        Socket socket = new Socket(addr, port);
     
    	        // Send header
    	        String path = url.getPath();
    	        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
    	        writer.write("POST "+ path +" HTTP/1.0\r\n");
    	        writer.write("Content-Length: "+ this.post_txt.length() +"\r\n");
    	        writer.write("Content-Type: application/x-www-form-urlencoded\r\n");
    	        writer.write("\r\n");
     
    	        // Send data
    	        writer.write(this.post_txt);//data);
    	        writer.write("\r\n");
    	        writer.flush();
     
    	        // Get response
    	        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    	        String ligne;
    	        this.source="";
    	        while ((ligne = reader.readLine()) != null) {
    	        	this.source += ligne + "\n";
    	        }
    	        writer.close();
    	        reader.close();
    	    } catch (Exception e) {
    	    	e.printStackTrace();
    	    }finally{
    	    	//try{writer.close();}catch(Exception e){}
    	    	//try{reader.close();}catch(Exception e){}
    	    }
    	    System.out.print("http_post : ");
    	    System.out.println(url_txt);
    	}
    	public void connect(String url_txt){
    		 if ( this.post_txt=="" ) connect_get(url_txt);
    		 else connect_post(url_txt);
    	}
    	public void post_del(){
    		this.post_txt = "";
    	}
    	public void post_add(String name, String value){
    		if ( this.post_txt=="" ){
    			try {
    				this.post_txt  = URLEncoder.encode(name, "UTF-8");
    				this.post_txt += "=";
    				this.post_txt += URLEncoder.encode(value, "UTF-8");
    			}catch (Exception e) {
    				 e.printStackTrace();
    			}finally{
    				//System.out.println("Post : "+ name +" = "+ value);
    			}
    		}else{
    			try {
    				this.post_txt += "&";
    				this.post_txt += URLEncoder.encode(name, "UTF-8");
    				this.post_txt += "=";
    				this.post_txt += URLEncoder.encode(value, "UTF-8");
    			}catch (Exception e) {
    				 e.printStackTrace();
    			}finally{
    				//System.out.println("Post : "+ name +" = "+ value);
    			}
    		}
    	}
    	public  void print_source(){
    		System.out.print(this.source);
    	}
    	public void print_post(){
    		System.out.print(this.post_txt);
    	}
    }
    Ma syntaxte http, pour l'envoye des get marche ( cookies non fait )
    Mais pas celle des post ... Pourquoi ?

    ( si je suis mal placé, merci de me le dire ... )

  2. #2
    Membre émérite
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    548
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2006
    Messages : 548
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    URLConnection conn = url.openConnection();
    conn.setMethod("POST");

  3. #3
    Membre confirmé
    Homme Profil pro
    Développeur Web
    Inscrit en
    Mars 2006
    Messages
    111
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2006
    Messages : 111
    Par défaut
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method setMethod(String) is undefined for the type URLConnection

    at http.connect_post(http.java:59)
    at http.connect(http.java:94)

  4. #4
    Membre émérite
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    548
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2006
    Messages : 548
    Par défaut
    Il faut la caster en HttpURLConnection

  5. #5
    Membre confirmé
    Homme Profil pro
    Développeur Web
    Inscrit en
    Mars 2006
    Messages
    111
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2006
    Messages : 111
    Par défaut
    J'ai surtout l'impression que c'est l'objet : URLConnection qui n'est pas reconnu.


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    	        URLConnection conn = (HttpURLConnection)url.openConnection();
    	        conn.setMethod("POST");
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method setMethod(String) is undefined for the type URLConnection

    at http.connect_post(http.java:59)
    at http.connect(http.java:94)

  6. #6
    Membre émérite
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    548
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2006
    Messages : 548
    Par défaut
    Il faut caster ET changer la déclaration, sinon ça sert pas à grand chose.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    HttpURLConnectionconn = (HttpURLConnection)url.openConnection();

Discussions similaires

  1. gestion Clavier Java
    Par wagui26 dans le forum Débuter
    Réponses: 5
    Dernier message: 30/01/2010, 20h23
  2. gestion mémoire Java
    Par donnadieujulien dans le forum Général Java
    Réponses: 10
    Dernier message: 15/09/2008, 15h59
  3. gestion dbase java
    Par am.adnane dans le forum JDBC
    Réponses: 1
    Dernier message: 10/10/2006, 08h05
  4. Jeu de gestion en java
    Par luckyvae dans le forum Développement 2D, 3D et Jeux
    Réponses: 6
    Dernier message: 27/08/2006, 18h19

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo