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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
|
/*
Class de gestion du protocol http
Version 1.4.2.1
*/
import java.net.*;
import java.io.*;
public class Http{
// Cookie=
String cookies="";
/** Outils **/
URL url;
/** Variables **/
String head;
String phpSessID;
String corp;
/** Initialisation **/
public Http(){
head = "";
corp = "";
phpSessID = "";
//java.net.CookieHandler cook = new java.net.CookieHandler();
//CookieManager cook = new CookieManager();
}
/** Fonctions Externe **/
public void Affiche(){
String txt =corp;
txt = txt.replaceAll("<[?]xml [-A-Za-z0-9\" .=]+ [?]>", "");
class Fl_Aff extends javax.swing.JFrame implements Runnable{
private javax.swing.JLabel lbAff;
public void setLbAff(String txt){ lbAff.setText(txt); };
public Fl_Aff(){
/* Fenaitre */
super();
setTitle("Http : Prévisualisation");
setSize(800,600);
setLocationRelativeTo(null); //On centre la fentre sur l'écran
setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
/* Zone de positionnement */
javax.swing.JPanel panel = new javax.swing.JPanel();
setContentPane(panel);
lbAff = new javax.swing.JLabel() {
public java.awt.Dimension getMinimumSize() {
return new java.awt.Dimension(200, 200);
}
};
panel.add(lbAff);
}
public void run(){
this.setSize(this.getX(),this.getY());
setVisible(true);
}
}Fl_Aff fl_aff = new Fl_Aff();
fl_aff.setLbAff(txt);
fl_aff.run();
}
public String get( String u ){
try {
this.url = new URL(u);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
get_head( u );
if ( head.indexOf("null=[HTTP/1.1 200 OK]")!=-1 )
return get_corp( u );
else
return head;
}
public String get_head( String u ){
try {
this.url = new URL(u);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
HttpURLConnection connectionHttp = (HttpURLConnection) new URL(u).openConnection();
connectionHttp.setDoOutput(true);
if ( !cookies.equals("") )
connectionHttp.setRequestProperty("Cookie", cookies);
java.util.Map map = connectionHttp.getHeaderFields();
head = map.toString();
}catch (IOException ioe) {
System.out.println("Echec connection sur : "+url.toString());
System.out.println("IOException ->");
ioe.printStackTrace();
System.exit(1);
}catch (NullPointerException npe) {
System.out.println("Echec connection sur : "+url.toString());
System.out.println("IOException ->");
npe.printStackTrace();
head = "null=[HTTP/1.1 4 System.out.println(04 Not Found]\tServer Not Resolved.";
}finally {}
return head;
}
public String get_corp( String u ){
try {
this.url = new URL(u);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
/* Initialisation */
InputStream is = null;
DataInputStream dis;
String s;
StringBuilder txt = new StringBuilder("");
try {
HttpURLConnection connectionHttp = (HttpURLConnection) new URL(u).openConnection();
connectionHttp.setDoOutput(true);
if ( !cookies.equals("") )
connectionHttp.setRequestProperty("Cookie", cookies);
/* Connection */
java.util.Map headFields = connectionHttp.getHeaderFields();
if ( headFields!=null )
head = headFields.toString();
else
head = "Erreur de chargement d'entête";
is = connectionHttp.getInputStream();
/* Ouverture du flot */
dis = new DataInputStream(new BufferedInputStream(is));
/* Mise en memoire : */
while ((s = dis.readLine()) != null) {
txt.append(s+"\n");
}
/* Gestion des erreurs */
} catch (MalformedURLException mue) {
System.out.println("MalformedURLException ->");
mue.printStackTrace();
System.exit(1);
} catch (IOException ioe) {
System.out.println("IOException ->");
ioe.printStackTrace();
System.exit(1);
} finally {
try {
if ( is!=null )
is.close();
} catch (IOException ioe) {}
}
corp = txt.toString();
return corp;
}
public String post(String url, String data){
try {
this.url = new URL(url);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
StringBuilder txt = new StringBuilder("");
try {
//Envoye du POST
HttpURLConnection connectionHttp = (HttpURLConnection) new URL(url).openConnection();
connectionHttp.setDoOutput(true);
if ( !cookies.equals("") )
connectionHttp.setRequestProperty("Cookie", cookies);
OutputStreamWriter osw = new OutputStreamWriter(connectionHttp.getOutputStream());
osw.write(data);
osw.flush();
osw.close();
// Resultat
BufferedReader readerPOST = new BufferedReader(new InputStreamReader(connectionHttp.getInputStream()));
String lignePOST;
while ((lignePOST = readerPOST.readLine()) != null)
txt.append(lignePOST+"\n");
connectionHttp.disconnect();
}catch (IOException e){
System.out.println("IOException ->");
e.printStackTrace();
}
corp = txt.toString();
return corp;
}
/** Extrateur **/
public String extract_sid(){
int posi=0;
if ( corp.equals("") ){return "";}
/* Recherche de la chaine "?sid=" */
posi = corp.indexOf(".sid=");
if ( posi<0 ) return extract_phpSessID();
posi+=5;
if ( corp.charAt(posi)=='"' ) posi++;
if ( corp.charAt(posi)=='\'' )posi++;
/* Extraction de la chaine "?sid=" */
phpSessID = "";
while ( posi < corp.length() ){
if ( corp.charAt(posi)=='\'' )break;
if ( corp.charAt(posi)=='"' ) break;
if ( corp.charAt(posi)==' ' ) break;
if ( corp.charAt(posi)=='>' ) break;
phpSessID += corp.charAt(posi)+"";
posi++;
}
/* Renvoye du resultat */
if ( phpSessID.equals("") ) return extract_phpSessID();
return phpSessID;
}
/** Information **/
public boolean url_200(){
return ( head.indexOf("null=[HTTP/1.1 200 OK]")!=-1 );
}
public boolean url_200( String u ){
get_head( u );
return url_200();
}
public boolean url_404(){
return ( head.indexOf("null=[HTTP/1.1 404 Not Found]")!=-1 );
}
public boolean url_404( String u ){
get_head( u );
return url_404();
}
public boolean is_ServerNotResolved(){
return ( head.indexOf("null=[HTTP/1.1 404 Not Found]\tServer Not Resolved.")!=-1 );
}
public boolean is_ServerNotResolved( String u ){
get_head( u );
return is_ServerNotResolved();
}
/** Extracteur **/
public String extract_phpSessID(){
int posi=0;
if ( head.equals("") ){return "";}
/* Recherche de la chaine "PHPSESSID=" */
posi = head.indexOf("PHPSESSID=");
if ( posi < 0 ) return "";
posi+=10;
if ( head.charAt(posi)=='"' ) posi++;
if ( head.charAt(posi)=='\'' )posi++;
/* Extraction de la chaine jusqu'as ; */
phpSessID = "";
while ( posi < head.length() ){
if ( head.charAt(posi)==';' ) break;
phpSessID += head.charAt(posi)+"";
posi++;
}
return phpSessID;
}
public java.util.HashSet<String> extractLiens(){
java.util.HashSet list = new java.util.HashSet();
int posi=0;
int posi_=0;
if ( head.equals("") ){return list;}
String url_path = url.toString();
url_path = url_path.substring(0,url_path.indexOf(url.getPath()));
/* Recherche de la chaine *//* http:// */
posi=0;
while ( (posi=corp.indexOf("http://",posi)) !=-1){
posi_ = endOfLiens(corp,posi);
list.add( corp.substring(posi,posi_).trim() );
posi = posi_;
}/* Recherche de la chaine *//* src= */
posi=0;
while ( (posi=corp.indexOf("src=",posi)) !=-1){
posi = posi+"src=".length();
if ( corp.substring(posi).startsWith("'")
|| corp.substring(posi).startsWith("\"") ) posi++;
posi_ = endOfLiens(corp,posi);
if ( corp.substring(posi).startsWith("http://") )
list.add( corp.substring(posi,posi_).trim() );
else
list.add( url_path+corp.substring(posi,posi_).trim() );
posi = posi_;
}/* Recherche de la chaine *//* href= */
posi=0;
while ( (posi=corp.indexOf("href=",posi)) !=-1){
posi = posi+"href=".length();
if ( corp.substring(posi).startsWith("'")
|| corp.substring(posi).startsWith("\"") ) posi++;
posi_ = endOfLiens(corp,posi);
if ( corp.substring(posi).startsWith("http://") )
list.add( corp.substring(posi,posi_).trim() );
else
list.add( url_path+corp.substring(posi,posi_).trim() );
posi = posi_;
}/* Recherche de la chaine *//* window.location.href= */
posi=0;
while ( (posi=corp.indexOf("window.location.href=",posi)) !=-1){
posi = posi+"window.location.href=".length()+1;// " ou '
posi_ = endOfLiens(corp,posi);
if ( corp.substring(posi).startsWith("http://") )
list.add( corp.substring(posi,posi_).trim() );
else
list.add( url_path+corp.substring(posi,posi_).trim() );
posi = posi_;
}/* Recherche de la chaine *//* window.open( */
posi=0;
while ( (posi=corp.indexOf("window.open(",posi)) !=-1){
posi = posi+"window.open(".length();
if ( corp.substring(posi).startsWith("'")
|| corp.substring(posi).startsWith("\"") ) posi++;
posi_ = endOfLiens(corp,posi);
if ( corp.substring(posi).startsWith("http://") )
list.add( corp.substring(posi,posi_).trim() );
else
list.add( url_path+corp.substring(posi,posi_).trim() );
posi = posi_;
}/* Recherche de la chaine *//* window.location.href= */
posi=0;
while ( (posi=corp.indexOf("window.location.href=",posi)) !=-1){
posi = posi+"window.location.href=".length()+1;// " ou '
posi_ = endOfLiens(corp,posi);
if ( corp.substring(posi).startsWith("http://") )
list.add( corp.substring(posi,posi_).trim() );
else
list.add( url_path+corp.substring(posi,posi_).trim() );
posi = posi_;
}/* Recherche de la chaine *//* action= */
posi=0;
while ( (posi=corp.indexOf("action=",posi)) !=-1){
posi = posi+"action=".length();
if ( corp.substring(posi).startsWith("'")
|| corp.substring(posi).startsWith("\"") ) posi++;
posi_ = endOfLiens(corp,posi);
if ( corp.substring(posi).startsWith("http://") )
list.add( corp.substring(posi,posi_).trim() );
else
list.add( url_path+corp.substring(posi,posi_).trim() );
posi = posi_;
}
/* Extraction de la chaine jusqu'as ; */
return list;// a faire : // background
}
private int endOfLiens(String txt,int posi){
int posi_=0;
posi_ = txt.length();
if ( txt.indexOf("\"" ,posi)!=-1 )
posi_ = java.lang.Math.min(txt.indexOf("\"",posi),posi_);
if ( txt.indexOf(" ",posi)!=-1 )
posi_ = java.lang.Math.min(txt.indexOf(" " ,posi),posi_);
if ( txt.indexOf("'",posi)!=-1 )
posi_ = java.lang.Math.min(txt.indexOf("'" ,posi),posi_);
if ( txt.indexOf("\n",posi)!=-1 )
posi_ = java.lang.Math.min(txt.indexOf("\n",posi),posi_);
if ( txt.indexOf(">",posi)!=-1 )
posi_ = java.lang.Math.min(txt.indexOf(">" ,posi),posi_);
if ( txt.indexOf("<",posi)!=-1 )
posi_ = java.lang.Math.min(txt.indexOf("<" ,posi),posi_);
if ( txt.indexOf("\t",posi)!=-1 )
posi_ = java.lang.Math.min(txt.indexOf("\t",posi),posi_);
if ( txt.indexOf(";",posi)!=-1 )
posi_ = java.lang.Math.min(txt.indexOf(";",posi),posi_);
return posi_;
}
public static java.util.List<String> getCookiesFireFox(){//on utilise les classes de base de Java
java.util.List<String> cookies = new java.util.ArrayList<String>();
String line = "";
//on lit les cookies
BufferedReader entree;
try {
String file_path;
// Linux :
if ( "Linux".equals(System.getProperty("os.name")) ){
file_path = System.getProperty("user.dir").replaceAll("(.*/home/[^/]+/).*","$1");
file_path = file_path + ".mozilla/firefox/";
entree = new BufferedReader(new FileReader(file_path+"profiles.ini"));
while ((line=entree.readLine()) != null){
if ( line.indexOf("Path=")<0 ) continue;
file_path = file_path + line.replaceAll("Path=","").trim();
file_path = file_path + "/";
break;
}
}else{
file_path = "C:\\Users\\Serge1\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\c98ruuo1.default\\";
}
file_path = file_path + "cookies.txt";
entree = new BufferedReader(new FileReader(file_path));
while ((line=entree.readLine()) != null){
cookies.add(line);
}
entree.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return cookies;
}
/** Testeur **/
public static void main(String[] args) {
Http http = new Http();
http.get("http://didungar.com");
// System.out.println(http.head);
// System.out.println(http.corp);
// System.out.println(http.extractLiens().toString());
http.get("http://192.168.1.4/~burnwar/");
// http.get("http://192.168.1.4/~black/test.php");
System.out.println(http.head);
http.Affiche();
// System.out.println("~~~ Fin du test ~~~");
}
} |
Partager