com.loadcoder.result.clients.HttpClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of loadcoder-result Show documentation
Show all versions of loadcoder-result Show documentation
This project contains the result related features of Loadcoder
/*******************************************************************************
* Copyright (C) 2018 Stefan Vahlgren at Loadcoder
*
* This file is part of Loadcoder.
*
* Loadcoder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Loadcoder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
******************************************************************************/
package com.loadcoder.result.clients;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
public class HttpClient {
protected String protocolAsString(boolean https) {
return https ? "https" : "http";
}
protected int sendPost(String body, String url, List headers) {
try {
return sendPostChecked(body, url, headers);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
protected int sendPostChecked(String body, String url, List headers) throws Exception {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
// add reuqest header
headers.stream().forEach(header -> con.setRequestProperty(header.getName(), header.getValue()));
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(body);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
InputStream _is;
if (responseCode < HttpURLConnection.HTTP_BAD_REQUEST) {
_is = con.getInputStream();
} else {
/* error from server */
_is = con.getErrorStream();
}
BufferedReader in = new BufferedReader(new InputStreamReader(_is));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return responseCode;
}
}