List of usage examples for java.net HttpURLConnection setDoInput
public void setDoInput(boolean doinput)
From source file:com.socialize.util.ImageUtils.java
public static Bitmap getBitmapFromURL(String src) { Bitmap myBitmap = null;// ww w .java 2 s .c o m HttpURLConnection connection = null; try { URL url = new URL(src); connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); myBitmap = BitmapFactory.decodeStream(input); } catch (IOException e) { e.printStackTrace(); myBitmap = null; } finally { try { connection.disconnect(); } catch (Exception ignored) { } } return myBitmap; }
From source file:gov.nasa.arc.geocam.geocam.HttpPost.java
public static String postFiles(String url, Map<String, String> vars, Map<String, File> files) { try {/* w w w. j a va 2s. c o m*/ HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); DataOutputStream out = new DataOutputStream(conn.getOutputStream()); assembleMultipartFiles(out, vars, files); InputStream in = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { sb.append(line); } return sb.toString(); } catch (UnsupportedEncodingException e) { return "Encoding exception: " + e; } catch (IOException e) { return "IOException: " + e; } catch (IllegalStateException e) { return "IllegalState: " + e; } }
From source file:com.example.admin.processingboilerplate.JsonIO.java
public static JSONObject pushJson(String requestURL, String jsonDataName, JSONObject json) { try {/*from ww w. j a v a 2 s. c om*/ String boundary = "===" + System.currentTimeMillis() + "==="; URL url = new URL(requestURL); //HttpURLConnection con = (HttpURLConnection) url.openConnection(); URLConnection uc = (url).openConnection(); HttpURLConnection con = requestURL.startsWith("https") ? (HttpsURLConnection) uc : (HttpURLConnection) uc; con.setUseCaches(false); con.setDoOutput(true); con.setDoInput(true); con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); con.setRequestProperty("User-Agent", USER_AGENT); OutputStream outputStream = con.getOutputStream(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, charset), true); writer.append("--" + boundary).append(CRLF); writer.append("Content-Disposition: form-data; name=\"" + "data" + "\"").append(CRLF); writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); writer.append(CRLF); writer.append(json.toString()).append(CRLF); writer.flush(); writer.append(CRLF).flush(); writer.append("--" + boundary + "--").append(CRLF); writer.close(); int status = con.getResponseCode(); if (status == HttpURLConnection.HTTP_OK) { StringBuilder sb = load(con); try { json = new JSONObject(sb.toString()); return json; } catch (JSONException e) { Log.e("loadJson", e.getMessage(), e); e.printStackTrace(); return new JSONObject(); } } else { throw new IOException("Server returned non-OK status: " + status); } } catch (IOException e) { e.printStackTrace(); } return new JSONObject(); }
From source file:com.openforevent.main.UserPosition.java
public static Map<String, Object> userPositionByIP(DispatchContext ctx, Map<String, ? extends Object> context) throws IOException { URL url = new URL("http://api.ipinfodb.com/v3/ip-city/?" + "key=" + ipinfodb_key + "&" + "&ip=" + default_ip + "&format=xml"); //URL url = new URL("http://ipinfodb.com/ip_query.php"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); // Set properties of the connection urlConnection.setRequestMethod("GET"); urlConnection.setDoInput(true); urlConnection.setDoOutput(true);// ww w. ja va2s. com urlConnection.setUseCaches(false); urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // Retrieve the output int responseCode = urlConnection.getResponseCode(); InputStream inputStream; if (responseCode == HttpURLConnection.HTTP_OK) { inputStream = urlConnection.getInputStream(); } else { inputStream = urlConnection.getErrorStream(); } StringWriter writer = new StringWriter(); IOUtils.copy(inputStream, writer, "UTF-8"); String theString = writer.toString(); Debug.logInfo("Get user position by IP stream is = ", theString); Map<String, Object> paramOut = FastMap.newInstance(); paramOut.put("stream", theString); return paramOut; }
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 w w. j a v a 2 s . com 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:com.illusionaryone.FrankerZAPIv1.java
@SuppressWarnings("UseSpecificCatch") private static JSONObject readJsonFromUrl(String urlAddress) { JSONObject jsonResult = new JSONObject("{}"); InputStream inputStream = null; URL urlRaw;// w ww .jav a2s. co m HttpURLConnection urlConn; String jsonText = ""; try { urlRaw = new URL(urlAddress); urlConn = (HttpURLConnection) urlRaw.openConnection(); urlConn.setDoInput(true); urlConn.setRequestMethod("GET"); urlConn.addRequestProperty("Content-Type", "application/json"); urlConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/44.0.2403.52 Safari/537.36 PhantomBotJ/2015"); urlConn.connect(); if (urlConn.getResponseCode() == 200) { inputStream = urlConn.getInputStream(); } else { inputStream = urlConn.getErrorStream(); } BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8"))); jsonText = readAll(rd); jsonResult = new JSONObject(jsonText); fillJSONObject(jsonResult, true, "GET", urlAddress, urlConn.getResponseCode(), "", "", jsonText); } catch (JSONException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "JSONException", ex.getMessage(), jsonText); com.gmt2001.Console.err.printStackTrace(ex); } catch (NullPointerException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "NullPointerException", ex.getMessage(), ""); com.gmt2001.Console.err.printStackTrace(ex); } catch (MalformedURLException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "MalformedURLException", ex.getMessage(), ""); com.gmt2001.Console.err.printStackTrace(ex); } catch (SocketTimeoutException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "SocketTimeoutException", ex.getMessage(), ""); com.gmt2001.Console.err.printStackTrace(ex); } catch (IOException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "IOException", ex.getMessage(), ""); com.gmt2001.Console.err.printStackTrace(ex); } catch (Exception ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "Exception", ex.getMessage(), ""); com.gmt2001.Console.err.printStackTrace(ex); } finally { if (inputStream != null) try { inputStream.close(); } catch (IOException ex) { fillJSONObject(jsonResult, false, "GET", urlAddress, 0, "IOException", ex.getMessage(), ""); com.gmt2001.Console.err.printStackTrace(ex); } } return (jsonResult); }
From source file:com.googlecode.osde.internal.igoogle.IgCredentials.java
/** * Makes a HTTP POST request for authentication. * * @param emailUserName the name of the user * @param password the password of the user * @param loginTokenOfCaptcha CAPTCHA token (Optional) * @param loginCaptchaAnswer answer of CAPTCHA token (Optional) * @return http response as a String//from w ww . j a v a2s. c o m * @throws IOException */ // TODO: Refactor requestAuthentication() utilizing HttpPost. private static String requestAuthentication(String emailUserName, String password, String loginTokenOfCaptcha, String loginCaptchaAnswer) throws IOException { // Prepare connection. URL url = new URL(URL_GOOGLE_LOGIN); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); urlConnection.setUseCaches(false); urlConnection.setRequestMethod("POST"); urlConnection.setRequestProperty(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded"); logger.fine("url: " + url); // Form the POST params. StringBuilder params = new StringBuilder(); params.append("Email=").append(emailUserName).append("&Passwd=").append(password) .append("&source=OSDE-01&service=ig&accountType=HOSTED_OR_GOOGLE"); if (loginTokenOfCaptcha != null) { params.append("&logintoken=").append(loginTokenOfCaptcha); } if (loginCaptchaAnswer != null) { params.append("&logincaptcha=").append(loginCaptchaAnswer); } // Send POST via output stream. OutputStream outputStream = null; try { outputStream = urlConnection.getOutputStream(); outputStream.write(params.toString().getBytes(IgHttpUtil.ENCODING)); outputStream.flush(); } finally { if (outputStream != null) { outputStream.close(); } } // Retrieve response. // TODO: Should the caller of this method need to know the responseCode? int responseCode = urlConnection.getResponseCode(); logger.fine("responseCode: " + responseCode); InputStream inputStream = (responseCode == HttpURLConnection.HTTP_OK) ? urlConnection.getInputStream() : urlConnection.getErrorStream(); logger.fine("inputStream: " + inputStream); try { String response = IOUtils.toString(inputStream, IgHttpUtil.ENCODING); return response; } finally { if (inputStream != null) { inputStream.close(); } } }
From source file:de.mas.telegramircbot.utils.images.ImgurUploader.java
public static String uploadImageAndGetLink(String clientID, byte[] image) throws IOException { URL url;/*from w w w. jav a2 s . c om*/ url = new URL(Settings.IMGUR_API_URL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); String dataImage = Base64.getEncoder().encodeToString(image); String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(dataImage, "UTF-8"); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Authorization", "Client-ID " + clientID); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.connect(); StringBuilder stb = new StringBuilder(); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); // Get the response BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = rd.readLine()) != null) { stb.append(line).append("\n"); } wr.close(); rd.close(); GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(ImgurResponse.class, new ImgurResponseDeserializer()); Gson gson = gsonBuilder.create(); // The JSON data try { ImgurResponse response = gson.fromJson(stb.toString(), ImgurResponse.class); return response.getLink(); } catch (Exception e) { e.printStackTrace(); } return stb.toString(); }
From source file:com.easy.facebook.android.util.Util.java
public static byte[] loadPicture(String urlPicture) { byte[] pictureData = null; try {/*from w ww. j a v a 2s .c o m*/ URL uploadFileUrl = null; try { uploadFileUrl = new URL(urlPicture); } catch (Exception e) { e.printStackTrace(); } HttpURLConnection conn = (HttpURLConnection) uploadFileUrl.openConnection(); conn.setDoInput(true); conn.connect(); InputStream fis = null; try { fis = conn.getInputStream(); } catch (IOException e) { e.printStackTrace(); } Bitmap bi = BitmapFactory.decodeStream(fis); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bi.compress(Bitmap.CompressFormat.JPEG, 100, baos); pictureData = baos.toByteArray(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return pictureData; }
From source file:de.cubeisland.engine.core.util.McUUID.java
private static HttpURLConnection postQuery(ArrayNode node, int page) throws IOException { HttpURLConnection con = (HttpURLConnection) new URL(MOJANG_API_URL_NAME_UUID + page).openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/json"); con.setUseCaches(false);//w ww.j a v a2s. c o m con.setDoInput(true); con.setDoOutput(true); DataOutputStream writer = new DataOutputStream(con.getOutputStream()); writer.write(node.toString().getBytes()); writer.close(); return con; }