List of usage examples for java.net HttpURLConnection setAllowUserInteraction
public void setAllowUserInteraction(boolean allowuserinteraction)
From source file:org.overlord.rtgov.tests.platforms.jbossas.calltrace.JBossASCallTraceServiceTest.java
public static String getCallTrace(String id) throws Exception { URL getUrl = new URL("http://localhost:8080/overlord-rtgov/call/trace/instance?value=" + id); HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection(); connection.setRequestMethod("GET"); connection.setDoOutput(true);// ww w .j a va 2 s . c o m connection.setDoInput(true); connection.setUseCaches(false); connection.setAllowUserInteraction(false); connection.setRequestProperty("Content-Type", "application/json"); initAuth(connection); java.io.InputStream is = connection.getInputStream(); byte[] b = new byte[is.available()]; is.read(b); String result = new String(b); is.close(); System.out.println("JSON RESULT=" + result); return (result); }
From source file:biz.mosil.webtools.MosilWeb.java
/** * ? InputStream/*www . ja va 2s . c om*/ * */ public static final InputStream getInputStream(final String _url) { InputStream result = null; try { URL url = new URL(_url); URLConnection conn = url.openConnection(); if (!(conn instanceof HttpURLConnection)) { throw new IOException("This URL Can't Connect!"); } HttpURLConnection httpConn = (HttpURLConnection) conn; //???? httpConn.setAllowUserInteraction(false); //?? httpConn.setInstanceFollowRedirects(true); httpConn.setRequestMethod("GET"); httpConn.connect(); final int response = httpConn.getResponseCode(); if (response == HttpURLConnection.HTTP_OK) { result = httpConn.getInputStream(); } } catch (MalformedURLException _ex) { Log.e("getInputStream", "Malformed URL Exception: " + _ex.toString()); } catch (IOException _ex) { Log.e("getInputStream", "IO Exception: " + _ex.toString()); } return result; }
From source file:org.overlord.rtgov.tests.platforms.jbossas.situationmgr.JBossASSituationManagerServiceTest.java
public static void ignore(IgnoreSubject ignore) throws Exception { URL getUrl = new URL("http://localhost:8080/overlord-rtgov/situation/manager/ignore"); HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true);//from ww w . jav a 2s . co m connection.setDoInput(true); connection.setUseCaches(false); connection.setAllowUserInteraction(false); connection.setRequestProperty("Content-Type", "application/json"); initAuth(connection); java.io.OutputStream os = connection.getOutputStream(); MAPPER.writeValue(os, ignore); os.flush(); os.close(); java.io.InputStream is = connection.getInputStream(); try { byte[] b = new byte[is.available()]; is.read(b); System.out.println("Ignore response=" + new String(b)); } catch (Exception e) { System.err.println("Exception when reading response: " + e); } is.close(); }
From source file:org.overlord.rtgov.tests.platforms.jbossas.situationmgr.JBossASSituationManagerServiceTest.java
public static void observe(String subject) throws Exception { URL getUrl = new URL("http://localhost:8080/overlord-rtgov/situation/manager/observe"); HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true);//from ww w. ja v a 2 s. com connection.setDoInput(true); connection.setUseCaches(false); connection.setAllowUserInteraction(false); connection.setRequestProperty("Content-Type", "application/json"); initAuth(connection); java.io.OutputStream os = connection.getOutputStream(); os.write(subject.getBytes()); os.flush(); os.close(); java.io.InputStream is = connection.getInputStream(); try { byte[] b = new byte[is.available()]; is.read(b); System.out.println("Observe response=" + new String(b)); } catch (Exception e) { System.err.println("Exception when reading response: " + e); } is.close(); }
From source file:org.overlord.rtgov.tests.platforms.jbossas.situationmgr.JBossASSituationManagerServiceTest.java
public static java.util.List<?> performACMQuery(QuerySpec qs) throws Exception { URL getUrl = new URL("http://localhost:8080/overlord-rtgov/acm/query"); HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true);//from w ww . ja v a2 s. c o m connection.setDoInput(true); connection.setUseCaches(false); connection.setAllowUserInteraction(false); connection.setRequestProperty("Content-Type", "application/json"); initAuth(connection); java.io.OutputStream os = connection.getOutputStream(); MAPPER.writeValue(os, qs); os.flush(); os.close(); java.io.InputStream is = connection.getInputStream(); java.util.List<?> result = null; try { result = MAPPER.readValue(is, java.util.List.class); } catch (Exception e) { System.err.println("Exception when reading ACMQuery '" + qs + "': " + e); result = new java.util.ArrayList<Object>(); } is.close(); return (result); }
From source file:com.evilisn.DAO.CertMapper.java
public static byte[] httpGetBin(URI uri, boolean bActiveCheckUnknownHost) throws Exception { InputStream is = null;/*from www . j a v a 2 s . co m*/ InputStream is_temp = null; try { if (uri == null) return null; URL url = uri.toURL(); if (bActiveCheckUnknownHost) { url.getProtocol(); String host = url.getHost(); int port = url.getPort(); if (port == -1) port = url.getDefaultPort(); InetSocketAddress isa = new InetSocketAddress(host, port); if (isa.isUnresolved()) { //fix JNLP popup error issue throw new UnknownHostException("Host Unknown:" + isa.toString()); } } HttpURLConnection uc = (HttpURLConnection) url.openConnection(); uc.setDoInput(true); uc.setAllowUserInteraction(false); uc.setInstanceFollowRedirects(true); setTimeout(uc); String contentEncoding = uc.getContentEncoding(); int len = uc.getContentLength(); // is = uc.getInputStream(); if (contentEncoding != null && contentEncoding.toLowerCase().indexOf("gzip") != -1) { is_temp = uc.getInputStream(); is = new GZIPInputStream(is_temp); } else if (contentEncoding != null && contentEncoding.toLowerCase().indexOf("deflate") != -1) { is_temp = uc.getInputStream(); is = new InflaterInputStream(is_temp); } else { is = uc.getInputStream(); } if (len != -1) { int ch = 0, i = 0; byte[] res = new byte[len]; while ((ch = is.read()) != -1) { res[i++] = (byte) (ch & 0xff); } return res; } else { ArrayList<byte[]> buffer = new ArrayList<byte[]>(); int buf_len = 1024; byte[] res = new byte[buf_len]; int ch = 0, i = 0; while ((ch = is.read()) != -1) { res[i++] = (byte) (ch & 0xff); if (i == buf_len) { //rotate buffer.add(res); i = 0; res = new byte[buf_len]; } } int total_len = buffer.size() * buf_len + i; byte[] buf = new byte[total_len]; for (int j = 0; j < buffer.size(); j++) { System.arraycopy(buffer.get(j), 0, buf, j * buf_len, buf_len); } if (i > 0) { System.arraycopy(res, 0, buf, buffer.size() * buf_len, i); } return buf; } } catch (Exception e) { e.printStackTrace(); return null; } finally { closeInputStream(is_temp); closeInputStream(is); } }
From source file:org.overlord.rtgov.tests.platforms.jbossas.activityserver.JBossASActivityServerServiceTest.java
public static java.util.List<ActivityType> getActivityEvents(long from, long to) throws Exception { java.net.CookieManager cm = new java.net.CookieManager(); java.net.CookieHandler.setDefault(cm); URL eventsUrl = new URL("http://localhost:8080/overlord-rtgov/activity/events?from=" + from + "&to=" + to); HttpURLConnection connection = (HttpURLConnection) eventsUrl.openConnection(); connection.setRequestMethod("GET"); connection.setDoOutput(true);/*from w ww. j a va 2s . c o m*/ connection.setDoInput(true); connection.setUseCaches(false); connection.setAllowUserInteraction(false); connection.setRequestProperty("Content-Type", "application/json"); initAuth(connection); java.io.InputStream is = connection.getInputStream(); java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(); while (is.available() > 0) { byte[] b = new byte[is.available()]; is.read(b); baos.write(b); } is.close(); baos.close(); byte[] b = baos.toByteArray(); System.out.println("THE JSON=" + new String(b)); return (ActivityUtil.deserializeActivityTypeList(b)); }
From source file:org.bibsonomy.util.WebUtils.java
/** * Returns the cookies returned by the server on accessing the URL. * The format of the returned cookies is as * //www . j ava 2 s. c o m * * @param url * @return The cookies as string, build by {@link #buildCookieString(List)}. * @throws IOException */ public static String getCookies(final URL url) throws IOException { final HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); urlConn.setAllowUserInteraction(false); urlConn.setDoInput(true); urlConn.setDoOutput(false); urlConn.setUseCaches(false); urlConn.setRequestProperty(USER_AGENT_HEADER_NAME, USER_AGENT_PROPERTY_VALUE); urlConn.connect(); final List<String> cookies = urlConn.getHeaderFields().get("Set-Cookie"); urlConn.disconnect(); return buildCookieString(cookies); }
From source file:org.bibsonomy.util.WebUtils.java
/** * Do a POST request to the given URL with the given content. Assume the charset of the result to be charset. * /* w w w. j ava 2 s .c om*/ * @param url * @param postContent * @param charset - the assumed charset of the result. If <code>null</code>, the charset from the response header is used. * @param cookie - the Cookie to be attached to the request. If <code>null</code>, the Cookie header is not set. * @return The content of the result page. * * @throws IOException * * @Deprecated */ public static String getPostContentAsString(final URL url, final String postContent, final String charset, final String cookie) throws IOException { final HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); urlConn.setAllowUserInteraction(false); urlConn.setDoInput(true); urlConn.setDoOutput(true); urlConn.setUseCaches(false); urlConn.setRequestMethod("POST"); urlConn.setRequestProperty(CONTENT_TYPE_HEADER_NAME, "application/x-www-form-urlencoded"); /* * set user agent (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html) since some * pages require it to download content. */ urlConn.setRequestProperty(USER_AGENT_HEADER_NAME, USER_AGENT_PROPERTY_VALUE); if (cookie != null) { urlConn.setRequestProperty(COOKIE_HEADER_NAME, cookie); } writeStringToStream(postContent, urlConn.getOutputStream()); // connect urlConn.connect(); /* * extract character encoding from header */ final String activeCharset; if (charset == null) { activeCharset = getCharset(urlConn); } else { activeCharset = charset; } /* * FIXME: check content type header to ensure that we only read textual * content (and not a PDF, radio stream or DVD image ...) */ // write into string writer final StringBuilder out = inputStreamToStringBuilder(urlConn.getInputStream(), activeCharset); // disconnect urlConn.disconnect(); return out.toString(); }
From source file:org.bibsonomy.util.WebUtils.java
/** * Reads from a URL and writes the content into a string. * /* w ww . ja v a 2 s . c o m*/ * @param inputURL the URL of the content. * @param cookie a cookie which should be included in the header of the request send to the server * @return String which holds the page content. * @throws IOException * * @Deprecated */ public static String getContentAsString(final URL inputURL, final String cookie) throws IOException { try { final HttpURLConnection urlConn = (HttpURLConnection) inputURL.openConnection(); urlConn.setAllowUserInteraction(false); urlConn.setDoInput(true); urlConn.setDoOutput(false); urlConn.setUseCaches(false); /* * set user agent (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html) since some * pages require it to download content. */ urlConn.setRequestProperty(USER_AGENT_HEADER_NAME, USER_AGENT_PROPERTY_VALUE); if (cookie != null) { urlConn.setRequestProperty(COOKIE_HEADER_NAME, cookie); } urlConn.connect(); /* * extract character encoding from header */ final String charSet = getCharset(urlConn); /* * FIXME: check content type header to ensure that we only read textual * content (and not a PDF, radio stream or DVD image ...) */ /* * write content into string buffer */ final StringBuilder out = inputStreamToStringBuilder(urlConn.getInputStream(), charSet); urlConn.disconnect(); return out.toString(); } catch (final ConnectException cex) { log.debug("Could not get content for URL " + inputURL.toString() + " : " + cex.getMessage()); throw new IOException(cex); } catch (final IOException ioe) { log.debug("Could not get content for URL " + inputURL.toString() + " : " + ioe.getMessage()); throw ioe; } }