List of usage examples for java.net HttpURLConnection getErrorStream
public InputStream getErrorStream()
From source file:com.mycompany.grupo6ti.GenericResource.java
@PUT @Produces("application/json") @Path("emitirFactura/{id_oC}") public String emitirFactura(@PathParam("id_oC") String id_oC) { try {/*from w w w .j a v a2 s .c o m*/ URL url = new URL("http://localhost:85/"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); InputStream is; conn.setRequestProperty("Accept-Charset", "UTF-8"); conn.setRequestProperty("Content-Type", "application/json"); conn.setUseCaches(true); conn.setRequestMethod("PUT"); conn.setDoOutput(true); conn.setDoInput(true); String idJson = "{\n" + " \"oc\": \"" + id_oC + "\"\n" + "}"; //String idJson2 = "[\"Test\", \"Funcionando Bien\"]"; OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); out.write(idJson); out.flush(); out.close(); if (conn.getResponseCode() >= 400) { is = conn.getErrorStream(); } else { is = conn.getInputStream(); } String result2 = ""; BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line; while ((line = rd.readLine()) != null) { result2 += line; } rd.close(); return result2; } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return ("[\"Test\", \"Funcionando Bien\"]"); }
From source file:com.mycompany.grupo6ti.GenericResource.java
@POST @Produces("application/json") @Path("/recepcionarOc/{id}") public String recepcionarOc(@PathParam("id") String id) { try {/*ww w . j a v a2 s. c o m*/ URL url = new URL("http://localhost:83/recepcionar/" + id); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); InputStream is; conn.setRequestProperty("Accept-Charset", "UTF-8"); conn.setRequestProperty("Content-Type", "application/json"); conn.setUseCaches(true); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setDoInput(true); String idJson = "{\n" + " \"id\": \"" + id + "\"\n" + "}"; //String idJson2 = "[\"Test\", \"Funcionando Bien\"]"; OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); out.write(idJson); out.flush(); out.close(); if (conn.getResponseCode() >= 400) { is = conn.getErrorStream(); } else { is = conn.getInputStream(); } String result2 = ""; BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line; while ((line = rd.readLine()) != null) { result2 += line; } rd.close(); return result2; } catch (IOException e) { e.printStackTrace(); return ("[\"Test\", \"Funcionando Bien1\"]"); } catch (Exception e) { e.printStackTrace(); return ("[\"Test\", \"Funcionando Bien2\"]"); } //return ("[\"Test\", \"Funcionando Bien\"]"); }
From source file:com.mycompany.grupo6ti.GenericResource.java
@POST @Produces("application/json") @Path("/pagarFactura/{id}") public String pagarFactura(@PathParam("id") String id) { try {//from w w w . j a va2s.c o m URL url = new URL("http://localhost:85/pay"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); InputStream is; conn.setRequestProperty("Accept-Charset", "UTF-8"); conn.setRequestProperty("Content-Type", "application/json"); conn.setUseCaches(true); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setDoInput(true); String idJson = "{\n" + " \"id\": \"" + id + "\"\n" + "}"; //String idJson2 = "[\"Test\", \"Funcionando Bien\"]"; OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); out.write(idJson); out.flush(); out.close(); if (conn.getResponseCode() >= 400) { is = conn.getErrorStream(); } else { is = conn.getInputStream(); } String result2 = ""; BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line; while ((line = rd.readLine()) != null) { result2 += line; } rd.close(); return result2; } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return ("[\"Test\", \"Funcionando Bien\"]"); }
From source file:com.mycompany.grupo6ti.GenericResource.java
@POST //? @Produces("application/json") @Path("/despacharProducto/{id}") public String despacharOc(@PathParam("id") String id) { try {// ww w .j a v a 2 s. c om URL url = new URL("http://localhost:83/despachar/" + id); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); InputStream is; conn.setRequestProperty("Accept-Charset", "UTF-8"); conn.setRequestProperty("Content-Type", "application/json"); conn.setUseCaches(true); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setDoInput(true); String idJson = "{\n" + " \"id\": \"" + id + "\"\n" + "}"; //String idJson2 = "[\"Test\", \"Funcionando Bien\"]"; OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); out.write(idJson); out.flush(); out.close(); if (conn.getResponseCode() >= 400) { is = conn.getErrorStream(); } else { is = conn.getInputStream(); } String result2 = ""; BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line; while ((line = rd.readLine()) != null) { result2 += line; } rd.close(); return result2; } catch (IOException e) { e.printStackTrace(); return ("[\"Test\", \"Funcionando Bien1\"]"); } catch (Exception e) { e.printStackTrace(); return ("[\"Test\", \"Funcionando Bien2\"]"); } //return ("[\"Test\", \"Funcionando Bien\"]"); }
From source file:alluxio.client.rest.TestCase.java
/** * Runs the test case and returns the {@link HttpURLConnection}. *//*from w w w . j a va 2 s . co m*/ public HttpURLConnection execute() throws Exception { HttpURLConnection connection = (HttpURLConnection) createURL().openConnection(); connection.setRequestMethod(mMethod); if (mOptions.getMD5() != null) { connection.setRequestProperty("Content-MD5", mOptions.getMD5()); } if (mOptions.getInputStream() != null) { connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/octet-stream"); ByteStreams.copy(mOptions.getInputStream(), connection.getOutputStream()); } if (mOptions.getBody() != null) { connection.setDoOutput(true); connection.setRequestProperty("Content-Type", mOptions.getContentType()); ObjectMapper mapper = new ObjectMapper(); // make sure that serialization of empty objects does not fail mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); OutputStream os = connection.getOutputStream(); os.write(mapper.writeValueAsString(mOptions.getBody()).getBytes()); os.close(); } connection.connect(); if (Response.Status.Family.familyOf(connection.getResponseCode()) != Response.Status.Family.SUCCESSFUL) { InputStream errorStream = connection.getErrorStream(); if (errorStream != null) { Assert.fail("Request failed: " + IOUtils.toString(errorStream)); } Assert.fail("Request failed with status code " + connection.getResponseCode()); } return connection; }
From source file:com.mycompany.grupo6ti.GenericResource.java
@POST @Produces("application/json") @Path("/rechazarOc/{id}/{rechazo}") public String rechazarOc(@PathParam("id") String id, @PathParam("rechazo") String rechazo) { try {/*w ww . j a v a 2s .c o m*/ URL url = new URL("http://localhost:83/rechazar/" + id); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); InputStream is; conn.setRequestProperty("Accept-Charset", "UTF-8"); conn.setRequestProperty("Content-Type", "application/json"); conn.setUseCaches(true); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setDoInput(true); String idJson = "{\n" + " \"rechazo\": \"" + rechazo + "\"\n" + "}"; //String idJson2 = "[\"Test\", \"Funcionando Bien\"]"; OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); out.write(idJson); out.flush(); out.close(); if (conn.getResponseCode() >= 400) { is = conn.getErrorStream(); } else { is = conn.getInputStream(); } String result2 = ""; BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line; while ((line = rd.readLine()) != null) { result2 += line; } rd.close(); return result2; } catch (IOException e) { e.printStackTrace(); return ("[\"Test\", \"Funcionando Bien1\"]"); } catch (Exception e) { e.printStackTrace(); return ("[\"Test\", \"Funcionando Bien2\"]"); } //return ("[\"Test\", \"Funcionando Bien\"]"); }
From source file:edu.mda.bioinfo.ids.DownloadFiles.java
private void downloadFromHttpToFile(String theURL, String theParameters, String theFile) throws IOException { // both in milliseconds //int connectionTimeout = 1000 * 60 * 3; //int readTimeout = 1000 * 60 * 3; try {//from w w w. j a v a2 s. co m URL myURL = new URL(theURL); HttpURLConnection connection = (HttpURLConnection) myURL.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setInstanceFollowRedirects(false); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "text/plain"); //connection.setRequestProperty("charset", "utf-8"); if (null != theParameters) { System.out.println("Content-Length " + Integer.toString(theParameters.getBytes().length)); connection.setRequestProperty("Content-Length", "" + Integer.toString(theParameters.getBytes().length)); } connection.setUseCaches(false); try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) { if (null != theParameters) { wr.write(theParameters.getBytes()); } wr.flush(); File myFile = new File(theFile); System.out.println("Requesting " + myURL); FileUtils.copyInputStreamToFile(connection.getInputStream(), myFile); System.out.println("Downloaded " + myURL); } catch (IOException rethrownExp) { InputStream errorStr = connection.getErrorStream(); if (null != errorStr) { System.err.println("Error stream returned: " + IOUtils.toString(errorStr)); } else { System.err.println("No error stream returned"); } throw rethrownExp; } } catch (IOException rethrownExp) { System.err.println("exception " + rethrownExp.getMessage() + " thrown while downloading " + theURL + " to " + theFile); throw rethrownExp; } }
From source file:com.amazon.alexa.avs.auth.companionservice.CompanionServiceClient.java
JsonObject callService(String path, Map<String, String> parameters) throws IOException { HttpURLConnection con = null; InputStream response = null;//from w w w.j ava2 s.c om try { String queryString = mapToQueryString(parameters); URL obj = new URL(deviceConfig.getCompanionServiceInfo().getServiceUrl(), path + queryString); con = (HttpURLConnection) obj.openConnection(); if (con instanceof HttpsURLConnection) { ((HttpsURLConnection) con).setSSLSocketFactory(pinnedSSLSocketFactory); } con.setRequestProperty("Content-Type", "application/json"); con.setRequestMethod("GET"); if ((con.getResponseCode() >= 200) || (con.getResponseCode() < 300)) { response = con.getInputStream(); } if (response != null) { String responsestring = IOUtils.toString(response); JsonReader reader = Json .createReader(new ByteArrayInputStream(responsestring.getBytes(StandardCharsets.UTF_8))); IOUtils.closeQuietly(response); return reader.readObject(); } return Json.createObjectBuilder().build(); } catch (IOException e) { if (con != null) { response = con.getErrorStream(); if (response != null) { String responsestring = IOUtils.toString(response); JsonReader reader = Json.createReader( new ByteArrayInputStream(responsestring.getBytes(StandardCharsets.UTF_8))); JsonObject error = reader.readObject(); String errorName = error.getString("error", null); String errorMessage = error.getString("message", null); if (!StringUtils.isBlank(errorName) && !StringUtils.isBlank(errorMessage)) { throw new RemoteServiceException(errorName + ": " + errorMessage); } } } throw e; } finally { if (response != null) { IOUtils.closeQuietly(response); } } }
From source file:com.example.httpjson.AppEngineClient.java
public static Response getOrPost(Request request) { mErrorMessage = null;/*from w w w . j a v a2 s .c om*/ HttpURLConnection conn = null; Response response = null; try { conn = (HttpURLConnection) request.uri.openConnection(); // if (!mAuthenticator.authenticate(conn)) { // mErrorMessage = str(R.string.aerc_authentication_failed) + ": " + mAuthenticator.errorMessage(); // } else { if (request.headers != null) { for (String header : request.headers.keySet()) { for (String value : request.headers.get(header)) { conn.addRequestProperty(header, value); } } } if (request instanceof POST) { byte[] payload = ((POST) request).body; String s = new String(payload, "UTF-8"); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("POST"); JSONObject jsonobj = getJSONObject(s); conn.setRequestProperty("Content-Type", "application/json; charset=utf8"); // ... OutputStream os = conn.getOutputStream(); os.write(jsonobj.toString().getBytes("UTF-8")); os.close(); // conn.setFixedLengthStreamingMode(payload.length); // conn.getOutputStream().write(payload); int status = conn.getResponseCode(); if (status / 100 != 2) response = new Response(status, new Hashtable<String, List<String>>(), conn.getResponseMessage().getBytes()); } if (response == null) { int a = conn.getResponseCode(); if (a == 401) { response = new Response(a, conn.getHeaderFields(), new byte[] {}); } InputStream a1 = conn.getErrorStream(); BufferedInputStream in = new BufferedInputStream(conn.getInputStream()); byte[] body = readStream(in); response = new Response(conn.getResponseCode(), conn.getHeaderFields(), body); // List<String> a = conn.getHeaderFields().get("aa"); } } } catch (IOException e) { e.printStackTrace(System.err); mErrorMessage = ((request instanceof POST) ? "POST " : "GET ") + str(R.string.aerc_failed) + ": " + e.getLocalizedMessage(); } finally { if (conn != null) conn.disconnect(); } return response; }