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
|
private static void makePost()
{
static final String CHARSET = "UTF-8";
static final int READ_TIME_OUT = 10000;
static final int CONNECTION_TIME_OUT =15000;
String url = "http://localhost:8080/WebServices/Texte/GameService/addRelations";
Tuple<String,String> languageTerm = new Tuple<>("Tomate", "Fr");
List<Tuple<String, String>> languageTerms = new ArrayList<>();
languageTerms.add(new Tuple<>("Jus", "Fr"));
languageTerms.add(new Tuple<>("Purée", "Fr"));
languageTerms.add(new Tuple<>("Bolognaise", "Fr"));
String nickname = "Max";
String mail = "max@max.com";
HttpURLConnection connection = null;
try
{
JSONObject jsonObject = new JSONObject();
ObjectMapper objectMapper = new ObjectMapper();
jsonObject.put("languageTerm", objectMapper.writeValueAsString(languageTerm));
jsonObject.put("languageTerms", objectMapper.writeValueAsString(languageTerms));
jsonObject.put("nickname", nickname);
jsonObject.put("mail", mail);
JSONArray jsonArray = new JSONArray();
jsonArray.put(jsonObject);
String data = jsonObject.toString();
System.out.println("JSON data :");
System.out.println(data);
URL urlObj = new URL(url);
connection = (HttpURLConnection) urlObj.openConnection();
connection.setRequestMethod("POST"); // Je poste
connection.setRequestProperty("Content-Type","application/json"); // du JSON
connection.setRequestProperty("Accept", "application/json"); // et attend du JSON
connection.setAllowUserInteraction(false);
connection.setReadTimeout(READ_TIME_OUT);
connection.setConnectTimeout(CONNECTION_TIME_OUT);
connection.setRequestProperty("Content-length", data.getBytes().length + "");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Accept-Charset", CHARSET);
connection.setRequestProperty("user-agent","Agent/1");
OutputStream outputStream = connection.getOutputStream();
outputStream.write(data.getBytes("UTF-8"));
outputStream.close();
connection.connect();
int statusCode = connection.getResponseCode();
System.out.println("statusCode : " + statusCode);
switch (statusCode)
{
case 200:
case 201:
StringBuilder _result = null;
InputStream in = new BufferedInputStream(connection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
_result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null)
{
_result.append(line);
}
System.out.println("Response ! : " + _result.toString());
return;
}
}
catch (MalformedURLException ex)
{
System.out.println( "Error in http connection" + ex.toString());
}
catch (IOException ex)
{
System.out.println( "Error in http connection" + ex.toString());
}
catch (Exception ex)
{
System.out.println("Error in http connection" + ex.toString());
}
finally
{
if (connection != null)
{
try
{
connection.disconnect();
}
catch (Exception ex)
{
System.out.println("Error in http connection" + ex.toString());
}
}
}
} |
Partager