Example usage for java.net URLConnection setRequestProperty

List of usage examples for java.net URLConnection setRequestProperty

Introduction

In this page you can find the example usage for java.net URLConnection setRequestProperty.

Prototype

public void setRequestProperty(String key, String value) 

Source Link

Document

Sets the general request property.

Usage

From source file:eionet.cr.util.URLUtil.java

/**
 * Connect to the URL and check if it exists at the remote end. Local identifiers are removed (the part after the '#') before
 * connecting. The method returns true (i.e. URL is considered as "not existing") if the given URL is malformed, or its
 * connection throws a {@link UnknownHostException} or sends a HTTP code that is 501 or 505 or anything in the range of 400
 * to 499. The latter range, however, is ignored if the given boolean input is is true (meaning a client error is OK).
 *
 * @param urlStr the URL to check.//from   ww  w. j a  v a 2 s .  c om
 * @param clientErrorOk If true, then a response code in the range of 400 to 499 is considered OK.
 * @return As described above.
 */
public static boolean isNotExisting(String urlStr, boolean clientErrorOk) {

    int responseCode = -1;
    IOException ioe = null;
    URLConnection urlConnection = null;
    try {
        URL url = new URL(StringUtils.substringBefore(urlStr, "#"));
        urlConnection = escapeIRI(url).openConnection();
        urlConnection.setRequestProperty("Connection", "close");
        responseCode = ((HttpURLConnection) urlConnection).getResponseCode();
    } catch (IOException e) {
        ioe = e;
    } finally {
        URLUtil.disconnect(urlConnection);
    }

    return ioe instanceof MalformedURLException || ioe instanceof UnknownHostException
            || (!clientErrorOk && isClientError(responseCode))
            || responseCode == HttpURLConnection.HTTP_NOT_IMPLEMENTED
            || responseCode == HttpURLConnection.HTTP_VERSION;
}

From source file:com.minoritycode.Application.java

public static URLConnection makeConnection(String url, boolean useProxy) {

    URLConnection connection = null;
    try {/*from  ww w .j  a  v a  2s.  co  m*/
        if (useProxy) {
            connection = new URL(url).openConnection(Application.proxy);
        } else {
            connection = new URL(url).openConnection();
        }
        connection.setRequestProperty("Accept-Charset", charset);
        connection.setConnectTimeout(30000);
    } catch (IOException e) {
        e.printStackTrace();
        Application.errorBoards.put(url, e.getMessage());
        logger.logLine(e.getMessage());
        return null;
    }

    return connection;
}

From source file:sce.RESTKBJob.java

public static byte[] readURL(String url) {
    try {// w ww  .java 2  s .  com
        URL u = new URL(url);
        URLConnection conn = u.openConnection();
        // Look like faking the request coming from Web browser solve 403 error
        conn.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)");
        byte[] bytes;
        try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"))) {
            String json = in.readLine();
            bytes = json.getBytes("UTF-8");
        }
        return bytes;
    } catch (IOException e) {
        e.printStackTrace(System.out);
        return null;
    }
}

From source file:eionet.cr.util.URLUtil.java

/**
 * Connect to the URL and check if it is modified since the timestamp argument. Returns null, if it cannot be determined for
 * sure.//from w  w w  . j  a  va 2 s  . c o m
 *
 * @param urlString
 * @param timestamp
 * @return true if it is modified.
 */
public static Boolean isModifiedSince(String urlString, long timestamp) {

    if (!URLUtil.isURL(urlString)) {
        return Boolean.FALSE;
    }

    if (timestamp == 0) {
        return Boolean.TRUE;
    }

    URLConnection urlConnection = null;
    try {
        URL url = new URL(StringUtils.substringBefore(urlString, "#"));
        urlConnection = escapeIRI(url).openConnection();
        urlConnection.setRequestProperty("Connection", "close");
        urlConnection.setRequestProperty("User-Agent", userAgentHeader());
        urlConnection.setIfModifiedSince(timestamp);

        int responseCode = ((HttpURLConnection) urlConnection).getResponseCode();
        System.out.println(responseCode);
        if (responseCode == HttpURLConnection.HTTP_NOT_MODIFIED) {
            return Boolean.FALSE;
        } else if (responseCode == HttpURLConnection.HTTP_OK) {
            return Boolean.TRUE;
        } else {
            // Return null to indicate if it's unclear whether the source has
            // been modified or not.
            return null;
        }
    } catch (IOException ioe) {
        return null;
    } finally {
        URLUtil.disconnect(urlConnection);
    }
}

