GetRequest.java
Home
/
src /
main /
java /
br /
ufrgs /
inf /
prosoft /
requestssimulator /
requests /
GetRequest.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package br.ufrgs.inf.prosoft.requestssimulator.requests;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author romulo
*/
public class GetRequest extends Request {
protected GetRequest(RequestPlan requestPlan, String URL) {
super(requestPlan, URL);
}
protected GetRequest(RequestPlan requestPlan, String URL, String headers) {
super(requestPlan, URL, headers);
}
@Override
public void fire() {
try {
URL url = new URL(getURL());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
getHeaders().forEach((entry) -> {
connection.setRequestProperty(entry.getKey(), entry.getValue());
});
int responseCode = connection.getResponseCode();
if (responseCode < 400) {
Logger.getGlobal().log(Level.INFO, "{0} {1}", new Object[]{getMethod(), getURL()});
} else {
Logger.getGlobal().log(Level.SEVERE, "error {0} on {1} {2}", new Object[]{responseCode, getMethod(), getURL()});
}
connection.disconnect();
} catch (MalformedURLException ex) {
Logger.getGlobal().log(Level.SEVERE, "Malormed URL");
} catch (IOException ex) {
Logger.getGlobal().log(Level.SEVERE, "IOException");
}
}
}