List of usage examples for java.net HttpURLConnection setRequestMethod
public void setRequestMethod(String method) throws ProtocolException
From source file:com.codelanx.codelanxlib.util.auth.UUIDFetcher.java
/** * Opens the connection to Mojang's profile API * //ww w . jav a 2s . c o m * @since 0.0.1 * @version 0.0.1 * * @return The {@link HttpURLConnection} object to the API server * @throws IOException If there is a problem opening the stream, a malformed * URL, or if there is a ProtocolException */ private static HttpURLConnection createConnection() throws IOException { URL url = new URL(UUIDFetcher.PROFILE_URL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); return connection; }
From source file:gr.scify.newsum.Utils.java
public static boolean urlChanged(String url) { try {//from w ww .j a v a2 s . com HttpURLConnection.setFollowRedirects(false); HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); con.setRequestMethod("HEAD"); return (con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED); } catch (Exception e) { e.printStackTrace(); return false; } }
From source file:com.intellij.lang.jsgraphql.languageservice.JSGraphQLNodeLanguageServiceClient.java
private static <R> R executeRequest(Request request, Class<R> responseClass, @NotNull Project project, boolean setProjectDir) { URL url = getJSGraphQLNodeLanguageServiceInstance(project, setProjectDir); if (url == null) { return null; }/*ww w .j a va 2s.com*/ HttpURLConnection httpConnection = null; try { httpConnection = (HttpURLConnection) url.openConnection(); httpConnection.setConnectTimeout(50); httpConnection.setReadTimeout(1000); httpConnection.setRequestMethod("POST"); httpConnection.setDoOutput(true); httpConnection.setRequestProperty("Content-Type", "application/json"); httpConnection.connect(); try (OutputStreamWriter writer = new OutputStreamWriter(httpConnection.getOutputStream())) { final String jsonRequest = new Gson().toJson(request); writer.write(jsonRequest); writer.flush(); writer.close(); } if (httpConnection.getResponseCode() == 200) { if (responseClass == null) { return null; } try (InputStream inputStream = httpConnection.getInputStream()) { String jsonResponse = IOUtils.toString(inputStream, "UTF-8"); R response = new Gson().fromJson(jsonResponse, responseClass); return response; } } else { log.warn("Got error from JS GraphQL Language Service: HTTP " + httpConnection.getResponseCode() + ": " + httpConnection.getResponseMessage()); } } catch (IOException e) { log.warn("Unable to connect to dev server", e); } finally { if (httpConnection != null) { httpConnection.disconnect(); } } return null; }
From source file:Main.java
private static InputStream getInputStreamFromUrl_V9(Context paramContext, String paramString) { if (confirmDownload(paramContext, paramString)) { try {/*w w w .ja v a2 s .c o m*/ URL localURL = new URL(paramString); HttpURLConnection localHttpURLConnection2 = (HttpURLConnection) localURL.openConnection(); localHttpURLConnection2.setRequestProperty("Accept-Charset", "UTF-8"); localHttpURLConnection2.setReadTimeout(30000); localHttpURLConnection2.setConnectTimeout(30000); localHttpURLConnection2.setRequestMethod("GET"); localHttpURLConnection2.setDoInput(true); localHttpURLConnection2.connect(); return localHttpURLConnection2.getInputStream(); } catch (Throwable localThrowable) { localThrowable.printStackTrace(); return null; } } else { return null; } }
From source file:Main.java
private static void post(String endpoint, Map<String, String> params) throws IOException { URL url;/*from w w w.ja va2 s .c o m*/ try { url = new URL(endpoint); } catch (MalformedURLException e) { throw new IllegalArgumentException("invalid url: " + endpoint); } StringBuilder bodyBuilder = new StringBuilder(); Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator(); // constructs the POST body using the parameters while (iterator.hasNext()) { Map.Entry<String, String> param = iterator.next(); bodyBuilder.append(param.getKey()).append('=').append(param.getValue()); if (iterator.hasNext()) { bodyBuilder.append('&'); } } String body = bodyBuilder.toString(); Log.v(TAG, "Posting '" + body + "' to " + url); byte[] bytes = body.getBytes(); HttpURLConnection conn = null; try { Log.e("URL", "> " + url); conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setUseCaches(false); conn.setFixedLengthStreamingMode(bytes.length); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); // post the request OutputStream out = conn.getOutputStream(); out.write(bytes); out.close(); // handle the response int status = conn.getResponseCode(); if (status != 200) { throw new IOException("Post failed with error code " + status); } } finally { if (conn != null) { conn.disconnect(); } } }
From source file:Main.java
public static String getHTML(String urlToRead) { URL url; // The URL to read HttpURLConnection conn; // The actual connection to the web page BufferedReader rd; // Used to read results from the web page String line; // An individual line of the web page HTML String result = ""; // A long string containing all the HTML try {/*w w w . ja v a 2 s .co m*/ url = new URL(urlToRead); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((line = rd.readLine()) != null) { result += line; } rd.close(); } catch (Exception e) { // e.printStackTrace(); } return result; }
From source file:com.meetingninja.csse.database.ProjectDatabaseAdapter.java
protected static String updateHelper(String jsonPayload) throws IOException { // Server URL setup String _url = getBaseUri().build().toString(); // Establish connection URL url = new URL(_url); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // add request header conn.setRequestMethod(IRequest.PUT); addRequestHeader(conn, true);//from ww w . j a v a 2 s. com sendPostPayload(conn, jsonPayload); String response = getServerResponse(conn); conn.disconnect(); return response; }
From source file:com.meetingninja.csse.database.ProjectDatabaseAdapter.java
public static void deleteProject(Project p) throws IOException { // Server URL setup String _url = getBaseUri().appendPath(p.getProjectID()).build().toString(); // Establish connection URL url = new URL(_url); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // add request header conn.setRequestMethod("DELETE"); addRequestHeader(conn, false);/*from w w w .ja va 2 s .c o m*/ // Get server response conn.getResponseCode(); getServerResponse(conn); }
From source file:com.meetingninja.csse.database.ProjectDatabaseAdapter.java
public static Project getProject(Project p) throws IOException { // Server URL setup String _url = getBaseUri().appendPath(p.getProjectID()).build().toString(); // Establish connection URL url = new URL(_url); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // add request header conn.setRequestMethod("GET"); addRequestHeader(conn, false);/*from w ww . j a va 2 s . c o m*/ // Get server response conn.getResponseCode(); String response = getServerResponse(conn); JsonNode projectNode = MAPPER.readTree(response); return parseProject(projectNode, p); }
From source file:gov.nasa.arc.geocam.geocam.HttpPost.java
public static int post(String url, Map<String, String> vars, String fileKey, String fileName, InputStream istream, String username, String password) throws IOException { HttpURLConnection conn = createConnection(url, username, password); try {//from w w w . j a v a2 s .com conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); Log.d("HttpPost", vars.toString()); DataOutputStream out = new DataOutputStream(conn.getOutputStream()); assembleMultipart(out, vars, fileKey, fileName, istream); istream.close(); out.flush(); InputStream in = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in), 2048); // Set postedSuccess to true if there is a line in the HTTP response in // the form "GEOCAM_SHARE_POSTED <file>" where <file> equals fileName. // Our old success condition was just checking for HTTP return code 200; turns // out that sometimes gives false positives. Boolean postedSuccess = false; for (String line = reader.readLine(); line != null; line = reader.readLine()) { //Log.d("HttpPost", line); if (!postedSuccess && line.contains("GEOCAM_SHARE_POSTED")) { String[] vals = line.trim().split("\\s+"); for (int i = 0; i < vals.length; ++i) { //String filePosted = vals[1]; if (vals[i].equals(fileName)) { Log.d(GeoCamMobile.DEBUG_ID, line); postedSuccess = true; break; } } } } out.close(); int responseCode = 0; responseCode = conn.getResponseCode(); if (responseCode == 200 && !postedSuccess) { // our code for when we got value 200 but no confirmation responseCode = -3; } return responseCode; } catch (UnsupportedEncodingException e) { throw new IOException("HttpPost - Encoding exception: " + e); } // ??? when would this ever be thrown? catch (IllegalStateException e) { throw new IOException("HttpPost - IllegalState: " + e); } catch (IOException e) { try { return conn.getResponseCode(); } catch (IOException f) { throw new IOException("HttpPost - IOException: " + e); } } }