118 lines
4.1 KiB
Java
118 lines
4.1 KiB
Java
|
|
package at.fhj.airkoality.network;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.io.OutputStream;
|
|
import java.io.OutputStreamWriter;
|
|
import java.net.URL;
|
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
|
|
|
|
/**
|
|
* General workflow for connections with UrlConnection:
|
|
*
|
|
* The connection object is created by invoking the openConnection method on a URL.
|
|
* The setup parameters and general request properties are manipulated.
|
|
* The actual connection to the remote object is made, using the connect method.
|
|
* The remote object becomes available. The header fields and the contents of the remote object can be accessed.
|
|
*
|
|
*/
|
|
public class HttpsClient {
|
|
public String get(URL url) throws IOException {
|
|
InputStream stream = null;
|
|
HttpsURLConnection connection = null;
|
|
String result = null;
|
|
try {
|
|
connection = (HttpsURLConnection) url.openConnection();
|
|
connection.setReadTimeout(3000);
|
|
connection.setConnectTimeout(3000);
|
|
|
|
//this is a get request
|
|
connection.setRequestMethod("GET");
|
|
//indicate that this connection wants to read from the connection
|
|
connection.setDoInput(true);
|
|
|
|
//connect and therefore initiate network communication
|
|
connection.connect();
|
|
|
|
int responseCode = connection.getResponseCode();
|
|
if (responseCode != HttpsURLConnection.HTTP_OK) {
|
|
throw new IOException("HTTP error code: " + responseCode);
|
|
}
|
|
stream = connection.getInputStream();
|
|
|
|
if(stream != null) {
|
|
BufferedReader r = new BufferedReader(new InputStreamReader(stream));
|
|
StringBuilder total = new StringBuilder();
|
|
for (String line; (line = r.readLine()) != null; ) {
|
|
total.append(line).append('\n');
|
|
}
|
|
result = total.toString();
|
|
}
|
|
} finally {
|
|
if (stream != null) {
|
|
stream.close();
|
|
}
|
|
if (connection != null) {
|
|
connection.disconnect();
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public String post(URL url, String body) throws IOException {
|
|
InputStream stream = null;
|
|
HttpsURLConnection connection = null;
|
|
String result = null;
|
|
try {
|
|
connection = (HttpsURLConnection) url.openConnection();
|
|
connection.setReadTimeout(3000);
|
|
connection.setConnectTimeout(3000);
|
|
|
|
//this is a post request
|
|
connection.setRequestMethod("POST");
|
|
//indicate that this connection wants to read from the connection
|
|
connection.setDoOutput(true);
|
|
|
|
//write request body to connection object
|
|
OutputStream outputStream = connection.getOutputStream();
|
|
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
|
|
outputStreamWriter.write(body);
|
|
outputStreamWriter.flush();
|
|
outputStreamWriter.close();
|
|
outputStream.close();
|
|
|
|
//connect and therefore initiate network communication
|
|
connection.connect();
|
|
|
|
int responseCode = connection.getResponseCode();
|
|
if (responseCode != HttpsURLConnection.HTTP_OK) {
|
|
throw new IOException("HTTP error code: " + responseCode);
|
|
}
|
|
|
|
stream = connection.getInputStream();
|
|
|
|
if(stream != null) {
|
|
BufferedReader r = new BufferedReader(new InputStreamReader(stream));
|
|
StringBuilder total = new StringBuilder();
|
|
for (String line; (line = r.readLine()) != null; ) {
|
|
total.append(line).append('\n');
|
|
}
|
|
result = total.toString();
|
|
}
|
|
|
|
} finally {
|
|
if (stream != null) {
|
|
stream.close();
|
|
}
|
|
if (connection != null) {
|
|
connection.disconnect();
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
} |