List of usage examples for java.net HttpURLConnection setRequestMethod
public void setRequestMethod(String method) throws ProtocolException
From source file:com.reactivetechnologies.jaxrs.RestServerTest.java
static String sendGet(String url) throws IOException { StringBuilder response = new StringBuilder(); HttpURLConnection con = null; BufferedReader in = null;//from ww w. j a va2 s . c o m try { URL obj = new URL(url); con = (HttpURLConnection) obj.openConnection(); // optional default is GET con.setRequestMethod("GET"); //add request header con.setRequestProperty("User-Agent", "Mozilla/5.0"); int responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { response.append(inputLine); } } else { throw new IOException("Response Code: " + responseCode); } return response.toString(); } catch (IOException e) { throw e; } finally { if (in != null) { try { in.close(); } catch (IOException e) { } } if (con != null) { con.disconnect(); } } }
From source file:com.gallatinsystems.common.util.S3Util.java
public static boolean putObjectAcl(String bucketName, String objectKey, ACL acl, String awsAccessId, String awsSecretKey) throws IOException { final String date = getDate(); final URL url = new URL(String.format(S3_URL, bucketName, objectKey) + "?acl"); final String payload = String.format(PUT_PAYLOAD_ACL, date, acl.toString(), bucketName, objectKey); final String signature = MD5Util.generateHMAC(payload, awsSecretKey); HttpURLConnection conn = null; try {// w w w . jav a 2 s .c om conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("PUT"); conn.setRequestProperty("Date", date); conn.setRequestProperty("x-amz-acl", acl.toString()); conn.setRequestProperty("Authorization", "AWS " + awsAccessId + ":" + signature); int status = conn.getResponseCode(); if (status != 200 && status != 201) { log.severe("Error setting ACL for: " + url.toString()); log.severe(IOUtils.toString(conn.getInputStream())); return false; } return true; } finally { if (conn != null) { conn.disconnect(); } } }
From source file:com.pubkit.network.PubKitNetwork.java
public static JSONObject sendPost(String apiKey, JSONObject jsonObject) { URL url;// w ww .j a v a 2 s. c o m HttpURLConnection connection = null; try { //Create connection url = new URL(PUBKIT_API_URL); String encodedData = jsonObject.toString(); byte[] postDataBytes = jsonObject.toString().getBytes("UTF-8"); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Accept", "application/json"); connection.setRequestProperty("Content-Length", "" + String.valueOf(postDataBytes.length)); connection.setRequestProperty("api_key", apiKey); connection.setUseCaches(false); connection.setDoOutput(true); //Send request DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.writeBytes(encodedData);//set data wr.flush(); //Get Response InputStream inputStream = connection.getErrorStream(); //first check for error. if (inputStream == null) { inputStream = connection.getInputStream(); } BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream)); String line; StringBuilder response = new StringBuilder(); while ((line = rd.readLine()) != null) { response.append(line); response.append('\r'); } wr.close(); rd.close(); String responseString = response.toString(); if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { return new JSONObject("{'error':'" + responseString + "'}"); } else { try { return new JSONObject(responseString); } catch (JSONException e) { Log.e("PUBKIT", "Error parsing data", e); } } } catch (Exception e) { Log.e("PUBKIT", "Network exception:", e); } finally { if (connection != null) { connection.disconnect(); } } return null; }
From source file:com.reactivetechnologies.jaxrs.RestServerTest.java
static String sendPost(String url, String content) throws IOException { StringBuilder response = new StringBuilder(); HttpURLConnection con = null; BufferedReader in = null;//from ww w.ja v a2 s. co m try { URL obj = new URL(url); con = (HttpURLConnection) obj.openConnection(); // optional default is GET con.setRequestMethod("POST"); //add request header con.setRequestProperty("User-Agent", "Mozilla/5.0"); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); // Send post request con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeUTF(content); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { response.append(inputLine); } } else { throw new IOException("Response Code: " + responseCode); } return response.toString(); } catch (IOException e) { throw e; } finally { if (in != null) { try { in.close(); } catch (IOException e) { } } if (con != null) { con.disconnect(); } } }
From source file:com.reactivetechnologies.jaxrs.RestServerTest.java
static String sendDelete(String url, String content) throws IOException { StringBuilder response = new StringBuilder(); HttpURLConnection con = null; BufferedReader in = null;// w w w .j a v a 2s . co m try { URL obj = new URL(url); con = (HttpURLConnection) obj.openConnection(); // optional default is GET con.setRequestMethod("DELETE"); //add request header con.setRequestProperty("User-Agent", "Mozilla/5.0"); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); // Send post request con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeUTF(content); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { response.append(inputLine); } } else { throw new IOException("Response Code: " + responseCode); } return response.toString(); } catch (IOException e) { throw e; } finally { if (in != null) { try { in.close(); } catch (IOException e) { } } if (con != null) { con.disconnect(); } } }
From source file:edu.hackathon.perseus.core.httpSpeedTest.java
public static String getRedirectUrl(REGION region) { String result = ""; switch (region) { case EU:/*from www. ja v a2 s. com*/ result = amazonEuDomain; break; case USA: result = amazonUsaDomain; break; case ASIA: result = amazonAsiaDomain; break; } System.out.println("Trying to get real IP address of " + result); try { /* HttpHead headRequest = new HttpHead(result); HttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(headRequest); final int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) { String location = response.getHeaders("Location")[0].toString(); String redirecturl = location.replace("Location: ", ""); result = redirecturl; } */ URL url = new URL(result); HttpURLConnection httpGetCon = (HttpURLConnection) url.openConnection(); httpGetCon.setInstanceFollowRedirects(false); httpGetCon.setRequestMethod("GET"); httpGetCon.setConnectTimeout(5000); //set timeout to 5 seconds httpGetCon.setRequestProperty("User-Agent", USER_AGENT); int status = httpGetCon.getResponseCode(); System.out.println("code: " + status); if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER) result = httpGetCon.getHeaderField("Location"); } catch (Exception e) { System.out.println("Exception is fired in redirector getter. error:" + e.getMessage()); e.printStackTrace(); } System.out.println("Real IP address is " + result); return result; }
From source file:edu.jhu.cvrg.timeseriesstore.opentsdb.TimeSeriesUtility.java
private static HttpURLConnection openHTTPConnection(String urlString, HttpVerbs verb) throws MalformedURLException, IOException { URL url = null;//from w w w. j a va 2 s .c o m HttpURLConnection conn = null; url = new URL(urlString); conn = (HttpURLConnection) url.openConnection(); switch (verb) { case POST: conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setDoInput(true); break; case PUT: conn.setRequestMethod("PUT"); conn.setDoOutput(true); break; case DELETE: conn.setRequestMethod("DELETE"); break; default: conn.setRequestMethod("GET"); break; } conn.setRequestProperty("Accept", "application/json"); conn.setRequestProperty("Content-type", "application/json"); return conn; }
From source file:com.keepsafe.switchboard.SwitchBoard.java
/** * Returns a String containing the server response from a GET request * @param address Valid http addess.//w w w . j a v a 2 s . co m * @param params String of params. Multiple params seperated with &. No leading ? in string * @return Returns String from server or null when failed. */ private static String readFromUrlGET(String address, String params) { if (address == null || params == null) return null; String completeUrl = address + "?" + params; if (DEBUG) Log.d(TAG, "readFromUrl(): " + completeUrl); try { URL url = new URL(completeUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setUseCaches(false); connection.setDoOutput(true); // get response InputStream is = connection.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(is); BufferedReader bufferReader = new BufferedReader(inputStreamReader, 8192); String line = ""; StringBuffer resultContent = new StringBuffer(); while ((line = bufferReader.readLine()) != null) { if (DEBUG) Log.d(TAG, line); resultContent.append(line); } bufferReader.close(); if (DEBUG) Log.d(TAG, "readFromUrl() result: " + resultContent.toString()); return resultContent.toString(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:fr.zcraft.zlib.tools.mojang.UUIDFetcher.java
/** * Opens a GET connection.//from w w w . jav a 2 s . c o m * * @param url The URL to connect to. * * @return A GET connection to this URL. * @throws IOException If an exception occurred while contacting the server. */ static private HttpURLConnection getGETConnection(String url) throws IOException { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestMethod("GET"); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); return connection; }
From source file:fr.zcraft.zlib.tools.mojang.UUIDFetcher.java
/** * Opens a POST connection./*from www. ja v a 2 s. c o m*/ * * @param url The URL to connect to. * * @return A POST connection to this URL. * @throws IOException If an exception occurred while contacting the server. */ static private HttpURLConnection getPOSTConnection(String url) throws IOException { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); return connection; }