List of usage examples for java.net HttpURLConnection setRequestMethod
public void setRequestMethod(String method) throws ProtocolException
From source file:test.LocationCrawler.java
private static String ProcessLocationRequest(String QueryItem) { try {// w w w . j a va 2 s . com QueryItem = URLEncoder.encode(QueryItem, "ISO-8859-1"); String URLStr = String.format( "https://maps.googleapis.com/maps/api/geocode/json?" + "address=%s&sensor=false&key=%s", QueryItem, //"AIzaSyAOkEu7MBxUj4t2pBq1GZ-0Td7cf7bOKTg"); //"AIzaSyCvzTx408371P7CtoXN8BejAAmBa0NUZbc"); "AIzaSyAIyQ3XaHrC9TQVMIs65IrwzREtnzqZRKw"); URL url = new URL(URLStr); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); if (connection.getResponseCode() == 200) { InputStream is = connection.getInputStream(); InputStreamReader isr = new InputStreamReader(is, "UTF-8"); BufferedReader br = new BufferedReader(isr); String sb = ""; String str; while ((str = br.readLine()) != null) { sb += (str); } br.close(); return sb; } else { return ""; } } catch (Exception e) { return ""; } }
From source file:Main.java
public static String executePost(String targetURL, String urlParameters) { try {//from w w w. ja v a 2s . c om HttpURLConnection connection = (HttpURLConnection) new URL(targetURL).openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); //connection.setRequestProperty("Accept-Charset", charset); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); OutputStream output = null; try { output = connection.getOutputStream(); output.write(urlParameters.getBytes()); } finally { if (output != null) try { output.flush(); output.close(); } catch (IOException logOrIgnore) { } } InputStream response = connection.getInputStream(); String contentType = connection.getHeaderField("Content-Type"); String responseStr = ""; if (true) { BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(response)); for (String line; (line = reader.readLine()) != null;) { //System.out.println(line); responseStr = responseStr + line; Thread.sleep(2); } } finally { if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) { } } } else { // It's likely binary content, use InputStream/OutputStream. System.out.println("Binary content"); } return responseStr; } catch (Exception e) { e.printStackTrace(); } return ""; }
From source file:Main.java
public static int sendMessage(String auth_token, String registrationId, String message) throws IOException { StringBuilder postDataBuilder = new StringBuilder(); postDataBuilder.append(PARAM_REGISTRATION_ID).append("=").append(registrationId); postDataBuilder.append("&").append(PARAM_COLLAPSE_KEY).append("=").append("0"); postDataBuilder.append("&").append("data.payload").append("=") .append(URLEncoder.encode("Lars war hier", UTF8)); byte[] postData = postDataBuilder.toString().getBytes(UTF8); // Hit the dm URL. URL url = new URL("https://android.clients.google.com/c2dm/send"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true);// ww w. jav a2s . c om conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); conn.setRequestProperty("Content-Length", Integer.toString(postData.length)); conn.setRequestProperty("Authorization", "GoogleLogin auth=" + auth_token); OutputStream out = conn.getOutputStream(); out.write(postData); out.close(); int responseCode = conn.getResponseCode(); return responseCode; }
From source file:com.cnaude.mutemanager.UUIDFetcher.java
private static HttpURLConnection createConnection() throws Exception { URL url = new URL(PROFILE_URL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setUseCaches(false);/*from ww w . j a v a 2s . c o m*/ connection.setDoInput(true); connection.setDoOutput(true); return connection; }
From source file:Main.java
public static String customrequestget(String url, HashMap<String, String> map, String method) { if (null != map) { int i = 0; for (Map.Entry<String, String> entry : map.entrySet()) { if (i == 0) { url = url + "?" + entry.getKey() + "=" + entry.getValue(); } else { url = url + "&" + entry.getKey() + "=" + entry.getValue(); }//from w w w . j ava2 s .com i++; } } try { URL murl = new URL(url); System.out.print(url); HttpURLConnection conn = (HttpURLConnection) murl.openConnection(); conn.setConnectTimeout(5 * 1000); conn.setRequestMethod(method); conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows XP; DigExt)"); InputStream inStream = conn.getInputStream(); String result = inputStream2String(inStream); conn.disconnect(); return result; } catch (Exception e) { // TODO: handle exception } return null; }
From source file:Main.java
public static String getRequest(String query) { HttpURLConnection connection = null; try {/*from w w w . j av a2s.c om*/ connection = (HttpURLConnection) new URL(url + query).openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Accept-Charset", charset); statusCode = connection.getResponseCode(); if (statusCode != 200) { return null; } InputStream response = connection.getInputStream(); BufferedReader bR = new BufferedReader(new InputStreamReader(response)); String line = ""; StringBuilder responseStrBuilder = new StringBuilder(); while ((line = bR.readLine()) != null) { responseStrBuilder.append(line); } response.close(); return responseStrBuilder.toString(); } catch (IOException e) { e.printStackTrace(); } return ""; }
From source file:Yak_Hax.Yak_Hax_Mimerme.PostRequest.java
public static String PostBodyRequest(String URL, String JSONRaw, String UserAgent) throws IOException { String type = "application/json"; URL u = new URL(URL); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.setDoOutput(true);/* w w w .ja v a 2 s .c o m*/ conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", type); conn.setRequestProperty("User-Agent", UserAgent); OutputStream os = conn.getOutputStream(); os.write(JSONRaw.getBytes()); os.flush(); os.close(); String response = null; DataInputStream input = new DataInputStream(conn.getInputStream()); while (null != ((response = input.readLine()))) { input.close(); return response; } return null; }
From source file:com.surfs.storage.common.util.HttpUtils.java
public static String invokeHttpForGet(String path, String... agrs) throws IOException { URL url = new URL(path); LogFactory.info("rest url:" + url.toString()); HttpURLConnection con = null; try {/*from w w w . j a v a 2 s . co m*/ con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); con.setConnectTimeout(10000); con.setReadTimeout(1000 * 60 * 30); con.setDoOutput(true); con.setDoInput(true); con.setUseCaches(false); for (String string : agrs) { con.setRequestProperty("Content-type", "application/json"); con.setRequestMethod("POST"); OutputStream out = con.getOutputStream(); out.write(string.getBytes("UTF-8")); } if (con.getResponseCode() != 200) throw new ConnectException(con.getResponseMessage()); InputStream is = con.getInputStream(); return readResponse(is); } catch (IOException e) { throw e; } finally { if (con != null) { con.disconnect(); } } }
From source file:com.intellectualcrafters.plot.uuid.UUIDFetcher.java
private static HttpURLConnection createConnection() throws Exception { final URL url = new URL(PROFILE_URL); final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setUseCaches(false);/* w w w.jav a2 s.c om*/ connection.setDoInput(true); connection.setDoOutput(true); return connection; }
From source file:Main.java
public static JSONObject postRequest(String query) { HttpURLConnection connection = null; try {//from ww w. ja v a 2 s .co m connection = (HttpURLConnection) new URL(url + query).openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Accept-Charset", charset); statusCode = connection.getResponseCode(); if (statusCode != 200) { return null; } InputStream response = connection.getInputStream(); BufferedReader bR = new BufferedReader(new InputStreamReader(response)); String line = ""; StringBuilder responseStrBuilder = new StringBuilder(); while ((line = bR.readLine()) != null) { responseStrBuilder.append(line); } response.close(); return new JSONObject(responseStrBuilder.toString()); } catch (IOException | JSONException e) { e.printStackTrace(); } return new JSONObject(); }