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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Scanner;
import javax.print.DocFlavor.STRING;
class Connection {
public static int Connection(String lgn,String pwd) {
int var = 0;
int pos = 0;
// On cree le client
HttpClient client = new HttpClient();
// Le HTTPMethod qui sera un Post en lui indiquant lURL du traitement du formulaire
PostMethod methode = new PostMethod("http://www.**********.com/test/Finalisation/window/TraitementConn.php");
// On ajoute les parametres du formulaire
methode.addParameter("lgn", lgn); // (champs, valeur)
methode.addParameter("pwd", pwd);
// Le buffer qui nous servira a recuperer le code de la page
BufferedReader br = null;
try
{
int retour = client.executeMethod(methode);
// Pour la gestion des erreurs ou un debuggage, on recupere le nombre renvoye.
System.out.println(" La reponse de executeMethod est : " + retour);
br = new BufferedReader(new InputStreamReader(methode.getResponseBodyAsStream()));
System.out.println(methode.getResponseHeaders());
Cookie[] cookies = client.getState( ).getCookies( );
for( int i = 0; i < 2; i++ ) {
Cookie cookie = cookies[i];
String domain = cookie.getDomain( );
String path = cookie.getPath( );
String name = cookie.getName( );
String value = cookie.getValue( );
System.out.println( "Cookie: " + domain + ", " + path + ", " +
name + ", " + value );
}
System.out.println("*****************************");
String readLine;
@SuppressWarnings("unused")
String cherche = "Connection réalisée";
// Tant que la ligne en cours nest pas vide
while(((readLine = br.readLine()) != null))
{
System.out.println(readLine);
//Ici on vérifie si la connection, c'est bien réalisé
pos = readLine.indexOf("Connection réalisée");
if (pos >= 0)
{
System.out.println(pos);
var = pos;
}
}
}
catch (Exception e)
{
System.err.println(e); // erreur possible de executeMethod
}
finally
{
// On ferme la connexion
methode.releaseConnection();
if(br != null)
{
try
{
br.close(); // on ferme le buffer
}
catch (Exception e) { /* on fait rien */ }
}
}
//Ici on ouvre une nouvelle page ou l'on doit être identifier
GetMethod methodes = new GetMethod("http://www.********.com/test/Finalisation/window/func-modalbox-test.php");
try
{
int retour = client.executeMethod(methodes);
// Pour la gestion des erreurs ou un debuggage, on recupere le nombre renvoye.
System.out.println(" La reponse de executeMethod est : " + retour);
br = new BufferedReader(new InputStreamReader(methodes.getResponseBodyAsStream()));
String readLine;
// Tant que la ligne en cours nest pas vide
while(((readLine = br.readLine()) != null))
{
System.out.println(readLine);
}
}
catch (Exception e)
{
System.err.println(e); // erreur possible de executeMethod
}
int co;
if (var > 0)
{
co = 1;
}
else
{
co = 0;
}
return co;
}
} |
Partager