List of usage examples for java.net HttpURLConnection HTTP_OK
int HTTP_OK
To view the source code for java.net HttpURLConnection HTTP_OK.
Click Source Link
From source file:Main.java
public static byte[] getHtmlByteArray(final String url) { URL htmlUrl = null;/* w w w .ja v a2s. c o m*/ InputStream inStream = null; try { htmlUrl = new URL(url); URLConnection connection = htmlUrl.openConnection(); HttpURLConnection httpConnection = (HttpURLConnection) connection; int responseCode = httpConnection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { inStream = httpConnection.getInputStream(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } byte[] data = inputStreamToByte(inStream); return data; }
From source file:Main.java
public static String getResponse(String urlParam) { try {//from w w w . ja va 2 s . c o m URL url = new URL(urlParam); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE6.0; Windows NT 5.1; SV1)"); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { InputStream inputStream = conn.getInputStream(); return inputStream2String(inputStream); } } catch (IOException e) { e.printStackTrace(); return null; } return null; }
From source file:Main.java
public static Bitmap downLoadBitmap(String httpUrl) { InputStream inputStream = null; try {//from ww w . j a v a2 s .co m URL url = new URL(httpUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setDoInput(true); conn.setReadTimeout(5000); conn.setConnectTimeout(5000); conn.connect(); int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { inputStream = conn.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); return bitmap; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }
From source file:Main.java
private static String httpPost(String address, String params) { InputStream inputStream = null; HttpURLConnection urlConnection = null; try {/*from ww w .j a v a 2 s. co m*/ URL url = new URL(address); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setDoOutput(true); urlConnection.setRequestMethod("POST"); urlConnection.getOutputStream().write(params.getBytes()); urlConnection.getOutputStream().flush(); urlConnection.connect(); if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { inputStream = urlConnection.getInputStream(); int len = 0; byte[] buffer = new byte[1024]; StringBuffer stringBuffer = new StringBuffer(); while ((len = inputStream.read(buffer)) != -1) { stringBuffer.append(new String(buffer, 0, len)); } return stringBuffer.toString(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { close(inputStream); if (urlConnection != null) { urlConnection.disconnect(); } } return null; }
From source file:Main.java
public static boolean selectServer() { boolean status = false; for (String host : host_arr) { String str = host + Servlet_phone; HttpURLConnection conn = null; try {/*from www. ja v a 2s. c o m*/ conn = (HttpURLConnection) new URL(str).openConnection(); conn.setConnectTimeout(4000); int result = conn.getResponseCode(); if (result == HttpURLConnection.HTTP_OK) { HOST = str;// HOST_PORT = host; status = true; break; } else { } } catch (MalformedURLException e) { } catch (IOException e) { } finally { if (conn != null) { conn.disconnect(); conn = null; } } } return status; }
From source file:Main.java
/** * Return '' or error message if error occurs during URL connection. * /* w w w . ja v a 2 s .c o m*/ * @param url The URL to ckeck * @return */ public static String getUrlStatus(String url) { URL u; URLConnection conn; int connectionTimeout = 500; try { u = new URL(url); conn = u.openConnection(); conn.setConnectTimeout(connectionTimeout); // TODO : set proxy if (conn instanceof HttpURLConnection) { HttpURLConnection httpConnection = (HttpURLConnection) conn; httpConnection.setInstanceFollowRedirects(true); httpConnection.connect(); httpConnection.disconnect(); // FIXME : some URL return HTTP200 with an empty reply from server // which trigger SocketException unexpected end of file from server int code = httpConnection.getResponseCode(); if (code == HttpURLConnection.HTTP_OK) { return ""; } else { return "Status: " + code; } } // TODO : Other type of URLConnection } catch (Exception e) { e.printStackTrace(); return e.toString(); } return ""; }
From source file:com.grosscommerce.ICEcat.utilities.Downloader.java
public static void download(String urlFrom, String login, String pwd, OutputStream destStream) throws Exception { try {//w w w .java 2s .c om HttpURLConnection uc = prepareConnection(urlFrom, login, pwd); uc.connect(); if (uc.getResponseCode() != HttpURLConnection.HTTP_OK) { Logger.getLogger(Downloader.class.getName()).log(Level.INFO, "Error, code: {0}, message {1}", new Object[] { uc.getResponseCode(), uc.getResponseMessage() }); return; } BufferedInputStream is = null; if ((uc.getContentEncoding() != null && uc.getContentEncoding().toLowerCase().equals("gzip")) || uc.getContentType() != null && uc.getContentType().toLowerCase().contains("gzip")) { is = new BufferedInputStream(new GZIPInputStream(uc.getInputStream())); Logger.getLogger(Downloader.class.getName()).log(Level.INFO, "Will download gzip data from: {0}", urlFrom); } else { is = new BufferedInputStream(uc.getInputStream()); Logger.getLogger(Downloader.class.getName()).log(Level.INFO, "Will download not compressed data from:{0}", urlFrom); } StreamsHelper.copy(is, destStream); destStream.flush(); } catch (Exception ex) { Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, "URL: " + urlFrom, ex); throw ex; } }
From source file:com.gabrielluz.megasenaanalyzer.MegaSenaAnalyzer.java
public static byte[] downloadFile(String fileUrl) throws IOException { java.net.CookieManager cm;/* ww w .j a va 2 s .c o m*/ cm = new java.net.CookieManager(); java.net.CookieHandler.setDefault(cm); URL url = new URL(fileUrl); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); int responseCode = httpConn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { try (InputStream inputStream = httpConn.getInputStream()) { return MegaSenaAnalyzer.unZipIt(inputStream); } } else { System.out.println("No file to download. Server replied HTTP code: " + responseCode); httpConn.disconnect(); return null; } }
From source file:org.elegosproject.romupdater.DownloadManager.java
public static boolean checkHttpFile(String url) { try {// ww w .j av a2s . c o m HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, 3000); Log.i(TAG, "Testing " + url + "..."); URL theUrl = new URL(url); HttpURLConnection connection = (HttpURLConnection) theUrl.openConnection(); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { connection.disconnect(); } else { Log.i(TAG, "HTTP Response code: " + connection.getResponseCode()); return false; } } catch (IOException e) { Log.e(TAG, e.toString()); return false; } return true; }
From source file:Main.java
public static String UpdateScore(String userName, int br) { String retStr = ""; try {/* w w w . j a va2 s . c o m*/ URL url = new URL("http://" + IP_ADDRESS + "/RiddleQuizApp/ServerSide/AzurirajHighScore.php"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(15000); conn.setReadTimeout(10000); conn.setRequestMethod("POST"); conn.setDoInput(true); conn.setDoOutput(true); Log.e("http", "por1"); JSONObject data = new JSONObject(); data.put("username", userName); data.put("broj", br); Log.e("http", "por3"); Uri.Builder builder = new Uri.Builder().appendQueryParameter("action", data.toString()); String query = builder.build().getEncodedQuery(); Log.e("http", "por4"); OutputStream os = conn.getOutputStream(); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); bw.write(query); Log.e("http", "por5"); bw.flush(); bw.close(); os.close(); int responseCode = conn.getResponseCode(); Log.e("http", String.valueOf(responseCode)); if (responseCode == HttpURLConnection.HTTP_OK) { retStr = inputStreamToString(conn.getInputStream()); } else retStr = String.valueOf("Error: " + responseCode); Log.e("http", retStr); } catch (Exception e) { Log.e("http", "greska"); } return retStr; }