Example usage for java.net HttpURLConnection addRequestProperty

List of usage examples for java.net HttpURLConnection addRequestProperty

Introduction

In this page you can find the example usage for java.net HttpURLConnection addRequestProperty.

Prototype

public void addRequestProperty(String key, String value) 

Source Link

Document

Adds a general request property specified by a key-value pair.

Usage

From source file:com.eTilbudsavis.etasdk.network.impl.HttpURLNetwork.java

private static void addBodyIfExists(HttpURLConnection connection, Request<?> request) throws IOException {
    byte[] body = request.getBody();
    if (body != null) {
        connection.setDoOutput(true);/*  w  ww  .  j  av  a 2  s . c om*/
        connection.addRequestProperty(HeaderUtils.CONTENT_TYPE, request.getBodyContentType());
        DataOutputStream out = new DataOutputStream(connection.getOutputStream());
        out.write(body);
        out.close();
    }
}

From source file:de.jetwick.util.Translate.java

public static String download(String urlAsString) {
    try {//from   w w w . ja  v  a  2s  .co m
        URL url = new URL(urlAsString);
        //using proxy may increase latency
        HttpURLConnection hConn = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
        hConn.setRequestProperty("User-Agent",
                "Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20100101 Firefox/7.0.1");
        hConn.addRequestProperty("Referer", "http://jetsli.de/crawler");

        hConn.setConnectTimeout(2000);
        hConn.setReadTimeout(2000);
        InputStream is = hConn.getInputStream();
        if ("gzip".equals(hConn.getContentEncoding()))
            is = new GZIPInputStream(is);

        return getInputStream(is);
    } catch (Exception ex) {
        return "";
    }
}

From source file:org.immopoly.android.helper.WebHelper.java

public static JSONObject getHttpsData(URL url, boolean signed, Context context) throws ImmopolyException {
    JSONObject obj = null;//from  w  w  w  .j  a  v  a 2 s. com
    if (Settings.isOnline(context)) {
        HttpURLConnection request;
        try {
            request = (HttpURLConnection) url.openConnection();
            request.addRequestProperty("User-Agent",
                    "immopoly android client " + ImmopolyActivity.getStaticVersionInfo());

            if (signed)
                OAuthData.getInstance(context).consumer.sign(request);

            request.setConnectTimeout(SOCKET_TIMEOUT);
            request.connect();

            InputStream in = new BufferedInputStream(request.getInputStream());
            String s = readInputStream(in);
            JSONTokener tokener = new JSONTokener(s);
            return new JSONObject(tokener);
        } catch (JSONException e) {
            throw new ImmopolyException("Kommunikationsproblem (beim lesen der Antwort)", e);
        } catch (MalformedURLException e) {
            throw new ImmopolyException("Kommunikationsproblem (fehlerhafte URL)", e);
        } catch (OAuthMessageSignerException e) {
            throw new ImmopolyException("Kommunikationsproblem (Signierung)", e);
        } catch (OAuthExpectationFailedException e) {
            throw new ImmopolyException("Kommunikationsproblem (Sicherherit)", e);
        } catch (OAuthCommunicationException e) {
            throw new ImmopolyException("Kommunikationsproblem (Sicherherit)", e);
        } catch (IOException e) {
            throw new ImmopolyException("Kommunikationsproblem", e);
        }
    } else
        throw new ImmopolyException("Kommunikationsproblem (Offline)");
}

From source file:io.helixservice.feature.configuration.cloudconfig.CloudConfigDecrypt.java

/**
 * Decrypt an encrypted string//w  w w  .  ja v  a2 s.c om
 * <p>
 * This method blocks on a HTTP request.
 *
 * @param name property or filename for reference/logging
 * @param encryptedValue Encrypted string
 * @param cloudConfigUri URI of the Cloud Config server
 * @param httpBasicHeader HTTP Basic header containing username and password for Cloud Config server
 * @return
 */
public static String decrypt(String name, String encryptedValue, String cloudConfigUri,
        String httpBasicHeader) {
    String result = encryptedValue;

    // Remove prefix if needed
    if (encryptedValue.startsWith(CIPHER_PREFIX)) {
        encryptedValue = encryptedValue.substring(CIPHER_PREFIX.length());
    }

    String decryptUrl = cloudConfigUri + "/decrypt";

    try {
        HttpURLConnection connection = (HttpURLConnection) new URL(decryptUrl).openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);

        connection.setRequestMethod("POST");
        connection.addRequestProperty(AUTHORIZATION_HEADER, httpBasicHeader);
        connection.setRequestProperty("Content-Type", "text/plain");
        connection.setRequestProperty("Content-Length", Integer.toString(encryptedValue.getBytes().length));
        connection.setRequestProperty("Accept", "*/*");

        // Write body
        OutputStream outputStream = connection.getOutputStream();
        outputStream.write(encryptedValue.getBytes());
        outputStream.close();

        if (connection.getResponseCode() == 200) {
            InputStream inputStream = connection.getInputStream();
            result = IOUtils.toString(inputStream);
            inputStream.close();
        } else {
            LOG.error("Unable to Decrypt name=" + name + " due to httpStatusCode="
                    + connection.getResponseCode() + " for decryptUrl=" + decryptUrl);
        }
    } catch (IOException e) {
        LOG.error("Unable to connect to Cloud Config server at decryptUrl=" + decryptUrl, e);
    }

    return result;
}

