List of usage examples for java.net HttpURLConnection setRequestMethod
public void setRequestMethod(String method) throws ProtocolException
From source file:Main.java
public static String readTextFromURL(String urlString) throws IOException { HttpURLConnection urlConnection = null; URL url = new URL(urlString); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setReadTimeout(10000 /* milliseconds */); urlConnection.setConnectTimeout(15000 /* milliseconds */); urlConnection.setDoOutput(true);//ww w . j a v a2 s . c o m urlConnection.connect(); BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); char[] buffer = new char[1024]; String jsonString = new String(); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); return sb.toString(); }
From source file:org.cytoscape.app.internal.net.server.CyHttpdImplTest.java
private static HttpURLConnection connectToURL(String urlString, String method) throws Exception { final URL url = new URL(urlString); final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(method); connection.setDoOutput(true);// w ww . j a v a 2s . com connection.connect(); return connection; }
From source file:org.elegosproject.romupdater.DownloadManager.java
public static boolean sendAnonymousData() { String link = "http://www.elegosproject.org/android/upload.php"; String data;/*from w w w. j a v a2s.c o m*/ SharedData shared = SharedData.getInstance(); String romName = shared.getRepositoryROMName(); String romVersion = shared.getDownloadVersion(); String romPhone = shared.getRepositoryModel(); String romRepository = shared.getRepositoryUrl(); if (romName.equals("") || romVersion.equals("") || romPhone.equals("") || romRepository.equals("")) { Log.e(TAG, "Internal error - missing system variables."); return false; } if (!checkHttpFile(link)) return false; try { data = URLEncoder.encode("phone", "UTF-8") + "=" + URLEncoder.encode(romPhone, "UTF-8"); data += "&" + URLEncoder.encode("rom_name", "UTF-8") + "=" + URLEncoder.encode(romName, "UTF-8"); data += "&" + URLEncoder.encode("rom_version", "UTF-8") + "=" + URLEncoder.encode(romVersion, "UTF-8"); data += "&" + URLEncoder.encode("rom_repository", "UTF-8") + "=" + URLEncoder.encode(romRepository, "UTF-8"); HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, 3000); URL url = new URL(link); url.openConnection(); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("User-Agent", "ROMUpdater"); conn.setDoOutput(true); PrintWriter out = new PrintWriter(conn.getOutputStream()); out.println(data); out.close(); int status = Integer.parseInt(conn.getHeaderField("ROMUpdater-status")); if (status == 1) return true; Log.e(TAG, "It was impossible to send data to the stastistics server (" + conn.getHeaderField("ROMUpdater-error") + ")."); return false; } catch (Exception e) { Log.e(TAG, "It was impossible to send data to the stastistics server."); Log.e(TAG, "Error: " + e.toString()); return false; } }
From source file:com.telefonica.iot.perseo.test.Help.java
public static Res sendMethod(String url, String body, String method) throws Exception { URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod(method); con.setDoOutput(true);/* w ww . j a v a 2 s.com*/ DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(body); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); String responseBody = getBodyResponse(con); return new Res(responseCode, responseBody); }
From source file:Main.java
public static void getNewsJSON(final String url, final Handler handler) { new Thread(new Runnable() { @Override/*from www .j a v a 2 s . c o m*/ public void run() { HttpURLConnection conn; InputStream is; try { conn = (HttpURLConnection) new URL(url).openConnection(); conn.setRequestMethod("GET"); is = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line = ""; StringBuffer result = new StringBuffer(); while ((line = reader.readLine()) != null) { result.append(line); } Message msg = new Message(); msg.obj = result.toString(); handler.sendMessage(msg); } catch (Exception e) { e.printStackTrace(); } } }).start(); }
From source file:Main.java
/** * Pings a HTTP URL. This effectively sends a HEAD request and returns <code>true</code> if the response code is in * the 200-399 range./*from ww w.j a va 2s . co m*/ * @param url The HTTP URL to be pinged. * @param timeout The timeout in millis for both the connection timeout and the response read timeout. Note that * the total timeout is effectively two times the given timeout. * @return <code>true</code> if the given HTTP URL has returned response code 200-399 on a HEAD request within the * given timeout, otherwise <code>false</code>. */ public static boolean ping(String url, int timeout) { // Otherwise an exception may be thrown on invalid SSL certificates: url = url.replaceFirst("^https", "http"); try { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setConnectTimeout(timeout); connection.setReadTimeout(timeout); connection.setRequestMethod("HEAD"); int responseCode = connection.getResponseCode(); return (200 <= responseCode && responseCode <= 399); } catch (IOException exception) { return false; } }
From source file:Main.java
public static String getToken(String email, String password) throws IOException { // Create the post data // Requires a field with the email and the password StringBuilder builder = new StringBuilder(); builder.append("Email=").append(email); builder.append("&Passwd=").append(password); builder.append("&accountType=GOOGLE"); builder.append("&source=markson.visuals.sitapp"); builder.append("&service=ac2dm"); // Setup the Http Post byte[] data = builder.toString().getBytes(); URL url = new URL("https://www.google.com/accounts/ClientLogin"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setUseCaches(false);// w w w .jav a 2s . c om con.setDoOutput(true); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); con.setRequestProperty("Content-Length", Integer.toString(data.length)); // Issue the HTTP POST request OutputStream output = con.getOutputStream(); output.write(data); output.close(); // Read the response BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream())); String line = null; String auth_key = null; while ((line = reader.readLine()) != null) { if (line.startsWith("Auth=")) { auth_key = line.substring(5); } } // Finally get the authentication token // To something useful with it return auth_key; }
From source file:Main.java
public static byte[] executePost(String url, byte[] data, Map<String, String> requestProperties) throws IOException { HttpURLConnection urlConnection = null; try {/*from w w w . j a v a 2 s. c o m*/ urlConnection = (HttpURLConnection) new URL(url).openConnection(); urlConnection.setRequestMethod("POST"); urlConnection.setDoOutput(data != null); urlConnection.setDoInput(true); if (requestProperties != null) { for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) { urlConnection.setRequestProperty(requestProperty.getKey(), requestProperty.getValue()); } } if (data != null) { OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream()); out.write(data); out.close(); } InputStream in = new BufferedInputStream(urlConnection.getInputStream()); return convertInputStreamToByteArray(in); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } }
From source file:gov.nist.healthcare.ttt.webapp.common.controller.GetCCDADocumentsController.java
public static JSONObject getHTML(String urlToRead) throws Exception { StringBuilder result = new StringBuilder(); URL url = new URL(urlToRead); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line;//from ww w. java2 s . c o m while ((line = rd.readLine()) != null) { result.append(line); } rd.close(); return new JSONObject(result.toString()); }
From source file:edu.xjtu.qxcamerabridge.LiveviewImageExtractor.java
private static InputStream getLiveviewInputStream(String liveviewURL) throws MalformedURLException, IOException { URL url = new URL(liveviewURL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(2000);//from w w w . j a v a2 s .c o m connection.connect(); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { return connection.getInputStream(); } return null; }