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);
}
} |
Partager