List of usage examples for java.net HttpURLConnection getResponseMessage
public String getResponseMessage() throws IOException
From source file:com.webcohesion.ofx4j.client.net.OFXV1Connection.java
/** * Send the specified buffer to the specified URL. * * @param url The URL./*from w w w. j av a 2s . c o m*/ * @param outBuffer The buffer. * @return The response. */ protected InputStream sendBuffer(URL url, ByteArrayOutputStream outBuffer) throws IOException, OFXConnectionException { HttpURLConnection connection = openConnection(url); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-ofx"); connection.setRequestProperty("Content-Length", String.valueOf(outBuffer.size())); connection.setRequestProperty("Accept", "*/*, application/x-ofx"); connection.setDoOutput(true); connection.connect(); OutputStream out = connection.getOutputStream(); out.write(outBuffer.toByteArray()); InputStream in; int responseCode = connection.getResponseCode(); if (responseCode >= 200 && responseCode < 300) { in = connection.getInputStream(); } else if (responseCode >= 400 && responseCode < 500) { throw new OFXServerException("Error with client request: " + connection.getResponseMessage(), responseCode); } else { throw new OFXServerException( "Invalid response code from OFX server: " + connection.getResponseMessage(), responseCode); } return in; }
From source file:org.apache.nifi.processors.livy.ExecuteSparkInteractive.java
private JSONObject readJSONObjectFromUrlPOST(String urlString, LivySessionService livySessionService, Map<String, String> headers, String payload) throws IOException, JSONException { HttpURLConnection connection = livySessionService.getConnection(urlString); connection.setRequestMethod("POST"); connection.setDoOutput(true);/* ww w .j a va2s. c o m*/ for (Map.Entry<String, String> entry : headers.entrySet()) { connection.setRequestProperty(entry.getKey(), entry.getValue()); } OutputStream os = connection.getOutputStream(); os.write(payload.getBytes()); os.flush(); if (connection.getResponseCode() != HttpURLConnection.HTTP_OK && connection.getResponseCode() != HttpURLConnection.HTTP_CREATED) { throw new RuntimeException("Failed : HTTP error code : " + connection.getResponseCode() + " : " + connection.getResponseMessage()); } InputStream content = connection.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(content, StandardCharsets.UTF_8)); String jsonText = IOUtils.toString(rd); return new JSONObject(jsonText); }
From source file:org.openrdf.http.server.ProtocolTest.java
private void delete(String location) throws Exception { URL url = new URL(location); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("DELETE"); conn.connect();//from w ww. j a va 2 s . c o m int responseCode = conn.getResponseCode(); if (responseCode != HttpURLConnection.HTTP_OK && // 200 OK responseCode != HttpURLConnection.HTTP_NO_CONTENT) // 204 NO CONTENT { String response = "location " + location + " responded: " + conn.getResponseMessage() + " (" + responseCode + ")"; fail(response); } }
From source file:ee.ria.xroad.proxy.clientproxy.WsdlRequestProcessor.java
HttpURLConnection createConnection(SoapMessageImpl message) throws Exception { URL url = new URL("http://127.0.0.1:" + SystemProperties.getClientProxyHttpPort()); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoInput(true);/* w w w . ja v a 2 s .c o m*/ con.setDoOutput(true); // Use the same timeouts as client proxy to server proxy connections. con.setConnectTimeout(SystemProperties.getClientProxyTimeout()); con.setReadTimeout(SystemProperties.getClientProxyHttpClientTimeout()); con.setRequestMethod("POST"); con.setRequestProperty(HttpHeaders.CONTENT_TYPE, MimeUtils.contentTypeWithCharset(MimeTypes.TEXT_XML, StandardCharsets.UTF_8.name())); IOUtils.write(message.getBytes(), con.getOutputStream()); if (con.getResponseCode() != HttpURLConnection.HTTP_OK) { throw new RuntimeException( "Received HTTP error: " + con.getResponseCode() + " - " + con.getResponseMessage()); } return con; }
From source file:com.epam.catgenome.manager.externaldb.HttpDataManager.java
private String getHttpResult(final int status, final String location, final HttpURLConnection conn) throws IOException, ExternalDbUnavailableException { String result;/*from ww w . j a va2 s .co m*/ if (status == HttpURLConnection.HTTP_OK) { LOGGER.info("HTTP_OK reply from destination server"); InputStream inputStream = conn.getInputStream(); BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); StringBuilder responseStrBuilder = new StringBuilder(); String inputStr; while ((inputStr = streamReader.readLine()) != null) { responseStrBuilder.append(inputStr); } result = responseStrBuilder.toString(); } else { LOGGER.severe("Unexpected HTTP status:" + conn.getResponseMessage() + " for " + location); throw new ExternalDbUnavailableException(String.format("Unexpected HTTP status: %d %s for URL %s", status, conn.getResponseMessage(), location)); } return result; }
From source file:org.eclipse.rdf4j.http.server.ProtocolTest.java
private void delete(String location) throws Exception { URL url = new URL(location); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("DELETE"); conn.connect();/*from w w w. j a v a2 s . c o m*/ int responseCode = conn.getResponseCode(); // HTTP 200 or 204 if (responseCode != HttpURLConnection.HTTP_OK && responseCode != HttpURLConnection.HTTP_NO_CONTENT) { String response = "location " + location + " responded: " + conn.getResponseMessage() + " (" + responseCode + ")"; fail(response); } }
From source file:com.yahoo.sql4d.sql4ddriver.DDataSource.java
/** * All commands.//from w w w .ja va 2 s .c o m * @return */ private Either<String, Either<JSONArray, JSONObject>> fireCommand(String endPoint, String optData, String httpType) { StringBuilder buff = new StringBuilder(); try { URL url = null; try { url = new URL(String.format(coordinatorUrl + endPoint, coordinatorHost, coordinatorPort)); } catch (MalformedURLException ex) { Logger.getLogger(DDataSource.class.getName()).log(Level.SEVERE, null, ex); return new Left<>("Bad Url : " + ex); } Proxy proxy = Proxy.NO_PROXY; if (proxyHost != null) { proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); } HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(proxy); httpConnection.setRequestMethod(httpType); httpConnection.addRequestProperty("content-type", "application/json"); httpConnection.setDoOutput(true); if ("POST".equals(httpType) && optData != null) { httpConnection.getOutputStream().write(optData.getBytes()); } if (httpConnection.getResponseCode() == 500 || httpConnection.getResponseCode() == 404) { return new Left<>(String.format("Http %d : %s \n", httpConnection.getResponseCode(), httpConnection.getResponseMessage())); } BufferedReader stdInput = new BufferedReader(new InputStreamReader(httpConnection.getInputStream())); String line = null; while ((line = stdInput.readLine()) != null) { buff.append(line); } } catch (IOException ex) { Logger.getLogger(DDataSource.class.getName()).log(Level.SEVERE, null, ex); } JSONArray possibleResArray = null; try { possibleResArray = new JSONArray(buff.toString()); return new Right<String, Either<JSONArray, JSONObject>>( new Left<JSONArray, JSONObject>(possibleResArray)); } catch (JSONException je) { try { JSONObject possibleResObj = new JSONObject(buff.toString()); return new Right<String, Either<JSONArray, JSONObject>>( new Right<JSONArray, JSONObject>(possibleResObj)); } catch (JSONException je2) { return new Left<>(String.format("Recieved data %s not in json format. \n", buff.toString())); } } }
From source file:org.eclipse.rdf4j.http.server.ProtocolTest.java
private Set<Namespace> getNamespaces(String location) throws Exception { URL url = new URL(location); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); try {//from w w w . j a va 2 s .com conn.setRequestProperty("Accept", "text/csv; charset=utf-8"); conn.connect(); int responseCode = conn.getResponseCode(); if (responseCode != HttpURLConnection.HTTP_OK) { String response = "location " + location + " responded: " + conn.getResponseMessage() + " (" + responseCode + ")"; fail(response); } CSVReader reader = new CSVReader( new InputStreamReader(conn.getInputStream(), Charset.forName("UTF-8"))); try { String[] headerRow = reader.readNext(); if (headerRow == null) { fail("header not found"); } if (!Arrays.equals(headerRow, new String[] { "prefix", "namespace" })) { fail("illegal header row: " + Arrays.toString(headerRow)); } Set<Namespace> namespaces = new HashSet<Namespace>(); String[] aRow; while ((aRow = reader.readNext()) != null) { String prefix = aRow[0]; String namespace = aRow[1]; namespaces.add(new SimpleNamespace(prefix, namespace)); } return namespaces; } finally { reader.close(); } } finally { conn.disconnect(); } }
From source file:org.openrdf.http.server.ProtocolTest.java
private void deleteNamespace(String location) throws Exception { // System.out.println("Delete namespace at " + location); URL url = new URL(location); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("DELETE"); conn.setDoOutput(true);/*from w ww . j av a 2 s . c o m*/ conn.connect(); int responseCode = conn.getResponseCode(); if (responseCode != HttpURLConnection.HTTP_OK && // 200 OK responseCode != HttpURLConnection.HTTP_NO_CONTENT) // 204 NO CONTENT { String response = "location " + location + " responded: " + conn.getResponseMessage() + " (" + responseCode + ")"; fail(response); } }
From source file:export.GarminUploader.java
@Override public Status listWorkouts(List<Pair<String, String>> list) { Status s;//from w w w.j av a 2 s .c o m if ((s = connect()) != Status.OK) { return s; } HttpURLConnection conn = null; Exception ex = null; try { conn = (HttpURLConnection) new URL(LIST_WORKOUTS_URL).openConnection(); conn.setRequestMethod("GET"); addCookies(conn); conn.connect(); getCookies(conn); InputStream in = new BufferedInputStream(conn.getInputStream()); JSONObject obj = parse(in); conn.disconnect(); int responseCode = conn.getResponseCode(); String amsg = conn.getResponseMessage(); if (responseCode == 200) { obj = obj.getJSONObject("com.garmin.connect.workout.dto.BaseUserWorkoutListDto"); JSONArray arr = obj.getJSONArray("baseUserWorkouts"); for (int i = 0;; i++) { obj = arr.optJSONObject(i); if (obj == null) break; list.add(new Pair<String, String>(obj.getString("workoutId"), obj.getString("workoutName") + ".json")); } return Status.OK; } ex = new Exception(amsg); } catch (IOException e) { ex = e; } catch (JSONException e) { ex = e; } s = Uploader.Status.ERROR; s.ex = ex; if (ex != null) { ex.printStackTrace(); } return s; }