List of usage examples for java.net HttpURLConnection getResponseCode
public int getResponseCode() throws IOException
From source file:Main.java
public static String requestData(String address) throws IOException { URL url = new URL(address); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000);//from ww w . j ava2s . c o m connection.setReadTimeout(5000); String data = null; InputStream is = null; if (connection.getResponseCode() == 200) { is = connection.getInputStream(); data = readFromStream(is); } if (is != null) { is.close(); } return data; }
From source file:com.magnet.plugin.common.helpers.URLHelper.java
public static boolean checkURL(String urlS) { FormattedLogger logger = new FormattedLogger(URLHelper.class); logger.append("checkURL:" + urlS); try {//ww w .j av a 2s . c o m URL url = new URL(urlS); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); logger.append("Response code: " + conn.getResponseCode()); logger.showInfoLog(); return true; } catch (IOException e) { logger.append(e.getMessage()); logger.showErrorLog(); } return false; }
From source file:Main.java
public static InputStream downloadURL(String link) throws IOException { URL url = new URL(link); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(10000);/*www .java2s .c o m*/ conn.setConnectTimeout(15000); conn.setRequestMethod("GET"); conn.setDoInput(true); conn.connect(); logInfo("downloadStatus: " + conn.getResponseCode()); return conn.getInputStream(); }
From source file:Main.java
private static String connect(String uri, String charsetName) { String result = ""; try {/* www . j a va2 s. c om*/ URL url = new URL(uri); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5 * 1000); if (Integer.parseInt(Build.VERSION.SDK) < 8) { System.setProperty("http.keepAlive", "false"); } if (conn.getResponseCode() == 200) { InputStream is = conn.getInputStream(); result = readData(is, charsetName); } conn.disconnect(); } catch (MalformedURLException e) { } catch (IOException e) { } catch (Exception e) { } return result; }
From source file:com.infosupport.service.LPRServiceCaller.java
public static JSONObject doGet(String urlString) { String json = null;/*from w w w . ja v a 2 s.c o m*/ try { URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "application/json"); if (conn.getResponseCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); } BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); String output; while ((output = br.readLine()) != null) { json = output; System.out.println(json + "from doGet"); } conn.disconnect(); } catch (IOException e) { Log.w(TAG, "IOException, waarschijnlijk geen internet connectie aanwezig..."); } JSONObject jsonObject = null; try { jsonObject = new JSONObject(json); } catch (JSONException e) { Log.e(TAG, "Kon geen JsonObject maken van het response"); } return jsonObject; }
From source file:org.droidparts.http.worker.HttpURLConnectionWorker.java
private static int connectAndGetResponseCodeOrThrow(HttpURLConnection conn) throws HTTPException { try {/*from w ww . j a v a 2 s . co m*/ conn.connect(); int respCode = conn.getResponseCode(); if (isErrorResponseCode(respCode)) { HTTPInputStream is = HTTPInputStream.getInstance(conn, (conn.getErrorStream() != null)); throw new HTTPException(respCode, is.readAndClose()); } return respCode; } catch (HTTPException e) { throw e; } catch (Exception e) { throw new HTTPException(e); } }
From source file:Main.java
public static void server2mobile(Context context, String type) { String serverPath = "http://shaunrain.zicp.net/FileUp/QueryServlet?type=" + type; try {// w w w . j a v a2 s .co m URL url = new URL(serverPath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(10000); conn.setRequestMethod("GET"); int code = conn.getResponseCode(); Log.d("code", code + ""); if (code == 200) { InputStream is = conn.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int len = 0; byte[] buffer = new byte[1024]; while ((len = is.read(buffer)) != -1) { baos.write(buffer, 0, len); } is.close(); conn.disconnect(); String result = new String(baos.toByteArray()); String[] results = result.split("[$]"); for (String r : results) { if (r.length() != 0) { URL json = new URL(r); HttpURLConnection con = (HttpURLConnection) json.openConnection(); con.setConnectTimeout(5000); con.setRequestMethod("GET"); int co = con.getResponseCode(); if (co == 200) { File jsonFile; if (r.endsWith("clear.json")) { jsonFile = new File( Environment.getExternalStorageDirectory() + "/traffic_json/clear.json"); } else if (r.endsWith("crowd.json")) { jsonFile = new File( Environment.getExternalStorageDirectory() + "/traffic_json/crowd.json"); } else if (r.endsWith("trouble.json")) { jsonFile = new File( Environment.getExternalStorageDirectory() + "/traffic_json/trouble.json"); } else { jsonFile = new File( Environment.getExternalStorageDirectory() + "/traffic_json/control.json"); } if (jsonFile.exists()) jsonFile.delete(); jsonFile.createNewFile(); try (BufferedReader reader = new BufferedReader( new InputStreamReader(con.getInputStream(), "gb2312")); BufferedWriter writer = new BufferedWriter(new FileWriter(jsonFile));) { String line = null; while ((line = reader.readLine()) != null) { writer.write(line); } } } con.disconnect(); } } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
public static String getRequest(String query) { HttpURLConnection connection = null; try {/*from w w w .jav a 2s . c o m*/ connection = (HttpURLConnection) new URL(url + query).openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Accept-Charset", charset); statusCode = connection.getResponseCode(); if (statusCode != 200) { return null; } InputStream response = connection.getInputStream(); BufferedReader bR = new BufferedReader(new InputStreamReader(response)); String line = ""; StringBuilder responseStrBuilder = new StringBuilder(); while ((line = bR.readLine()) != null) { responseStrBuilder.append(line); } response.close(); return responseStrBuilder.toString(); } catch (IOException e) { e.printStackTrace(); } return ""; }
From source file:Main.java
public static JSONObject postRequest(String query) { HttpURLConnection connection = null; try {/* w w w. j ava 2 s.c om*/ connection = (HttpURLConnection) new URL(url + query).openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Accept-Charset", charset); statusCode = connection.getResponseCode(); if (statusCode != 200) { return null; } InputStream response = connection.getInputStream(); BufferedReader bR = new BufferedReader(new InputStreamReader(response)); String line = ""; StringBuilder responseStrBuilder = new StringBuilder(); while ((line = bR.readLine()) != null) { responseStrBuilder.append(line); } response.close(); return new JSONObject(responseStrBuilder.toString()); } catch (IOException | JSONException e) { e.printStackTrace(); } return new JSONObject(); }
From source file:com.github.thorqin.webapi.oauth2.OAuthClient.java
public static <T> T obtainResource(String authorityServerUri, String accessToken, Class<T> type) throws IOException, OAuthException, ClassCastException { URL u = new URL(authorityServerUri); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.setRequestProperty("Authorization", "Bearer " + accessToken); if (conn.getResponseCode() == 401) { String wwwAuth = conn.getHeaderField("WWW-Authenticate"); if (wwwAuth == null) { throw new OAuthException("unauthorized_client"); } else {/* w w w . j av a2 s .c o m*/ throw new OAuthException(getHeadSubParameter(wwwAuth, "error"), getHeadSubParameter(wwwAuth, "error_description"), getHeadSubParameter(wwwAuth, "error_uri")); } } else if (conn.getResponseCode() == 400) { throw new OAuthException("invalid_request"); } else if (conn.getResponseCode() > 401 && conn.getResponseCode() < 500) { throw new OAuthException("access_denied"); } else if (conn.getResponseCode() >= 500) { throw new OAuthException("server_error"); } else if (conn.getResponseCode() != 200) throw new OAuthException("unsupported_response_type"); try (InputStream is = conn.getInputStream(); InputStreamReader reader = new InputStreamReader(is)) { T resource = Serializer.fromJson(reader, type); return resource; } }