Connection HTTP +Accès à un serveur

cs_dom22 Messages postés 3 Date d'inscription mercredi 5 février 2003 Statut Membre Dernière intervention 28 février 2003 - 28 févr. 2003 à 10:36
sisadel Messages postés 29 Date d'inscription mardi 4 avril 2006 Statut Membre Dernière intervention 12 août 2006 - 16 avril 2006 à 11:05
dom22
Bonjour vous tous .
Actuellement je suis en stage pour un mois encore et je ne sais plus comment progresser. Je dois charger un lien internet ou plutôt un lien se trouvant sur le serveur de l'entreprise afin de pouvoir accéder à des données précises. C'est le PDA Blackberry qui doit receptionner tous ces liens et l'utilisateur doit pourvoir lire ces informations à partir du PDA.
Voici un code qui devrais établir la connexion HTTP, mais je ne sais pas le faire fonctionner et je ne comprends pas grand chose.
le voici

//The HttpConnection manager
HttpConnection httpConnector = null;

//An inputStream to read incoming data
InputStream in = null;

//A string buffer to place incoming data into
StringBuffer inBuffer = new StringBuffer();

String response;
int inChar;

//URL to connect to via standard HTTP
String URL = ?www.myServer.com/myContent?;

/* Use the following URL instead if you wish to
* connect through a WAP Gateway
* String URL =
"www.myServer.com/myContent;WAPGatewayIP=127.0.0.1";
*/

try {
//Set the URL for this connection
httpConnector = (HttpConnection)Connector.open(URL);

//Set the HTTP request type
httpConnector.setRequestMethod(HttpConnection.GET);

//openInputStream initiates the connection
in = httpConnector.openInputStream();

//read in all data until the end of the message
while ((inChar = in.read()) != -1) {
//append incoming characters to the buffer
inBuffer.append((char)inChar);

response = inBuffer.toString();
//close the inputStream
in.close();

//close the HTTP Connection
httpConnector.close();

}catch(IOException ioe){}

Merci d'avance, je suis désespérée il ne me reste plus 3 semaines pour arriver à faire ceci.

Dom22

3 réponses

cs_SuLEy Messages postés 26 Date d'inscription mercredi 14 janvier 2004 Statut Membre Dernière intervention 14 juin 2004
4 mai 2004 à 20:02
Je ne vois pas le "}"
de while ((inChar = in.read()) != -1) {

!
0
cs_SuLEy Messages postés 26 Date d'inscription mercredi 14 janvier 2004 Statut Membre Dernière intervention 14 juin 2004
4 mai 2004 à 23:06
Voila ce que ca donne corrigé !

import java.net.*;
import java.io.*;

public class Requeteur {
private URL url=null;
private HttpURLConnection httpConnector=null;
private InputStream in=null;
private StringBuffer inBuffer=new StringBuffer();
private String response=null;
private int inChar;

public String requete(){
try {
url=new URL("http","host",80,"/page");}
catch (java.net.MalformedURLException e){System.out.println(e);}

try {
httpConnector=(HttpURLConnection)url.openConnection();

httpConnector.setDefaultAllowUserInteraction(true);
httpConnector.setDefaultUseCaches(true);
httpConnector.setRequestMethod("GET");

httpConnector.connect();

in=httpConnector.getInputStream();
while ((inChar=in.read())!=-1) inBuffer.append((char)inChar);
response=inBuffer.toString();

in.close();
httpConnector.disconnect();}
catch(IOException ioe){System.out.println(ioe);}
return(response);
}
}
0
sisadel Messages postés 29 Date d'inscription mardi 4 avril 2006 Statut Membre Dernière intervention 12 août 2006
16 avril 2006 à 11:05
bonjour ,moi en fait j'utilise une connection avec les socket de java et la methode get pour envoyer ma requete je recoi la reponse du serveur ,mais je voudrai savoir comment inclure une requete avec mot cle pour l,envoyer a un moteur de recherche par exemple google.merci c urgent
0
Rejoignez-nous