List of usage examples for java.net URLConnection setAllowUserInteraction
public void setAllowUserInteraction(boolean allowuserinteraction)
From source file:Main.java
public static void main(String args[]) throws Exception { URL u = new URL("http://www.java2s.com"); URLConnection uc = u.openConnection(); uc.setAllowUserInteraction(true); System.out.println(uc.getAllowUserInteraction()); }
From source file:MainClass.java
public static void main(String[] args) { try {//from w w w.ja va2 s. com MyHttpHandler handler = new MyHttpHandler(); URLConnection uc = handler.openConnection(new URL("http://www.ora.com")); if (!uc.getAllowUserInteraction()) { uc.setAllowUserInteraction(true); } } catch (MalformedURLException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); } }
From source file:MainClass.java
public static void main(String args[]) throws Exception { String query = "name=yourname&email=youremail@yourserver.com"; URLConnection uc = new URL("http:// your form ").openConnection(); uc.setDoOutput(true);//from w w w. ja va 2 s . c o m uc.setDoInput(true); uc.setAllowUserInteraction(false); DataOutputStream dos = new DataOutputStream(uc.getOutputStream()); // The POST line, the Accept line, and // the content-type headers are sent by the URLConnection. // We just need to send the data dos.writeBytes(query); dos.close(); // Read the response DataInputStream dis = new DataInputStream(uc.getInputStream()); String nextline; while ((nextline = dis.readLine()) != null) { System.out.println(nextline); } dis.close(); }
From source file:com.maverick.ssl.SSLSocket.java
public static void main(String[] args) { try {/* www . jav a 2 s . c om*/ HttpsURLStreamHandlerFactory.addHTTPSSupport(); URL url = new URL("https://localhost"); //$NON-NLS-1$ URLConnection con = url.openConnection(); con.setDoInput(true); con.setDoOutput(false); con.setAllowUserInteraction(false); con.setRequestProperty("Accept", //$NON-NLS-1$ "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*"); //$NON-NLS-1$ con.setRequestProperty("Accept-Encoding", "gzip, deflate"); //$NON-NLS-1$ //$NON-NLS-2$ con.setRequestProperty("Accept-Language", "en-gb"); //$NON-NLS-1$ //$NON-NLS-2$ con.setRequestProperty("Connection", "Keep-Alive"); //$NON-NLS-1$ //$NON-NLS-2$ con.setRequestProperty("User-Agent", //$NON-NLS-1$ "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)"); //$NON-NLS-1$ con.connect(); InputStream in = con.getInputStream(); int read; while ((read = in.read()) > -1) { System.out.write(read); } } catch (SSLIOException ex) { ex.getRealException().printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:com.krawler.esp.utils.HttpPost.java
public static String GetResponse(String postdata) { try {//from w w w. j ava 2 s . com String s = URLEncoder.encode(postdata, "UTF-8"); // URL u = new URL("http://google.com"); URL u = new URL("http://localhost:7070/service/soap/"); URLConnection uc = u.openConnection(); uc.setDoOutput(true); uc.setDoInput(true); uc.setAllowUserInteraction(false); DataOutputStream dstream = new DataOutputStream(uc.getOutputStream()); // The POST line dstream.writeBytes(s); dstream.close(); // Read Response InputStream in = uc.getInputStream(); int x; while ((x = in.read()) != -1) { System.out.write(x); } in.close(); BufferedReader r = new BufferedReader(new InputStreamReader(in)); StringBuffer buf = new StringBuffer(); String line; while ((line = r.readLine()) != null) { buf.append(line); } return buf.toString(); } catch (Exception e) { // throw e; return e.toString(); } }
From source file:xtrememp.update.SoftwareUpdate.java
public static Version getLastVersion(URL url) throws Exception { Version result = null;//from ww w. j av a 2 s . co m InputStream urlStream = null; try { URLConnection urlConnection = url.openConnection(); urlConnection.setAllowUserInteraction(false); urlConnection.setConnectTimeout(30000); urlConnection.setDoInput(true); urlConnection.setDoOutput(false); urlConnection.setReadTimeout(10000); urlConnection.setUseCaches(true); urlStream = urlConnection.getInputStream(); Properties properties = new Properties(); properties.load(urlStream); result = new Version(); result.setMajorNumber(Integer.parseInt(properties.getProperty("xtrememp.lastVersion.majorNumber"))); result.setMinorNumber(Integer.parseInt(properties.getProperty("xtrememp.lastVersion.minorNumber"))); result.setMicroNumber(Integer.parseInt(properties.getProperty("xtrememp.lastVersion.microNumber"))); result.setVersionType( Version.VersionType.valueOf(properties.getProperty("xtrememp.lastVersion.versionType"))); result.setReleaseDate(properties.getProperty("xtrememp.lastVersion.releaseDate")); result.setDownloadURL(properties.getProperty("xtrememp.lastVersion.dounloadURL")); } finally { IOUtils.closeQuietly(urlStream); } return result; }
From source file:ninja.standalone.NinjaJettyTest.java
static public String get(String url) throws Exception { URL u = new URL(url); URLConnection conn = u.openConnection(); conn.setAllowUserInteraction(false); conn.setConnectTimeout(3000);//from w ww .j ava2 s .c om conn.setReadTimeout(3000); try (InputStream is = conn.getInputStream()) { return IOUtils.toString(conn.getInputStream()); } }
From source file:com.comcast.cdn.traffic_control.traffic_monitor.util.Fetcher.java
public static String fetchContent(final URLConnection conn, final int timeout) throws IOException { conn.setAllowUserInteraction(true); if (timeout != 0) { conn.setConnectTimeout(timeout); conn.setReadTimeout(timeout);/*from w w w. j a va2 s.c o m*/ } conn.connect(); return IOUtils.toString(new InputStreamReader(conn.getInputStream(), UTF8_STR)); }
From source file:com.adrguides.utils.HTTPUtils.java
public static InputStream openAddress(Context context, URL url) throws IOException { if ("file".equals(url.getProtocol()) && url.getPath().startsWith("/android_asset/")) { return context.getAssets().open(url.getPath().substring(15)); // "/android_asset/".length() == 15 } else {/*from w w w.j av a 2 s . co m*/ URLConnection urlconn = url.openConnection(); urlconn.setReadTimeout(10000 /* milliseconds */); urlconn.setConnectTimeout(15000 /* milliseconds */); urlconn.setAllowUserInteraction(false); urlconn.setDoInput(true); urlconn.setDoOutput(false); if (urlconn instanceof HttpURLConnection) { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); int responsecode = connection.getResponseCode(); if (responsecode != HttpURLConnection.HTTP_OK) { throw new IOException("Http response code returned:" + responsecode); } } return urlconn.getInputStream(); } }
From source file:com.roadwarrior.vtiger.client.NetworkUtilities.java
/** * Connects to the server, authenticates the provided username and * password.//from w w w . ja v a2 s. c o m * * @param username The user's username * @param password The user's password * @return String The authentication token returned by the server (or null) */ public static String authenticate(String username, String accessKey, String base_url) { String token = null; String hash = null; authenticate_log_text = "authenticate()\n"; AUTH_URI = base_url + "/webservice.php"; authenticate_log_text = authenticate_log_text + "url: " + AUTH_URI + "?operation=getchallenge&username=" + username + "\n"; Log.d(TAG, "AUTH_URI : "); Log.d(TAG, AUTH_URI + "?operation=getchallenge&username=" + username); // =========== get challenge token ============================== ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(); try { URL url; // HTTP GET REQUEST url = new URL(AUTH_URI + "?operation=getchallenge&username=" + username); HttpURLConnection con; con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("Content-length", "0"); con.setUseCaches(false); // for some site that redirects based on user agent con.setInstanceFollowRedirects(true); con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.15) Gecko/2009101600 Firefox/3.0.15"); con.setAllowUserInteraction(false); int timeout = 20000; con.setConnectTimeout(timeout); con.setReadTimeout(timeout); con.connect(); int status = con.getResponseCode(); authenticate_log_text = authenticate_log_text + "Request status = " + status + "\n"; switch (status) { case 200: case 201: case 302: BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); Log.d(TAG, "message body"); Log.d(TAG, sb.toString()); authenticate_log_text = authenticate_log_text + "body : " + sb.toString(); if (status == 302) { authenticate_log_text = sb.toString(); return null; } JSONObject result = new JSONObject(sb.toString()); Log.d(TAG, result.getString("result")); JSONObject data = new JSONObject(result.getString("result")); token = data.getString("token"); break; case 401: Log.e(TAG, "Server auth error: ");// + readResponse(con.getErrorStream())); authenticate_log_text = authenticate_log_text + "Server auth error"; return null; default: Log.e(TAG, "connection status code " + status);// + readResponse(con.getErrorStream())); authenticate_log_text = authenticate_log_text + "connection status code :" + status; return null; } } catch (ClientProtocolException e) { Log.i(TAG, "getchallenge:http protocol error"); Log.e(TAG, e.getMessage()); authenticate_log_text = authenticate_log_text + "ClientProtocolException :" + e.getMessage() + "\n"; return null; } catch (IOException e) { Log.e(TAG, "getchallenge: IO Exception"); Log.e(TAG, e.getMessage()); Log.e(TAG, AUTH_URI + "?operation=getchallenge&username=" + username); authenticate_log_text = authenticate_log_text + "getchallenge: IO Exception : " + e.getMessage() + "\n"; return null; } catch (JSONException e) { Log.i(TAG, "json exception"); authenticate_log_text = authenticate_log_text + "JSon exception\n"; // TODO Auto-generated catch block e.printStackTrace(); return null; } // ================= login ================== try { MessageDigest m = MessageDigest.getInstance("MD5"); m.update(token.getBytes()); m.update(accessKey.getBytes()); hash = new BigInteger(1, m.digest()).toString(16); Log.i(TAG, "hash"); Log.i(TAG, hash); } catch (NoSuchAlgorithmException e) { authenticate_log_text = authenticate_log_text + "MD5 => no such algorithm\n"; e.printStackTrace(); } try { String charset; charset = "utf-8"; String query = String.format("operation=login&username=%s&accessKey=%s", URLEncoder.encode(username, charset), URLEncoder.encode(hash, charset)); authenticate_log_text = authenticate_log_text + "login()\n"; URLConnection connection = new URL(AUTH_URI).openConnection(); connection.setDoOutput(true); // Triggers POST. int timeout = 20000; connection.setConnectTimeout(timeout); connection.setReadTimeout(timeout); connection.setAllowUserInteraction(false); connection.setRequestProperty("Accept-Charset", charset); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset); connection.setUseCaches(false); OutputStream output = connection.getOutputStream(); try { output.write(query.getBytes(charset)); } finally { try { output.close(); } catch (IOException logOrIgnore) { } } Log.d(TAG, "Query written"); BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); Log.d(TAG, "message post body"); Log.d(TAG, sb.toString()); authenticate_log_text = authenticate_log_text + "post message body :" + sb.toString() + "\n"; JSONObject result = new JSONObject(sb.toString()); String success = result.getString("success"); Log.i(TAG, success); if (success == "true") { Log.i(TAG, result.getString("result")); Log.i(TAG, "sucesssfully logged in is"); JSONObject data = new JSONObject(result.getString("result")); sessionName = data.getString("sessionName"); Log.i(TAG, sessionName); authenticate_log_text = authenticate_log_text + "successfully logged in\n"; return token; } else { // success is false, retrieve error JSONObject data = new JSONObject(result.getString("error")); authenticate_log_text = "can not login :\n" + data.toString(); return null; } //token = data.getString("token"); //Log.i(TAG,token); } catch (ClientProtocolException e) { Log.d(TAG, "login: http protocol error"); Log.d(TAG, e.getMessage()); authenticate_log_text = authenticate_log_text + "HTTP Protocol error \n"; authenticate_log_text = authenticate_log_text + e.getMessage() + "\n"; } catch (IOException e) { Log.d(TAG, "login: IO Exception"); Log.d(TAG, e.getMessage()); authenticate_log_text = authenticate_log_text + "login: IO Exception \n"; authenticate_log_text = authenticate_log_text + e.getMessage() + "\n"; } catch (JSONException e) { Log.d(TAG, "JSON exception"); // TODO Auto-generated catch block authenticate_log_text = authenticate_log_text + "JSON exception "; authenticate_log_text = authenticate_log_text + e.getMessage(); e.printStackTrace(); } return null; // ======================================================================== }