envoie du get/post

This commit is contained in:
basti 2021-01-09 22:19:24 +01:00
parent 726f2d8cba
commit dac29b05e8
7 changed files with 126 additions and 96 deletions

View file

@ -1,9 +1,11 @@
package defaut;
import java.io.IOException;
import bdd.GestionnaireHistorique;
import liste.GestionnaireListeUtilisateur;
import ui.Login_RegisterUI;
import servlet.Get;
public class Main {
@ -20,6 +22,11 @@ public class Main {
//on met à jour notre liste
GestionnaireListeUtilisateur.instance().majListe2(); //enlever le 2 //TODO
try {
Get.sendGET();
} catch (IOException e) {
e.printStackTrace();
};
//on lance le UI
Thread t = new Thread(new Login_RegisterUI());

View file

@ -12,6 +12,7 @@ public class GestionnaireNom{
private String nomUtilisateur;
static private String ipUtilisateur;
private boolean dansReseau;
private String statut = "online";
//Instance du gestionnaire de nom
@ -58,12 +59,18 @@ public class GestionnaireNom{
}
//retorurne dansReseau
// retourne dansReseau
public boolean getDansReseau() {
return dansReseau;
}
// retourne statut
public String getStatut() {
return statut;
}
// set le NomUtilisateur
public void setNom(String nom) {
@ -91,6 +98,13 @@ public class GestionnaireNom{
}
//set dansReseau
public void setStatut(String statut) {
this.statut = statut;
}
// renvoie l'ID d'un utilisateur ou "null" s'il n'existe pas (ou non en ligne)
public String idFromNom(String nom) {
for (int i=0; i<GestionnaireListeUtilisateur.instance().getListeUtilisateur().size(); i++) {

View file

@ -0,0 +1,31 @@
package servlet;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class Get {
//private static final String USER_AGENT = "Mozilla/5.0";
private static final String GET_URL = "http://localhost:8080/Servlet_MBP/test";
public static void sendGET() throws IOException { // SUbscribe
URL obj = new URL(GET_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
//con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
System.out.println("GET Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { // success
System.out.println("Get success");
} else {
System.out.println("GET request not worked");
}
}
}

View file

@ -0,0 +1,37 @@
package servlet;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class Post {
private static final String POST_URL = "http://localhost:8080/Servlet_MBP/test";
public static void sendPOST(String msg) throws IOException { // Publish cmd : change$$$id$$$nom$$$ip$$$dedans$$$statut
URL obj = new URL(POST_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
// Output Stream
con.setDoOutput(true);
DataOutputStream os = new DataOutputStream(con.getOutputStream());
//os.write(POST_PARAMS.getBytes());
os.writeBytes(msg);
os.flush();
os.close();
// For POST only - END
int responseCode = con.getResponseCode();
System.out.println("POST Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { //success
System.out.println("Post success");
} else {
System.out.println("POST request not worked");
}
}
}

View file

@ -1,91 +0,0 @@
package servlet;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Url {
//private static final String USER_AGENT = "Mozilla/5.0";
private static final String GET_URL = "http://localhost:8080/Servlet_MBP/test";
private static final String POST_URL = "http://localhost:8080/Servlet_MBP/test";
private static final String POST_PARAMS = "userName=Pankaj";
public static void main(String[] args) throws IOException {
sendGET();
System.out.println("GET DONE");
sendPOST();
System.out.println("POST DONE");
}
private static void sendGET() throws IOException {
URL obj = new URL(GET_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
//con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
System.out.println("GET Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { // success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
System.out.println(response.toString());
} else {
System.out.println("GET request not worked");
}
}
private static void sendPOST() throws IOException {
URL obj = new URL(POST_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
//con.setRequestProperty("User-Agent", USER_AGENT);
// For POST only - START
con.setDoOutput(true);
DataOutputStream os = new DataOutputStream(con.getOutputStream());
//os.write(POST_PARAMS.getBytes());
os.writeBytes("bonjour");
os.flush();
os.close();
// For POST only - END
int responseCode = con.getResponseCode();
System.out.println("POST Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { //success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
System.out.println(response.toString());
} else {
System.out.println("POST request not worked");
}
}
}

View file

@ -26,10 +26,12 @@ import java.util.ArrayList;
import clavardage.gestionnaireClavardage;
import liste.GestionnaireListeUtilisateur;
import nom.GestionnaireNom;
import servlet.Post;
import liste.TypeListeUtilisateur;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ItemListener;
import java.io.IOException;
import java.awt.event.ItemEvent;
@ -40,6 +42,8 @@ public class ListUI2 extends JFrame implements Runnable{
private static final long serialVersionUID = 42L;
private JPanel contentPane;
private JComboBox<String> comboBox = new JComboBox<String>();
private static JList<TypeListeUtilisateur> list = new JList<TypeListeUtilisateur>();
@ -183,11 +187,11 @@ public class ListUI2 extends JFrame implements Runnable{
contentPane.add(nameLabel);
// combobox
JComboBox<String> comboBox = new JComboBox<String>();
comboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
System.out.println(comboBox.getSelectedItem());
actionComboBox();
}
}
});
@ -274,4 +278,22 @@ public class ListUI2 extends JFrame implements Runnable{
list.setModel(defaultListModel);
}
// action ComboBox
private void actionComboBox() {
String item = (String) comboBox.getSelectedItem();
System.out.println(item); // debug
GestionnaireNom.instance().setStatut(item);
// on envoie l'info à la servlet
try {
Post.sendPOST("nom$$$" + GestionnaireNom.instance().getId() + "$$$" + GestionnaireNom.instance().getNom()+ "$$$"
+ GestionnaireNom.instance().getIp() + "$$$" + GestionnaireNom.instance().getDansReseau() + "$$$"
+ GestionnaireNom.instance().getStatut() );
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

View file

@ -15,8 +15,10 @@ import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;
import nom.GestionnaireNom;
import servlet.Post;
public class NomUI extends JFrame implements Runnable{
@ -134,6 +136,14 @@ public class NomUI extends JFrame implements Runnable{
//Celui-ci ne peut pas être "vide"
if (!nom.isBlank()) {
GestionnaireNom.instance().nommer2(nom); //enlever le 2 à la fin //TODO
// on envoie l'info à la servlet
try {
Post.sendPOST("nom$$$" + GestionnaireNom.instance().getId() + "$$$" + GestionnaireNom.instance().getNom()+ "$$$"
+ GestionnaireNom.instance().getIp() + "$$$" + GestionnaireNom.instance().getDansReseau() + "$$$"
+ GestionnaireNom.instance().getStatut() );
} catch (IOException e) {
e.printStackTrace();
}
dispose();
Thread t = new Thread(new ListUI2());