From source file:org.immopoly.android.helper.WebHelper.java

public static JSONObject getHttpData(URL url, boolean signed, Context context) throws JSONException {
    JSONObject obj = null;//from   w  w  w. ja va  2  s .  c  o  m
    if (Settings.isOnline(context)) {
        HttpURLConnection request;
        try {

            request = (HttpURLConnection) url.openConnection();

            request.addRequestProperty("User-Agent",
                    "immopoly android client " + ImmopolyActivity.getStaticVersionInfo());
            request.addRequestProperty("Accept-Encoding", "gzip");
            if (signed)
                OAuthData.getInstance(context).consumer.sign(request);
            request.setConnectTimeout(SOCKET_TIMEOUT);

            request.connect();
            String encoding = request.getContentEncoding();

            InputStream in;
            if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
                in = new GZIPInputStream(request.getInputStream());
            } else {
                in = new BufferedInputStream(request.getInputStream());
            }
            String s = readInputStream(in);
            JSONTokener tokener = new JSONTokener(s);
            obj = new JSONObject(tokener);

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthMessageSignerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return obj;
}

From source file:com.shopgun.android.sdk.network.impl.HttpURLNetwork.java

private static void addBodyIfExists(HttpURLConnection connection, Request<?> request) throws IOException {
    byte[] body = request.getBody();
    if (body != null) {
        connection.setDoOutput(true);//from w w  w  .j a v  a2  s  .  co  m
        connection.addRequestProperty("Content-Type", request.getBodyContentType());
        DataOutputStream out = new DataOutputStream(connection.getOutputStream());
        out.write(body);
        out.close();
    }
}

From source file:br.eb.ime.pfc.domain.GeoServerCommunication.java

private static void redirectStream(String urlName, HttpServletRequest request, HttpServletResponse response) {
    URL url = null;/* w  w  w. ja v a  2  s  . co  m*/
    try {
        url = new URL(urlName);
    } catch (MalformedURLException e) {
        //Internal error, the user will receive no data.
        sendError(HTTP_STATUS.BAD_REQUEST, response);
        return;
    }
    HttpURLConnection conn = null;
    try {
        conn = (HttpURLConnection) url.openConnection();
        conn.addRequestProperty("Authorization", "Basic " + BASE64_AUTHORIZATION);
        //conn.setRequestMethod("GET");
        //conn.setDoOutput(true);
        conn.connect();
    } catch (IOException e) {
        sendError(HTTP_STATUS.INTERNAL_ERROR, response);
        return;
    }

    try (InputStream is = conn.getInputStream(); OutputStream os = response.getOutputStream()) {
        response.setContentType(conn.getContentType());
        IOUtils.copy(is, os);
    } catch (IOException e) {
        request.getServletContext().log("IO");
        sendError(HTTP_STATUS.INTERNAL_ERROR, response);
        return;
    } finally { //Close connection to save resources
        conn.disconnect();
    }
}

From source file:com.scut.easyfe.network.kjFrame.http.HttpConnectStack.java

/**
 * body/*from  ww w .ja  va 2  s  . c  o  m*/
 */
private static void addBodyIfExists(HttpURLConnection connection, Request<?> request) throws IOException {
    byte[] body = request.getBody();
    if (body != null) {
        connection.setDoOutput(true);
        connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getBodyContentType());
        DataOutputStream out = new DataOutputStream(connection.getOutputStream());
        out.write(body);
        out.close();
    }
}

From source file:com.android.volley.stack.HurlStack.java

private static void addBodyIfExists(HttpURLConnection connection, Request<?> request)
        throws IOException, AuthFailureError {
    byte[] body = request.getBody();
    if (body != null) {
        connection.setDoOutput(true);/*from  w  w  w.  j a  v a2  s .  c  o m*/
        connection.addRequestProperty(HTTP.CONTENT_TYPE, request.getBodyContentType());
        DataOutputStream out = new DataOutputStream(connection.getOutputStream());
        out.write(body);
        out.close();
    }
}

From source file:org.jvnet.hudson.update_center.ConfluencePluginList.java

private static HttpURLConnection connect(String url, String sessionId) throws IOException {
    HttpURLConnection huc = (HttpURLConnection) new URL(url).openConnection();
    huc.setInstanceFollowRedirects(false);
    huc.setDoOutput(false);//from  w  w w. j  av  a  2  s  .c o  m
    if (sessionId != null)
        huc.addRequestProperty("Cookie", sessionId);
    InputStream i = huc.getInputStream();
    while (i.read() >= 0)
        ; // Drain stream
    return huc;
}