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
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testedtfac.iut;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicNameValuePair;
import pattern.observer.IObservable;
import pattern.observer.IObserver;
/**
*
* @author Morgan
*/
public final class IUTLogin implements IObservable{
private HttpURLConnection connection;
private ArrayList<IObserver> observers;
private URL iutURL;
private boolean connecting = false;
private HttpURLConnection result;
public IUTLogin(String username, String password) throws MalformedURLException, IOException, URISyntaxException{
iutURL = new URL("monURL");
HashMap form = new HashMap();
form.put("username",username);
form.put("passwd",password);
makeQuery(iutURL, form);
}
public IUTLogin(String username, String password, IObserver observer) throws MalformedURLException, IOException, URISyntaxException{
addObserver(observer);
iutURL = new URL("[monURL]");
HashMap form = new HashMap();
form.put("username",username);
form.put("passwd",password);
makeQuery(iutURL, form);
}
private void makeQuery(URL url, HashMap form) throws IOException, URISyntaxException{
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setAllowUserInteraction(true);
if(connection.getResponseCode() == 200){
notifyObservers("connecting");
form.put("Submit","Connexion");
//On complete le formulaire avec les champs cachés
Pattern p = Pattern.compile("<input type=\"hidden\".*name=\"(.*)\".*value=\"(.*)\".*/>");
Matcher m;
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String buffer;
boolean stop = false;
while((buffer = reader.readLine())!=null && !stop){
m = p.matcher(buffer);
if(m.find()){
if(m.group(2).indexOf("search") == -1){
form.put(m.group(1),m.group(2));
}else{
stop = true;
}
}
}
HttpClient http = new DefaultHttpClient();
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
Set keys = form.keySet();
Iterator it = keys.iterator();
while(it.hasNext()){
String key = (String)it.next();
params.add(new BasicNameValuePair(key,form.get(key).toString()));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
HttpPost httppost = new HttpPost("monURL");
httppost.setEntity(entity);
HttpResponse rep = http.execute(httppost);
reader = new BufferedReader(new InputStreamReader(rep.getEntity().getContent()));
while((buffer = reader.readLine()) != null){
System.out.println(buffer);
}
notifyObservers("connected");
}
}
@Override
public void addObserver(IObserver observer) {
if(observers == null){
observers = new ArrayList<IObserver>();
}
observers.add(observer);
}
@Override
public void deleteObserver(IObserver observer) {
observers.remove(observer);
}
@Override
public void notifyObservers(String state) {
if(observers != null){
for (int i = 0; i < observers.size(); i++) {
observers.get(i).update(state);
}
}
}
public HttpURLConnection getResult(){
return result;
}
} |
Partager