From source file:disko.utils.DiscoProxySettings.java

/**
 * //from  w  w w.j  a  v a 2 s .c o m
 * <p>
 * Convenience method to open a URL connection by using the
 * proxy settings. If proxyHost is null, the default is to  just
 * call <code>url.openConnection</code>.
 * </p>
 *
 * @param url
 * @return
 */
public static URLConnection newConnection(URL url) throws IOException {
    URLConnection connection;
    String externalForm = url.toExternalForm();
    url = new URL(externalForm.replace(" ", "%20"));
    if (DiscoProxySettings.proxyHost != null) {
        connection = url.openConnection(new Proxy(Proxy.Type.HTTP,
                new InetSocketAddress(DiscoProxySettings.proxyHost, DiscoProxySettings.proxyPort)));
        if (DiscoProxySettings.proxyUser != null) {
            String enc = new String(Base64.encodeBase64(
                    new String(DiscoProxySettings.proxyUser + ":" + DiscoProxySettings.proxyPassword)
                            .getBytes()));
            connection.setRequestProperty("Proxy-Authorization", "Basic " + enc);
        }
    } else
        connection = url.openConnection();
    return connection;
}

From source file:org.sakaiproject.tool.help.RestContentProvider.java

/**
 * get transformed document/*  w w w .jav a2 s.co  m*/
 * @param context
 * @return transformed document
 */
public static String getTransformedDocument(ServletContext context, HelpManager helpManager,
        Resource resource) {

    Long now = new Long((new Date()).getTime());

    if (LOG.isDebugEnabled()) {
        LOG.debug("getTransformedDocument(ServletContext " + context + ", HelpManager " + helpManager
                + "String " + resource.getDocId() + ")");
    }

    // test if resource is cached
    if (resource.getTstamp() != null) {
        if ((now.longValue() - resource.getTstamp().longValue()) < helpManager.getRestConfiguration()
                .getCacheInterval()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("retrieving document: " + resource.getDocId() + " from cache");
            }
            return resource.getSource();
        }
    }

    URL url = null;
    String transformedString = null;
    try {
        url = new URL(helpManager.getRestConfiguration().getRestUrlInDomain() + resource.getDocId() + "?domain="
                + helpManager.getRestConfiguration().getRestDomain());
        URLConnection urlConnection = url.openConnection();

        String basicAuthUserPass = helpManager.getRestConfiguration().getRestCredentials();

        String encoding = Base64.encodeBase64(basicAuthUserPass.getBytes("utf-8")).toString();
        urlConnection.setRequestProperty("Authorization", "Basic " + encoding);

        StringBuilder sBuffer = new StringBuilder();

        BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"),
                512);
        try {
            int readReturn = 0;
            char[] cbuf = new char[512];
            while ((readReturn = br.read(cbuf, 0, 512)) != -1) {
                sBuffer.append(cbuf, 0, readReturn);
            }
        } finally {
            br.close();
        }

        Document transformedDocument = getTransformedDocument(context, sBuffer);
        transformedString = serializeDocument(transformedDocument);
    } catch (MalformedURLException e) {
        LOG.error("Malformed URL in REST document: " + url.getPath());
    } catch (IOException e) {
        LOG.error("Could not open connection to REST document: " + url.getPath());
    }

    resource.setSource(transformedString);
    resource.setTstamp(now);
    helpManager.storeResource(resource);

    return transformedString;
}

From source file:net.pms.util.OpenSubtitle.java

public static String postPage(URLConnection connection, String query) throws IOException {
    connection.setDoOutput(true);//from  ww  w  . ja v a 2  s  .c  o m
    connection.setDoInput(true);
    connection.setUseCaches(false);
    connection.setDefaultUseCaches(false);
    connection.setRequestProperty("Content-Type", "text/xml");
    connection.setRequestProperty("Content-Length", "" + query.length());
    ((HttpURLConnection) connection).setRequestMethod("POST");
    //LOGGER.debug("opensub query "+query);
    // open up the output stream of the connection
    if (!StringUtils.isEmpty(query)) {
        try (DataOutputStream output = new DataOutputStream(connection.getOutputStream())) {
            output.writeBytes(query);
            output.flush();
        }
    }

    StringBuilder page;
    try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
        page = new StringBuilder();
        String str;
        while ((str = in.readLine()) != null) {
            page.append(str.trim());
            page.append("\n");
        }
    }
    //LOGGER.debug("opensubs result page "+page.toString());
    return page.toString();
}

