List of usage examples for java.net HttpURLConnection setDoInput
public void setDoInput(boolean doinput)
From source file:Main.java
public static URI unredirect(URI uri) throws IOException { if (!REDIRECTOR_DOMAINS.contains(uri.getHost())) { return uri; }//from www . ja va2 s . com URL url = uri.toURL(); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setInstanceFollowRedirects(false); connection.setDoInput(false); connection.setRequestMethod("HEAD"); connection.setRequestProperty("User-Agent", "ZXing (Android)"); try { connection.connect(); switch (connection.getResponseCode()) { case HttpURLConnection.HTTP_MULT_CHOICE: case HttpURLConnection.HTTP_MOVED_PERM: case HttpURLConnection.HTTP_MOVED_TEMP: case HttpURLConnection.HTTP_SEE_OTHER: case 307: // No constant for 307 Temporary Redirect ? String location = connection.getHeaderField("Location"); if (location != null) { try { return new URI(location); } catch (URISyntaxException e) { // nevermind } } } return uri; } finally { connection.disconnect(); } }
From source file:Main.java
public static InputStream downloadFromURL(String urlString) { InputStream retval = null;//ww w.ja va 2 s .c o m try { URL url = new URL(urlString); HttpURLConnection con = (HttpURLConnection) url.openConnection(); //URLConnection ucon = url.openConnection(); con.setRequestMethod("GET"); con.setDoInput(true); retval = con.getInputStream(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return retval; }
From source file:com.javielinux.utils.Translate.java
/** * Forms an HTTP request and parses the response for a translation. * * @param text The String to translate./*w w w. ja v a 2s.c om*/ * @param from The language code to translate from. * @param to The language code to translate to. * @return The translated String. * @throws Exception */ private static String retrieveTranslation(String text, String from, String to) throws Exception { try { StringBuilder url = new StringBuilder(); url.append(URL_STRING).append(from).append("%7C").append(to); url.append(TEXT_VAR).append(URLEncoder.encode(text, ENCODING)); Log.d(Utils.TAG, "Conectadndo a " + url.toString()); HttpURLConnection uc = (HttpURLConnection) new URL(url.toString()).openConnection(); uc.setDoInput(true); uc.setDoOutput(true); try { InputStream is = uc.getInputStream(); String result = toString(is); JSONObject json = new JSONObject(result); return ((JSONObject) json.get("responseData")).getString("translatedText"); } finally { // http://java.sun.com/j2se/1.5.0/docs/guide/net/http-keepalive.html uc.getInputStream().close(); if (uc.getErrorStream() != null) uc.getErrorStream().close(); } } catch (Exception ex) { throw ex; } }
From source file:Main.java
public static String getJsonContent(String url_path) { try {//from w w w.j ava 2 s . c o m URL url = new URL(url_path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(3000); connection.setRequestMethod("GET"); connection.setDoInput(true); int code = connection.getResponseCode(); if (code == 200) { return changeInputStream(connection.getInputStream()); } } catch (Exception e) { } return ""; }
From source file:com.mopaas_mobile.http.BaseHttpRequester.java
public static String doGET(String urlstr, List<BasicNameValuePair> params) throws IOException { String result = null;/*ww w.j av a 2s . co m*/ String content = ""; for (int i = 0; i < params.size(); i++) { content = content + "&" + URLEncoder.encode(((NameValuePair) params.get(i)).getName(), "UTF-8") + "=" + URLEncoder.encode(((NameValuePair) params.get(i)).getValue(), "UTF-8"); } URL url = new URL(urlstr + "?" + content.substring(1)); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setRequestMethod("GET"); connection.setUseCaches(false); connection.connect(); InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuffer b = new StringBuffer(); int ch; while ((ch = br.read()) != -1) { b.append((char) ch); } result = b.toString().trim(); connection.disconnect(); return result; }
From source file:com.beust.android.translate.Translate.java
/** * Forms an HTTP request and parses the response for a translation. * * @param text The String to translate./*from w w w . j a va2s . co m*/ * @param from The language code to translate from. * @param to The language code to translate to. * @return The translated String. * @throws Exception */ private static String retrieveTranslation(String text, String from, String to) throws Exception { try { StringBuilder url = new StringBuilder(); url.append(URL_STRING).append(from).append("%7C").append(to); url.append(TEXT_VAR).append(URLEncoder.encode(text, ENCODING)); Log.d(TranslateService.TAG, "Connecting to " + url.toString()); HttpURLConnection uc = (HttpURLConnection) new URL(url.toString()).openConnection(); uc.setDoInput(true); uc.setDoOutput(true); try { Log.d(TranslateService.TAG, "getInputStream()"); InputStream is = uc.getInputStream(); String result = toString(is); JSONObject json = new JSONObject(result); return ((JSONObject) json.get("responseData")).getString("translatedText"); } finally { // http://java.sun.com/j2se/1.5.0/docs/guide/net/http-keepalive.html uc.getInputStream().close(); if (uc.getErrorStream() != null) uc.getErrorStream().close(); } } catch (Exception ex) { throw ex; } }
From source file:com.itwizard.mezzofanti.Translate.java
/** * Forms an HTTP request and parses the response for a translation. * * @param text The String to translate./*from w ww.j a va 2s .com*/ * @param from The language code to translate from. * @param to The language code to translate to. * @return The translated String. * @throws Exception */ private static String retrieveTranslation(String text, String from, String to) throws Exception { try { StringBuilder url = new StringBuilder(); url.append(URL_STRING).append(from).append("%7C").append(to); url.append(TEXT_VAR).append(URLEncoder.encode(text, ENCODING)); Log.d("TEST", "Connecting to " + url.toString()); HttpURLConnection uc = (HttpURLConnection) new URL(url.toString()).openConnection(); uc.setDoInput(true); uc.setDoOutput(true); try { Log.d("TEST", "getInputStream()"); InputStream is = uc.getInputStream(); String result = toString(is); JSONObject json = new JSONObject(result); return ((JSONObject) json.get("responseData")).getString("translatedText"); } finally { // http://java.sun.com/j2se/1.5.0/docs/guide/net/http-keepalive.html uc.getInputStream().close(); if (uc.getErrorStream() != null) uc.getErrorStream().close(); } } catch (Exception ex) { throw ex; } }
From source file:Main.java
public static void connectAndSendHttp(ByteArrayOutputStream baos) { try {/* w w w.j a v a2 s . c o m*/ URL url; url = new URL("http://10.0.2.2:8080"); String charset = "UTF-8"; HttpURLConnection conn; conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestProperty("Accept-Charset", charset); conn.setRequestProperty("ENCTYPE", "multipart/form-data"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset); OutputStream output = conn.getOutputStream(); output.write(baos.toByteArray()); output.close(); conn.getInputStream(); } catch (IOException e) { e.printStackTrace(); } }
From source file:cc.vileda.sipgatesync.api.SipgateApi.java
public static String getToken(final String username, final String password) { try {//w w w. ja v a2 s.c o m final HttpURLConnection urlConnection = getConnection("/authorization/token"); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); urlConnection.setRequestMethod("POST"); JSONObject request = new JSONObject(); request.put("username", username); request.put("password", password); OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream()); Log.d("SipgateApi", request.getString("username")); wr.write(request.toString()); wr.flush(); 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(); Log.d("SipgateApi", "" + sb.toString()); final JSONObject response = new JSONObject(sb.toString()); return response.getString("token"); } else { System.out.println(urlConnection.getResponseMessage()); } } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:com.mboarder.util.Translate.java
/** * Forms an HTTP request and parses the response for a translation. * * @param text The String to translate./*ww w . java2s . com*/ * @param from The language code to translate from. * @param to The language code to translate to. * @return The translated String. * @throws Exception */ private static String retrieveTranslation(String text, String from, String to) throws Exception { try { StringBuilder url = new StringBuilder(); url.append(URL_STRING).append(from).append("%7C").append(to); url.append(TEXT_VAR).append(URLEncoder.encode(text, ENCODING)); Log.d(TAG, "Connecting to " + url.toString()); HttpURLConnection uc = (HttpURLConnection) new URL(url.toString()).openConnection(); uc.setDoInput(true); uc.setDoOutput(true); try { Log.d(TAG, "getInputStream()"); InputStream is = uc.getInputStream(); String result = toString(is); JSONObject json = new JSONObject(result); return ((JSONObject) json.get("responseData")).getString("translatedText"); } finally { // http://java.sun.com/j2se/1.5.0/docs/guide/net/http-keepalive.html uc.getInputStream().close(); if (uc.getErrorStream() != null) uc.getErrorStream().close(); } } catch (Exception ex) { throw ex; } }