public void sendHttpRequest( final HttpRequest httpRequest, final HttpResponseListener httpResponseListener) { if (httpRequest.getUrl() == null) { httpResponseListener.failed( new GdxRuntimeException("can't process a HTTP request without URL set")); return; } try { final String method = httpRequest.getMethod(); URL url; if (method.equalsIgnoreCase(HttpMethods.GET)) { String queryString = ""; String value = httpRequest.getContent(); if (value != null && !"".equals(value)) queryString = "?" + value; url = new URL(httpRequest.getUrl() + queryString); } else { url = new URL(httpRequest.getUrl()); } final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // should be enabled to upload data. final boolean doingOutPut = method.equalsIgnoreCase(HttpMethods.POST) || method.equalsIgnoreCase(HttpMethods.PUT); connection.setDoOutput(doingOutPut); connection.setDoInput(true); connection.setRequestMethod(method); // 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()); executorService.submit( new Runnable() { @Override public void run() { try { // 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(); InputStream contentAsStream = httpRequest.getContentStream(); OutputStream outputStream = connection.getOutputStream(); if (contentAsString != null) { OutputStreamWriter writer = new OutputStreamWriter(outputStream); writer.write(contentAsString); writer.flush(); writer.close(); } else if (contentAsStream != null) { StreamUtils.copyStream(contentAsStream, outputStream); outputStream.flush(); outputStream.close(); } } connection.connect(); final HttpClientResponse clientResponse = new HttpClientResponse(connection); try { httpResponseListener.handleHttpResponse(clientResponse); } finally { connection.disconnect(); } } catch (final Exception e) { connection.disconnect(); httpResponseListener.failed(e); } } }); } catch (Exception e) { httpResponseListener.failed(e); return; } }