From source file:com.moviejukebox.plugin.OpenSubtitlesPlugin.java

private static String sendRPC(String xml) throws IOException {

    StringBuilder str = new StringBuilder();
    String strona = OS_DB_SERVER;
    String logowanie = xml;/*w w w .j a  v a2 s  .  co m*/
    URL url = new URL(strona);
    URLConnection connection = url.openConnection();
    connection.setRequestProperty("Connection", "Close");

    // connection.setRequestProperty("Accept","text/html");
    connection.setRequestProperty("Content-Type", "text/xml");
    connection.setDoOutput(Boolean.TRUE);

    connection.getOutputStream().write(logowanie.getBytes("UTF-8"));

    try (Scanner in = new Scanner(connection.getInputStream())) {
        while (in.hasNextLine()) {
            str.append(in.nextLine());
        }
    }

    ((HttpURLConnection) connection).disconnect();

    return str.toString();
}

From source file:com.android.tools.idea.sdk.remote.internal.UrlOpener.java

private static Pair<InputStream, HttpResponse> openWithUrl(String url, Header[] inHeaders) throws IOException {
    URL u = new URL(url);

    URLConnection c = u.openConnection();

    c.setConnectTimeout(sConnectionTimeoutMs);
    c.setReadTimeout(sSocketTimeoutMs);//from ww w .j a  v a2  s.  c  o m

    if (inHeaders != null) {
        for (Header header : inHeaders) {
            c.setRequestProperty(header.getName(), header.getValue());
        }
    }

    // Trigger the access to the resource
    // (at which point setRequestProperty can't be used anymore.)
    int code = 200;

    if (c instanceof HttpURLConnection) {
        code = ((HttpURLConnection) c).getResponseCode();
    }

    // Get the input stream. That can fail for a file:// that doesn't exist
    // in which case we set the response code to 404.
    // Also we need a buffered input stream since the caller need to use is.reset().
    InputStream is = null;
    try {
        is = new BufferedInputStream(c.getInputStream());
    } catch (Exception ignore) {
        if (is == null && code == 200) {
            code = 404;
        }
    }

    HttpResponse outResponse = new BasicHttpResponse(new ProtocolVersion(u.getProtocol(), 1, 0), // make up the protocol version
            code, ""); //$NON-NLS-1$;

    Map<String, List<String>> outHeaderMap = c.getHeaderFields();

    for (Entry<String, List<String>> entry : outHeaderMap.entrySet()) {
        String name = entry.getKey();
        if (name != null) {
            List<String> values = entry.getValue();
            if (!values.isEmpty()) {
                outResponse.setHeader(name, values.get(0));
            }
        }
    }

    return Pair.of(is, outResponse);
}

From source file:org.motechproject.mmnaija.web.util.HTTPCommunicator.java

public static String doPost(String serviceUrl, String queryString) {
    URLConnection connection = null;
    try {/*from w  w w. ja v a 2s  .c o  m*/

        URL url = new URL(serviceUrl);
        URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                url.getQuery(), url.getRef());
        url = uri.toURL();
        // Open the connection
        connection = url.openConnection();
        connection.setDoInput(true);
        connection.setUseCaches(false); // Disable caching the document
        connection.setDoOutput(true); // Triggers POST.
        connection.setRequestProperty("Content-Type", "text/html");

        OutputStreamWriter writer = null;

        log.info("About to write");
        try {
            if (null != connection.getOutputStream()) {
                writer = new OutputStreamWriter(connection.getOutputStream());
                writer.write(queryString); // Write POST query

            } else {
                log.warn("connection Null");
            }
            // string.
        } catch (ConnectException ex) {
            log.warn("Exception : " + ex);
            // ex.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (Exception lg) {
                    log.warn("Exception lg: " + lg.toString());
                    //lg.printStackTrace();
                }
            }
        }

        InputStream in = connection.getInputStream();

        //            StringWriter writer = new StringWriter();
        IOUtils.copy(in, writer, "utf-8");
        String theString = writer.toString();
        return theString;
    } catch (Exception e) {
        //e.printStackTrace();
        log.warn("Error URL " + e.toString());
        return "";
    }
}