List of usage examples for com.badlogic.gdx.net HttpStatus SC_OK
int SC_OK
To view the source code for com.badlogic.gdx.net HttpStatus SC_OK.
Click Source Link
From source file:es.eucm.ead.editor.platform.AbstractPlatform.java
License:Open Source License
@Override public <T> T sendHttpRequest(HttpRequest httpRequest, Class<T> type) throws IOException { String method = httpRequest.getMethod(); URL url;//from w w w.jav a 2s. co m String queryUrl; if (method.equalsIgnoreCase(HttpMethods.GET)) { String queryString = ""; String value = httpRequest.getContent(); if (value != null && !"".equals(value)) queryString = "?" + value; queryUrl = httpRequest.getUrl() + queryString; } else { queryUrl = httpRequest.getUrl(); } url = new URL(queryUrl); Gdx.app.log(ABSTRACT_PLATFORM_TAG, "Sending HTTP " + method + " request to " + queryUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // should be enabled to upload data. boolean doingOutPut = method.equalsIgnoreCase(HttpMethods.POST) || method.equalsIgnoreCase(HttpMethods.PUT); connection.setDoOutput(doingOutPut); connection.setDoInput(true); connection.setRequestMethod(method); HttpURLConnection.setFollowRedirects(httpRequest.getFollowRedirects()); // Headers get set regardless of the method for (Map.Entry<String, String> header : httpRequest.getHeaders().entrySet()) connection.addRequestProperty(header.getKey(), header.getValue()); // Set Timeouts connection.setConnectTimeout(httpRequest.getTimeOut()); connection.setReadTimeout(httpRequest.getTimeOut()); // Set the content for POST and PUT (GET has the // information embedded in the URL) if (doingOutPut) { // we probably need to use the content as stream // here instead of using it as a string. String contentAsString = httpRequest.getContent(); if (contentAsString != null) { OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); try { writer.write(contentAsString); } finally { StreamUtils.closeQuietly(writer); } } else { InputStream contentAsStream = httpRequest.getContentStream(); if (contentAsStream != null) { OutputStream os = connection.getOutputStream(); try { StreamUtils.copyStream(contentAsStream, os); } finally { StreamUtils.closeQuietly(os); } } } } connection.connect(); int status = connection.getResponseCode(); if (status != HttpStatus.SC_OK) { throw new IOException("Http stauts code: " + status); } if (type == HttpURLConnection.class) { return (T) connection; } try { InputStream input = null; input = connection.getInputStream(); if (type == String.class) { try { return (T) StreamUtils.copyStreamToString(input, connection.getContentLength()); } finally { StreamUtils.closeQuietly(input); } } else if (type == byte[].class) { try { return (T) StreamUtils.copyStreamToByteArray(input, connection.getContentLength()); } finally { StreamUtils.closeQuietly(input); } } return null; } finally { connection.disconnect(); } }