Last active
August 21, 2019 08:55
-
-
Save gustinmi/e3fd32de03e8919e83a6b169bce36911 to your computer and use it in GitHub Desktop.
Java Http client simple
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package http; | |
import java.io.*; | |
import java.net.HttpURLConnection; | |
import java.net.Proxy; | |
import java.net.URL; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import moped.common.Utils; | |
public class HttpClient { | |
public static final boolean VERBOSE_TRACE = true; | |
public static final Logger log = Logger.getLogger(HttpClient.class.getCanonicalName()); | |
public static void callHttp(String payload, String url) { | |
final StringBuilder outputString = new StringBuilder(1000); | |
HttpURLConnection connection = null; | |
try { | |
connection = getConnection(url); | |
if (connection == null) { throw new IllegalStateException("Cannot continue with no connection"); } | |
try { | |
final ByteArrayOutputStream bout = new ByteArrayOutputStream(); | |
final byte[] buffer = payload.getBytes(); | |
bout.write(buffer); | |
final byte[] b = bout.toByteArray(); | |
// Set the appropriate HTTP parameters. | |
connection.setRequestProperty("Content-Length", String.valueOf(b.length)); | |
connection.setRequestProperty("Content-Type", "Content-Type: application/json;charset=UTF-8"); | |
connection.setRequestMethod("POST"); | |
connection.setDoOutput(true); | |
connection.setDoInput(true); | |
final OutputStream out = connection.getOutputStream(); | |
//Write the content of the request to the outputstream of the HTTP Connection. | |
out.write(b); | |
out.close(); | |
} catch (IOException e) { | |
log.log(Level.SEVERE, "Error connecting", e); | |
try (final InputStream errorStream = connection.getErrorStream()) { | |
final String errMsg = Utils.readFully(errorStream); | |
log.log(Level.SEVERE, errMsg); | |
} catch (IOException e1) { | |
log.log(Level.SEVERE, "Error reading error stream: ", e1); | |
} | |
return; | |
} | |
finally { | |
if (VERBOSE_TRACE) log.info("Sucessfully send request"); | |
} | |
String responseString; | |
try (final InputStreamReader isr = new InputStreamReader(connection.getInputStream())) { // try to read the response. | |
try (final BufferedReader in = new BufferedReader(isr)) { | |
while ((responseString = in.readLine()) != null) { | |
outputString.append(responseString); | |
} | |
} | |
} catch (IOException e) { | |
log.log(Level.SEVERE, "Error while sending ", e); | |
try (final InputStream errorStream = connection.getErrorStream()) { | |
final String errMsg = Utils.readFully(errorStream); | |
log.log(Level.SEVERE, errMsg); | |
} catch (IOException e1) { | |
log.log(Level.SEVERE, "Error reading error stream: ", e1); | |
} | |
return; | |
} | |
// TODO convert responseString to whatever you like | |
} | |
finally { | |
if (connection != null) connection.disconnect(); | |
} | |
} | |
protected static HttpURLConnection getConnection(final String url) { | |
try { | |
final HttpURLConnection connection; | |
if (VERBOSE_TRACE) log.info("Connecting to " + url); | |
connection = (HttpURLConnection) new URL(url).openConnection(Proxy.NO_PROXY); | |
if (VERBOSE_TRACE) log.info("Connected."); | |
if (connection != null) return connection; | |
} catch (Exception e) { | |
log.log(Level.SEVERE, String.format("Error %s while connecting to : %s ", e.getMessage(), url), e); | |
} | |
throw new IllegalStateException("Could not connect!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment