[CLIENT] Mise en place d'un mécanisme d'encoding en UTF-8 lors de l'envoi de requêtes HTTP vers le serveur

This commit is contained in:
Notmoo-PC\Notmoo 2018-01-24 18:37:31 +01:00
parent 67f9f54d8d
commit d771e9214d
2 changed files with 12 additions and 9 deletions

View File

@ -61,7 +61,7 @@ public class ConnectionService {
throw new IllegalStateException("No url specified for data server");
if(executor.isShutdown() || executor.isTerminated())
throw new IllegalStateException("Service was shut down : unable to send text");
executor.submit(()->textSender.send(serverUrl, "message="+text, listener));
executor.submit(()->textSender.send(serverUrl, text, listener));
}
public void shutdown() {

View File

@ -1,12 +1,8 @@
package com.pqt.client.module.connection.senders;
import com.pqt.client.module.connection.listeners.IConnectionListener;
import com.sun.javafx.binding.StringFormatter;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.*;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
@ -16,7 +12,7 @@ public class HttpTextSender implements ITextSender{
@Override
public void send(String host, String text, IConnectionListener listener) {
try {
String trueURL = String.format("http://%s?%s", host, encode(text));
String trueURL = String.format("http://%s?%s", host, format(text));
HttpURLConnection con = (HttpURLConnection) new URL(trueURL).openConnection();
con.setRequestMethod("GET");
@ -53,7 +49,14 @@ public class HttpTextSender implements ITextSender{
}
// Méthode à modifier pour encoder le message suivant un algorithme spécifique
private String encode(String toEncode){
return toEncode;
private String format(String toFormat){
try {
String encodeTo = "UTF-8";
return String.format("format=%s&message=%s", encodeTo, URLEncoder.encode(toFormat, encodeTo));
}catch(UnsupportedEncodingException e){
//TODO Issue #6 : ajouter un log ici
e.printStackTrace();
}
return String.format("message=%s", toFormat);
}
}