List of usage examples for java.net URLConnection setRequestProperty
public void setRequestProperty(String key, String value)
From source file:com.comcast.cdn.traffic_control.traffic_monitor.util.Fetcher.java
public static String fetchContent(final String link, final Map<String, String> headerMap, final int timeout) throws IOException { final URL url = new URL(link); final URLConnection conn = url.openConnection(); for (String key : headerMap.keySet()) { conn.setRequestProperty(key, headerMap.get(key)); }/* ww w. j a va2 s.co m*/ return fetchContent(conn, timeout); }
From source file:imageLines.ImageHelpers.java
/**Read a jpeg image from a url into a BufferedImage*/ public static BufferedImage readAsBufferedImage(URL imageURL) { InputStream i = null;/*w w w.j ava 2 s . c o m*/ try { URLConnection u = imageURL.openConnection(); u.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.151 Safari/535.19"); if (u instanceof HttpURLConnection) { HttpURLConnection h = (HttpURLConnection) u; if (h.getResponseCode() == 403) throw new Exception("403 forbidden"); } i = u.getInputStream(); BufferedImage bi = ImageIO.read(i); return bi; } catch (Exception e) { System.out.println(e + " for " + imageURL.toString() + "\n"); return null; } finally { try { i.close(); } catch (IOException ex) { } } }
From source file:test.ApplicationTest.java
static String call(final String request, final String contentType) { try {/*from www . j ava2s .c o m*/ final URLConnection url = new URL("http://localhost:" + TEST_SERVER_PORT + "/" + request) .openConnection(); url.setRequestProperty("Accept", contentType); return CharStreams.toString(new InputStreamReader(url.getInputStream(), Charsets.UTF_8)); } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:org.alfresco.webservice.util.ContentUtils.java
/** * Get the content as an imput stream// ww w . j a va2 s .co m * * @param content * @return */ public static InputStream getContentAsInputStream(Content content) { // Get the url and the ticket String ticket = AuthenticationUtils.getTicket(); String strUrl = content.getUrl() + "?ticket=" + ticket; try { // Create the url connection to the download servlet URL url = new URL(strUrl); URLConnection conn = url.openConnection(); // Set the cookie information conn.setRequestProperty("Cookie", "JSESSIONID=" + AuthenticationUtils.getAuthenticationDetails().getSessionId() + ";"); // Return the input stream return conn.getInputStream(); } catch (Exception exception) { throw new WebServiceException("Unable to get content as inputStream.", exception); } }
From source file:com.liferay.util.Http.java
public static String URLtoString(URL url) throws IOException { String xml = null;/*from ww w . j a v a 2 s.c o m*/ if (url != null) { URLConnection con = url.openConnection(); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); con.setRequestProperty("User-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); InputStream is = con.getInputStream(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); byte[] bytes = new byte[512]; for (int i = is.read(bytes, 0, 512); i != -1; i = is.read(bytes, 0, 512)) { buffer.write(bytes, 0, i); } xml = new String(buffer.toByteArray()); is.close(); buffer.close(); } return xml; }
From source file:com.omertron.themoviedbapi.tools.WebBrowser.java
public static URLConnection openProxiedConnection(URL url) throws MovieDbException { try {//from w w w . j a va 2 s .com if (proxyHost != null) { System.getProperties().put("proxySet", "true"); System.getProperties().put("proxyHost", proxyHost); System.getProperties().put("proxyPort", proxyPort); } URLConnection cnx = url.openConnection(); if (proxyUsername != null) { cnx.setRequestProperty("Proxy-Authorization", proxyEncodedPassword); } return cnx; } catch (IOException ex) { throw new MovieDbException(MovieDbException.MovieDbExceptionType.INVALID_URL, null, url, ex); } }
From source file:com.omertron.thetvdbapi.tools.WebBrowser.java
/** * Open a connection using proxy parameters if they exist. * * @param url//w w w. j a v a 2s . c om * @throws IOException */ public static URLConnection openProxiedConnection(URL url) throws IOException { if (proxyHost != null) { System.getProperties().put("proxySet", "true"); System.getProperties().put("proxyHost", proxyHost); System.getProperties().put("proxyPort", proxyPort); } URLConnection cnx = url.openConnection(); if (proxyUsername != null) { cnx.setRequestProperty("Proxy-Authorization", proxyEncodedPassword); } return cnx; }
From source file:de.unigoettingen.sub.commons.util.stream.StreamUtils.java
/************************************************************************************ * get MimeType as {@link String} from given URL including proxy details * /*from ww w . ja v a 2s . co m*/ * @param url the url from where to get the MimeType * @param httpproxyhost host of proxy * @param httpproxyport port of proxy * @param httpproxyusername username for proxy * @param httpproxypassword password for proxy * @return MimeType as {@link String} * @throws IOException ************************************************************************************/ public static String getMimeTypeFromUrl(URL url, String httpproxyhost, String httpproxyport, String httpproxyusername, String httpproxypassword) throws IOException { if (httpproxyhost != null) { Properties properties = System.getProperties(); properties.put("http.proxyHost", httpproxyhost); if (httpproxyport != null) { properties.put("http.proxyPort", httpproxyport); } else { properties.put("http.proxyPort", "80"); } } URLConnection con = url.openConnection(); if (httpproxyusername != null) { String login = httpproxyusername + ":" + httpproxypassword; String encodedLogin = new String(Base64.encodeBase64(login.getBytes())); con.setRequestProperty("Proxy-Authorization", "Basic " + encodedLogin); } return con.getContentType(); }
From source file:org.wso2.carbon.connector.integration.test.common.ConnectorIntegrationUtil.java
public static int sendRequestToRetriveHeaders(String addUrl, String query, String contentType) throws IOException, JSONException { String charset = "UTF-8"; URLConnection connection = new URL(addUrl).openConnection(); connection.setDoOutput(true);//w w w . j a v a2 s. c o m connection.setRequestProperty("Accept-Charset", charset); connection.setRequestProperty("Content-Type", contentType + ";charset=" + charset); OutputStream output = null; try { output = connection.getOutputStream(); output.write(query.getBytes(charset)); } finally { if (output != null) { try { output.close(); } catch (IOException logOrIgnore) { log.error("Error while closing the connection"); } } } HttpURLConnection httpConn = (HttpURLConnection) connection; int responseCode = httpConn.getResponseCode(); return responseCode; }
From source file:com.allblacks.utils.web.HttpUtil.java
private static URLConnection getConnection(URL url) throws IOException { URLConnection urlc; urlc = url.openConnection();//ww w .j a v a 2 s. c om urlc.setUseCaches(false); urlc.setRequestProperty(HttpUtil.CONTENT_TYPE, "application/x-www-form-urlencoded"); urlc.setRequestProperty(HttpUtil.USER_AGENT, "Mozilla/5.0 (X11; U; Linux x86_64; en-GB; rv:1.9.1.9) Gecko/20100414 Iceweasel/3.5.9 (like Firefox/3.5.9)"); urlc.setRequestProperty(HttpUtil.ACCEPT_ENCODING, HttpUtil.GZIP); return urlc; }