Added Auth-Header
This commit is contained in:
parent
12fce27d04
commit
ea6420e234
@ -34,7 +34,8 @@ public class DataController {
|
|||||||
USER_ID = Long.parseLong(httpRequest.sendPostRequest(
|
USER_ID = Long.parseLong(httpRequest.sendPostRequest(
|
||||||
LOGIN_ENDPOINT,
|
LOGIN_ENDPOINT,
|
||||||
"login=" + username
|
"login=" + username
|
||||||
+ "&password=" + password
|
+ "&password=" + password,
|
||||||
|
false
|
||||||
));
|
));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -45,7 +46,7 @@ public class DataController {
|
|||||||
|
|
||||||
public void createEvent(Event event) {
|
public void createEvent(Event event) {
|
||||||
try {
|
try {
|
||||||
System.out.println(httpRequest.sendPostRequest(ADD_EVENT_ENDPOINT, event.getAsUrlParam()));
|
System.out.println(httpRequest.sendPostRequest(ADD_EVENT_ENDPOINT, event.getAsUrlParam(), true));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("Es konnte keine Verbindung mit dem Server hergestellt werden.");
|
throw new RuntimeException("Es konnte keine Verbindung mit dem Server hergestellt werden.");
|
||||||
}
|
}
|
||||||
@ -53,7 +54,7 @@ public class DataController {
|
|||||||
|
|
||||||
public void deleteEvent(int eventId) {
|
public void deleteEvent(int eventId) {
|
||||||
try {
|
try {
|
||||||
System.out.println(httpRequest.sendPostRequest(DELETE_EVENT_ENDPOINT, "eventId=" + eventId));
|
System.out.println(httpRequest.sendPostRequest(DELETE_EVENT_ENDPOINT, "eventId=" + eventId, true));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -63,7 +64,7 @@ public class DataController {
|
|||||||
ArrayList<Event> eventList = new ArrayList<>();
|
ArrayList<Event> eventList = new ArrayList<>();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String jsonResponse = httpRequest.sendPostRequest(ALL_EVENTS_ENDPOINT, "userId=" + USER_ID);
|
String jsonResponse = httpRequest.sendPostRequest(ALL_EVENTS_ENDPOINT, "userId=" + USER_ID, true);
|
||||||
System.out.println(jsonResponse);
|
System.out.println(jsonResponse);
|
||||||
|
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
@ -9,10 +9,23 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class HttpRequest {
|
public class HttpRequest {
|
||||||
public String sendPostRequest(String urlString, String urlParameters) throws Exception {
|
public String sendPostRequest(String urlString, String urlParameters, boolean sendAuth) throws Exception {
|
||||||
byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
|
byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
|
||||||
int postDataLength = postData.length;
|
int postDataLength = postData.length;
|
||||||
|
|
||||||
|
/*
|
||||||
|
URL url = new URL("http://test.de:8080/event/add");
|
||||||
|
HttpURLConnection http = (HttpURLConnection)url.openConnection();
|
||||||
|
http.setRequestMethod("POST");
|
||||||
|
http.setDoOutput(true);
|
||||||
|
http.setRequestProperty("Accept", "application/json");
|
||||||
|
http.setRequestProperty("Authorization", "Bearer {token}");
|
||||||
|
http.setRequestProperty("Content-Type", "");
|
||||||
|
http.setRequestProperty("Content-Length", "0");
|
||||||
|
|
||||||
|
System.out.println(http.getResponseCode() + " " + http.getResponseMessage());
|
||||||
|
http.disconnect();
|
||||||
|
*/
|
||||||
URL url = new URL(urlString);
|
URL url = new URL(urlString);
|
||||||
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
||||||
|
|
||||||
@ -23,6 +36,12 @@ public class HttpRequest {
|
|||||||
con.setRequestProperty("charset", "utf-8");
|
con.setRequestProperty("charset", "utf-8");
|
||||||
con.setRequestProperty("Content-Length", Integer.toString(postDataLength));
|
con.setRequestProperty("Content-Length", Integer.toString(postDataLength));
|
||||||
con.setUseCaches(false);
|
con.setUseCaches(false);
|
||||||
|
|
||||||
|
if(sendAuth){
|
||||||
|
con.setRequestProperty("Accept", "application/json");
|
||||||
|
con.setRequestProperty("Authorization", "Bearer {token}");
|
||||||
|
}
|
||||||
|
|
||||||
try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
|
try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
|
||||||
wr.write(postData);
|
wr.write(postData);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user