List of usage examples for java.net HttpURLConnection setDoInput
public void setDoInput(boolean doinput)
From source file:Main.java
private static HttpURLConnection defineHttpsURLConnection(HttpURLConnection connection, JSONObject jsonObject) { try {/*w ww. j a va 2 s .c om*/ connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); /* Add headers to the request */ connection.setRequestProperty("Content-Type", "application/json;charset=utf-8"); DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.write(jsonObject.toString().getBytes(Charset.forName("UTF-8"))); wr.flush(); wr.close(); } catch (Exception e) { e.printStackTrace(); } return connection; }
From source file:Main.java
public static Bitmap getHttpBitmap(String url) { URL myFileUrl = null;//from w w w.j a v a 2 s.com Bitmap bitmap = null; try { myFileUrl = new URL(url); } catch (MalformedURLException e) { e.printStackTrace(); } try { HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection(); conn.setConnectTimeout(0); conn.setDoInput(true); conn.connect(); InputStream is = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(is); is.close(); } catch (IOException e) { e.printStackTrace(); } return bitmap; }
From source file:Main.java
public static String post(String url, Map<String, String> params) { try {//from w w w .j a va 2 s. co m URL u = new URL(url); HttpURLConnection connection = (HttpURLConnection) u.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); PrintWriter pw = new PrintWriter(connection.getOutputStream()); StringBuilder sbParams = new StringBuilder(); if (params != null) { for (String key : params.keySet()) { sbParams.append(key + "=" + params.get(key) + "&"); } } if (sbParams.length() > 0) { String strParams = sbParams.substring(0, sbParams.length() - 1); Log.e("cat", "strParams:" + strParams); pw.write(strParams); pw.flush(); pw.close(); } BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuffer response = new StringBuffer(); String readLine = ""; while ((readLine = br.readLine()) != null) { response.append(readLine); } br.close(); return response.toString(); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static Bitmap decodeBitmapFromURL(String src, boolean large) { // If the artwork returned null, don't want to try to show artwork if (src.equals("null")) { return null; }//from w w w . j a va 2 s . c o m if (large) { src = src.replace("large", "t500x500"); } InputStream inputStream = null; try { URL url = new URL(src); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); inputStream = connection.getInputStream(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (inputStream == null) { return null; } // Decided not to scale because would have to recreate input stream /* final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(inputStream, null, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; */ Bitmap returnBitmap = BitmapFactory.decodeStream(inputStream); return returnBitmap; }
From source file:Main.java
public static String loadImageFromUrl(Context context, String imageURL, File file) { try {//from w w w . j a v a2s .com URL url = new URL(imageURL); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(5 * 1000); con.setDoInput(true); con.connect(); if (con.getResponseCode() == 200) { InputStream inputStream = con.getInputStream(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024 * 20]; int length = -1; while ((length = inputStream.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, length); } byteArrayOutputStream.close(); inputStream.close(); FileOutputStream outputStream = new FileOutputStream(file); outputStream.write(byteArrayOutputStream.toByteArray()); outputStream.close(); return file.getPath(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; }
From source file:Main.java
public static Bitmap downLoadBitmap(String httpUrl) { InputStream inputStream = null; try {/*w w w.j av a 2s . c o m*/ URL url = new URL(httpUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setDoInput(true); conn.setReadTimeout(5000); conn.setConnectTimeout(5000); conn.connect(); int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { inputStream = conn.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); return bitmap; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }
From source file:Main.java
public static String executePost(String url, String parameters) throws IOException { URL request = new URL(url); HttpURLConnection connection = (HttpURLConnection) request.openConnection(); connection.setDoOutput(true);/* w w w. j a v a 2s .c o m*/ connection.setDoInput(true); connection.setInstanceFollowRedirects(false); connection.setRequestMethod("POST"); connection.setRequestProperty("User-Agent", "StripeConnectAndroid"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("charset", "utf-8"); connection.setRequestProperty("Content-Length", "" + Integer.toString(parameters.getBytes().length)); connection.setUseCaches(false); DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.writeBytes(parameters); wr.flush(); wr.close(); String response = streamToString(connection.getInputStream()); connection.disconnect(); return response; }
From source file:cc.vileda.sipgatesync.api.SipgateApi.java
@NonNull private static String getUrl(final String apiUrl, final String token) throws IOException { final HttpURLConnection urlConnection = getConnection(apiUrl); urlConnection.setDoInput(true); urlConnection.setRequestMethod("GET"); if (token != null) { urlConnection.setRequestProperty("Authorization", "Bearer " + token); }/*from ww w . j a v a2 s . c o m*/ StringBuilder sb = new StringBuilder(); int HttpResult = urlConnection.getResponseCode(); if (HttpResult == HttpURLConnection.HTTP_OK) { BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "utf-8")); String line; while ((line = br.readLine()) != null) { sb.append(line).append("\n"); } br.close(); return sb.toString(); } else { System.out.println(urlConnection.getResponseMessage()); } return ""; }
From source file:cloudlens.parser.FileReader.java
public static InputStream fetchFile(String urlString) { try {/*from w w w. j a va2s . c om*/ InputStream inputStream; URL url; if (urlString.startsWith("local:")) { final String path = urlString.replaceFirst("local:", ""); inputStream = Files.newInputStream(Paths.get(path)); } else if (urlString.startsWith("file:")) { url = new URL(urlString); inputStream = Files.newInputStream(Paths.get(url.toURI())); } else if (urlString.startsWith("http:") || urlString.startsWith("https:")) { url = new URL(urlString); final HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); final Matcher matcher = Pattern.compile("//([^@]+)@").matcher(urlString); if (matcher.find()) { final String encoding = Base64.getEncoder().encodeToString(matcher.group(1).getBytes()); conn.setRequestProperty("Authorization", "Basic " + encoding); } conn.setRequestMethod("GET"); inputStream = conn.getInputStream(); } else { throw new CLException("supported protocols are: http, https, file, and local."); } return inputStream; } catch (IOException | URISyntaxException e) { throw new CLException(e.getMessage()); } }
From source file:dk.nsi.minlog.test.utils.TestHelper.java
public static String sendRequest(String url, String action, String docXml, boolean failOnError) throws IOException, ServiceException { URL u = new URL(url); HttpURLConnection uc = (HttpURLConnection) u.openConnection(); uc.setDoOutput(true);//from w w w . j a va 2s. c o m uc.setDoInput(true); uc.setRequestMethod("POST"); uc.setRequestProperty("SOAPAction", "\"" + action + "\""); uc.setRequestProperty("Content-Type", "text/xml; charset=utf-8;"); OutputStream os = uc.getOutputStream(); IOUtils.write(docXml, os, "UTF-8"); os.flush(); os.close(); InputStream is; if (uc.getResponseCode() != 200) { is = uc.getErrorStream(); } else { is = uc.getInputStream(); } String res = IOUtils.toString(is); is.close(); if (uc.getResponseCode() != 200 && (uc.getResponseCode() != 500 || failOnError)) { throw new ServiceException(res); } uc.disconnect(); return res; }