List of usage examples for java.net HttpURLConnection connect
public abstract void connect() throws IOException;
From source file:net.line2soft.preambul.utils.Network.java
/** * Checks if a connection is possible between device and server. * @param address The URL to request// w ww .j a va 2 s . c om * @return True if the connection is possible, false else */ public static boolean checkConnection(URL address) { boolean result = false; try { HttpURLConnection urlc = (HttpURLConnection) address.openConnection(); //urlc.setRequestProperty("User-Agent", "Android Application:"+Z.APP_VERSION); urlc.setRequestProperty("Connection", "close"); urlc.setConnectTimeout(1000); urlc.connect(); if (urlc.getResponseCode() == 200) { result = true; } } catch (Exception e) { ; } return result; }
From source file:com.blogspot.ryanfx.auth.GoogleUtil.java
/** * Sends the auth token to google and gets the json result. * @param token auth token//from w w w. j a v a2s. c o m * @return json result if valid request, null if invalid. * @throws IOException * @throws LoginException */ private static String issueTokenGetRequest(String token) throws IOException, LoginException { int timeout = 2000; URL u = new URL("https://www.googleapis.com/oauth2/v2/userinfo"); HttpURLConnection c = (HttpURLConnection) u.openConnection(); c.setRequestMethod("GET"); c.setRequestProperty("Content-length", "0"); c.setRequestProperty("Authorization", "OAuth " + token); c.setUseCaches(false); c.setAllowUserInteraction(false); c.setConnectTimeout(timeout); c.setReadTimeout(timeout); c.connect(); int status = c.getResponseCode(); if (status == HttpServletResponse.SC_OK) { BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); return sb.toString(); } else if (status == HttpServletResponse.SC_UNAUTHORIZED) { Logger.getLogger(GoogleUtil.class.getName()).severe("Invalid token request: " + token); } return null; }
From source file:Main.java
private static String httpPost(String address, String params) { InputStream inputStream = null; HttpURLConnection urlConnection = null; try {//from w w w. ja va 2 s.c o 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:com.android.idearse.Result.java
public static Bitmap getBitmapFromURL(String src) { try {//from ww w. j a v a 2 s. c o m URL url = new URL(src); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); Bitmap myBitmap = BitmapFactory.decodeStream(input); return myBitmap; } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:lu.list.itis.dkd.aig.util.FusekiHttpHelper.java
/** * Check if specified dataset already exists on server * /*w w w . j a v a 2 s.c o m*/ * @param dataSetName * @return boolean true if the dataset already exits, false if not * @throws IOException * @throws HttpException */ public static boolean chechIfExist(String dataSetName) throws IOException, HttpException { boolean exists = false; URL url = new URL(HOST + "/$/datasets/" + dataSetName); final HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(); httpConnection.setUseCaches(false); httpConnection.setRequestMethod("GET"); // handle HTTP/HTTPS strange behaviour httpConnection.connect(); httpConnection.disconnect(); // handle response switch (httpConnection.getResponseCode()) { case HttpURLConnection.HTTP_OK: exists = true; break; case HttpURLConnection.HTTP_NOT_FOUND: break; default: throw new HttpException( httpConnection.getResponseCode() + " message: " + httpConnection.getResponseMessage()); } return exists; }
From source file:fr.haploid.webservices.WebServicesHelper.java
protected static synchronized WebServicesResponseData downloadFromServer(String url, JSONObject params, String type) {/*from w w w . j av a 2s . c o m*/ int responseCode = 9999; StringBuffer buffer = new StringBuffer(); Uri.Builder builder = new Uri.Builder(); builder.encodedPath(url); JSONArray namesOfParams = params.names(); for (int i = 0; i < namesOfParams.length(); i++) { try { String param = namesOfParams.getString(i); builder.appendQueryParameter(param, params.get(param).toString()); } catch (JSONException e) { return new WebServicesResponseData("JSONException " + e.toString(), responseCode, false); } } URL urlBuild; try { urlBuild = new URL(builder.build().toString()); } catch (MalformedURLException e) { return new WebServicesResponseData("MalformedURLException " + e.toString(), responseCode, false); } try { HttpURLConnection urlConnection = (HttpURLConnection) urlBuild.openConnection(); try { urlConnection.setRequestMethod(type); } catch (ProtocolException e) { return new WebServicesResponseData("ProtocolException " + e.toString(), responseCode, false); } urlConnection.connect(); responseCode = urlConnection.getResponseCode(); if (responseCode < 400) { InputStream inputStream = urlConnection.getInputStream(); if (inputStream == null) { return new WebServicesResponseData(null, responseCode, false); } BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { buffer.append(line + "\n"); } if (buffer.length() == 0) { return new WebServicesResponseData(null, responseCode, false); } } } catch (IOException e) { return new WebServicesResponseData("IOException " + e.toString(), responseCode, false); } return new WebServicesResponseData(buffer.toString(), responseCode, true); }
From source file:dictinsight.utils.io.HttpUtils.java
public static String getDataFromOtherServer(String url) { String rec = null;/*from w ww . j ava 2 s .co m*/ BufferedReader reader = null; HttpURLConnection connection = null; try { URL srcUrl = new URL(url); connection = (HttpURLConnection) srcUrl.openConnection(); connection.setConnectTimeout(1000 * 10); connection.connect(); reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); rec = reader.readLine(); } catch (Exception e) { System.out.println("get date from " + url + " error!"); e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (connection != null) connection.disconnect(); } return rec; }
From source file:com.brobwind.brodm.NetworkUtils.java
public static void deviceInfo(String path, OnMessage callback) { try {/*w ww. j a v a 2 s .c o m*/ URL url = new URL(path + "/privet/info"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestProperty("Authorization", "Basic anonymous"); urlConnection.setRequestProperty("Content-Type", "application/json"); urlConnection.connect(); try { InputStream in = new BufferedInputStream(urlConnection.getInputStream()); BufferedReader bufReader = new BufferedReader(new InputStreamReader(in)); StringBuilder sb = new StringBuilder(); for (String line = bufReader.readLine(); line != null;) { sb.append(line).append("\n"); line = bufReader.readLine(); } callback.onMessage(sb.toString(), null); return; } finally { urlConnection.disconnect(); } } catch (MalformedURLException muex) { Log.e(TAG, "Malformed url: " + path); callback.onMessage(null, muex.getMessage()); } catch (Exception e) { e.printStackTrace(); callback.onMessage(null, e.getMessage()); } }
From source file:Main.java
public static String deleteUrl(String url, Bundle params) throws MalformedURLException, IOException { System.setProperty("http.keepAlive", "false"); HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setRequestProperty("User-Agent", System.getProperties().getProperty("http.agent") + " agent"); conn.setRequestMethod("DELETE"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setDoOutput(false);/*from w ww . j ava 2 s. c om*/ conn.setDoInput(true); //conn.setRequestProperty("Connection", "Keep-Alive"); conn.connect(); String response = ""; try { response = read(conn.getInputStream()); } catch (FileNotFoundException e) { // Error Stream contains JSON that we can parse to a FB error response = read(conn.getErrorStream()); } return response; }
From source file:Main.java
public static String getHtml(String getUrl, int outtime, String charsetName) { String html = ""; URL url;//from w ww. j a va 2 s.co m try { url = new URL(getUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1; CIBA)"); // connection.setRequestProperty("Connection", "Keep-Alive"); // connection.setRequestProperty("Cache-Control", "no-cache"); connection.setConnectTimeout(outtime); connection.connect(); InputStream inStrm = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(inStrm, charsetName)); String temp = ""; while ((temp = br.readLine()) != null) { html = html + (temp + '\n'); } try { br.close(); } catch (Exception e) { // TODO: handle exception } try { connection.disconnect(); } catch (Exception e) { // TODO: handle exception } } catch (Exception e) { e.printStackTrace(); } return html; }