List of usage examples for java.net HttpURLConnection getErrorStream
public InputStream getErrorStream()
From source file:org.apache.olingo.fit.tecsvc.http.AcceptHeaderAcceptCharsetHeaderITCase.java
@Test public void invalidAcceptHeader2() throws Exception { URL url = new URL(SERVICE_URI + "ESAllPrim"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(HttpMethod.GET.name()); connection.setRequestProperty(HttpHeader.ACCEPT, "application/xyz"); connection.connect();//w ww . j a va2 s . c o m assertEquals(HttpStatusCode.NOT_ACCEPTABLE.getStatusCode(), connection.getResponseCode()); final String content = IOUtils.toString(connection.getErrorStream()); assertTrue(content.contains( "The content-type range 'application/xyz' is " + "not supported as value of the Accept header.")); }
From source file:org.apache.olingo.fit.tecsvc.http.AcceptHeaderAcceptCharsetHeaderITCase.java
@Test public void invalidAcceptCharsetHeader1() throws Exception { URL url = new URL(SERVICE_URI + "ESAllPrim"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(HttpMethod.GET.name()); connection.setRequestProperty(HttpHeader.ACCEPT_CHARSET, "US-ASCII"); connection.connect();/*from w ww . j a va 2 s .c o m*/ assertEquals(HttpStatusCode.NOT_ACCEPTABLE.getStatusCode(), connection.getResponseCode()); final String content = IOUtils.toString(connection.getErrorStream()); assertTrue(content .contains("The charset specified in Accept charset header " + "'US-ASCII' is not supported.")); }
From source file:org.apache.olingo.fit.tecsvc.http.AcceptHeaderAcceptCharsetHeaderITCase.java
@Test public void validFormatWithIncorrectParamsInAcceptCharsetHeader() throws Exception { URL url = new URL(SERVICE_URI + "ESAllPrim?$format=json"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(HttpMethod.GET.name()); connection.setRequestProperty(HttpHeader.ACCEPT_CHARSET, "utf-8;abc=xyz"); connection.connect();// www .ja v a2 s . co m assertEquals(HttpStatusCode.BAD_REQUEST.getStatusCode(), connection.getResponseCode()); final String content = IOUtils.toString(connection.getErrorStream()); assertTrue(content.contains("The Accept charset header 'utf-8;abc=xyz' is not supported.")); }
From source file:org.apache.olingo.fit.tecsvc.http.AcceptHeaderAcceptCharsetHeaderITCase.java
@Test public void validFormatWithIncorrectParamInAcceptCharsetHeader() throws Exception { URL url = new URL(SERVICE_URI + "ESAllPrim?$format=json"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(HttpMethod.GET.name()); connection.setRequestProperty(HttpHeader.ACCEPT_CHARSET, "utf-8;abc=xyz"); connection.connect();/*from www . j a v a2s . co m*/ assertEquals(HttpStatusCode.BAD_REQUEST.getStatusCode(), connection.getResponseCode()); final String content = IOUtils.toString(connection.getErrorStream()); assertTrue(content.contains("The Accept charset header 'utf-8;abc=xyz' is not supported.")); }
From source file:org.apache.olingo.fit.tecsvc.http.AcceptHeaderAcceptCharsetHeaderITCase.java
@Test public void validFormatWithUnsupportedAcceptCharsetHeader() throws Exception { URL url = new URL(SERVICE_URI + "ESAllPrim?$format=json"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(HttpMethod.GET.name()); connection.setRequestProperty(HttpHeader.ACCEPT_CHARSET, "iso-8859-1"); connection.connect();//from w ww . ja v a2 s . c o m assertEquals(HttpStatusCode.NOT_ACCEPTABLE.getStatusCode(), connection.getResponseCode()); final String content = IOUtils.toString(connection.getErrorStream()); assertTrue(content .contains("The charset specified in Accept charset header " + "'iso-8859-1' is not supported.")); }
From source file:eu.codeplumbers.cosi.api.tasks.RegisterDeviceTask.java
@Override protected Device doInBackground(Void... voids) { URL urlO = null;/*from w w w . j av a2 s . c o m*/ try { urlO = new URL(url); HttpURLConnection conn = (HttpURLConnection) urlO.openConnection(); conn.setConnectTimeout(5000); conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); conn.setRequestProperty("Authorization", authHeader); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("POST"); OutputStream os = conn.getOutputStream(); os.write(deviceString.getBytes("UTF-8")); os.flush(); // read the response int status = conn.getResponseCode(); InputStream in = null; if (status >= HttpURLConnection.HTTP_BAD_REQUEST) { in = conn.getErrorStream(); } else { in = conn.getInputStream(); } StringWriter writer = new StringWriter(); IOUtils.copy(in, writer, "UTF-8"); String result = writer.toString(); JSONObject jsonObject = new JSONObject(result); if (jsonObject != null) { if (jsonObject.has("login")) { resultDevice = new Device(); resultDevice.setUrl(url.replace("/device/", "")); //Log.d(getClass().getName(), "Token="+jsonObject.getString("login")); resultDevice.setLogin(jsonObject.getString("login")); resultDevice.setPassword(jsonObject.getString("password")); resultDevice.setPermissions(jsonObject.getString("permissions")); resultDevice.setFirstSyncDone(false); resultDevice.setSyncCalls(false); resultDevice.setSyncContacts(false); resultDevice.setSyncFiles(false); resultDevice.setSyncNotes(false); resultDevice.save(); } else { errorMessage = jsonObject.getString("error"); } } in.close(); conn.disconnect(); } catch (MalformedURLException e) { errorMessage = e.getLocalizedMessage(); } catch (ProtocolException e) { errorMessage = e.getLocalizedMessage(); } catch (IOException e) { errorMessage = e.getLocalizedMessage(); } catch (JSONException e) { errorMessage = e.getLocalizedMessage(); } return resultDevice; }
From source file:org.apache.olingo.fit.tecsvc.http.AcceptHeaderAcceptCharsetHeaderITCase.java
@Test public void validFormatWithIncorrectMultipleAcceptCharsetHeader1() throws Exception { URL url = new URL(SERVICE_URI + "ESAllPrim?$format=json"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(HttpMethod.GET.name()); connection.setRequestProperty(HttpHeader.ACCEPT_CHARSET, "utf<8,utf-8;q=0.8"); connection.connect();// ww w. j a v a 2s. co m assertEquals(HttpStatusCode.BAD_REQUEST.getStatusCode(), connection.getResponseCode()); final String content = IOUtils.toString(connection.getErrorStream()); assertTrue(content.contains("The Accept charset header 'utf<8' is not supported.")); }
From source file:org.apache.olingo.fit.tecsvc.http.AcceptHeaderAcceptCharsetHeaderITCase.java
@Test public void acceptHeaderWithIncorrectParams() throws Exception { URL url = new URL(SERVICE_URI + "ESAllPrim"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(HttpMethod.GET.name()); connection.setRequestProperty(HttpHeader.ACCEPT, "application/json;abc=xyz"); connection.connect();/*from w w w . j a va 2 s . co m*/ assertEquals(HttpStatusCode.NOT_ACCEPTABLE.getStatusCode(), connection.getResponseCode()); final String content = IOUtils.toString(connection.getErrorStream()); assertTrue(content.contains("The content-type range 'application/json;abc=xyz' is not " + "supported as value of the Accept header.")); }
From source file:org.apache.olingo.fit.tecsvc.http.AcceptHeaderAcceptCharsetHeaderITCase.java
@Test public void validFormatWithIncorrectMultipleAcceptCharsetHeader2() throws Exception { URL url = new URL(SERVICE_URI + "ESAllPrim?$format=json"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(HttpMethod.GET.name()); connection.setRequestProperty(HttpHeader.ACCEPT_CHARSET, "utf-8;q=0.8,utf-8;q=<"); connection.connect();/*from w ww .j a v a2 s . c o m*/ assertEquals(HttpStatusCode.BAD_REQUEST.getStatusCode(), connection.getResponseCode()); final String content = IOUtils.toString(connection.getErrorStream()); assertTrue(content.contains("The Accept charset header 'utf-8;q=<' is not supported.")); }
From source file:com.hp.mqm.atrf.core.rest.RestConnector.java
/** * @param con that already connected to it's url with an http request, and that should contain a * response for us to retrieve * @return a response from the server to the previously submitted http request * @throws Exception//from w ww .j a v a 2s . co m */ private Response retrieveHtmlResponse(HttpURLConnection con) throws IOException { Response ret = new Response(); InputStream inputStream; //select the source of the input bytes, first try "regular" input try { inputStream = con.getInputStream(); } catch (Exception e) { /*if the connection to the server somehow failed, for example 404 or 500, con.getInputStream() will throw an exception, which we'll keep. we'll also store the body of the exception page, in the response data. */ inputStream = con.getErrorStream(); ret.setFailure(e); ret.setResponseData(e.getMessage());//set default error message } //this actually takes the data from the previously decided stream (error or input) and stores it in a byte[] inside the response if (inputStream != null) { ByteArrayOutputStream container = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int read; while ((read = inputStream.read(buf, 0, 1024)) > 0) { container.write(buf, 0, read); } ret.setResponseData(container.toString()); } try { ret.setStatusCode(con.getResponseCode()); } catch (Exception e) { ret.setStatusCode(0); } ret.setResponseHeaders(con.getHeaderFields()); return ret;/**/ }