Example usage for javax.net.ssl HttpsURLConnection setRequestProperty

List of usage examples for javax.net.ssl HttpsURLConnection setRequestProperty

Introduction

In this page you can find the example usage for javax.net.ssl HttpsURLConnection setRequestProperty.

Prototype

public void setRequestProperty(String key, String value) 

Source Link

Document

Sets the general request property.

Usage

From source file:com.camel.trainreserve.JDKHttpsClient.java

private static HttpsURLConnection getConnection(URL url, String method, String ctype) throws IOException {
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    conn.setRequestMethod(method);//from  w w  w  .ja va2s . c  om
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setRequestProperty("Accept", "text/xml,text/javascript,text/html");
    conn.setRequestProperty("User-Agent", "stargate");
    conn.setRequestProperty("Content-Type", ctype);
    return conn;
}

From source file:org.eclipse.agail.recommenderserver.Recommenders.java

private static ListOfWFs updateFlowsNodes(ListOfWFs wflist) {

    for (int i = 0; i < wflist.getWfList().size(); i++) {

        // UPDATE LINK
        wflist.getWfList().get(i).setHref("https://flows.nodered.org" + wflist.getWfList().get(i).getHref());

        char[] out = new char[12000];
        URL url;/*  w  ww . j av  a2s  .  c o  m*/
        try {

            url = new URL(wflist.getWfList().get(i).getHref().toString());
            HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
            con.setRequestProperty("User-Agent",
                    "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
            con.connect();
            InputStream input = con.getInputStream();
            byte[] bytes = IOUtils.toByteArray(input);

            String str = new String(bytes);
            String substr = str;
            // ADD DESCRIPTION
            // flow-description"> or "flow-title">
            int start = str.lastIndexOf("flow-description\">");
            if (start != -1) {
                substr = str.substring(start);
                start = substr.indexOf(">");
                start += 1;
                substr = substr.substring(start);
            }

            else {
                start = str.lastIndexOf("flow-title\">");
                start += 12;
                substr = str.substring(start);
                start = substr.indexOf("<p>");
                start += 3;
                substr = substr.substring(start);
            }

            int end = substr.indexOf("</p>");
            String desc = substr.substring(0, end);
            wflist.getWfList().get(i).setDescription(desc);

            // ADD JS CODE
            if (wflist.getWfList().get(i).getType().equals("flow")) {

                start = str.indexOf("javascript\">");
                start += 12;
                substr = str.substring(start);
                end = substr.indexOf("</pre>");

                StringEscapeUtils util = new StringEscapeUtils();

                String code = substr.substring(0, end);
                code = util.unescapeHtml4(code);
                //               code = code.replace("&quot;", "\"");
                //               code = code.replace("&lt;", "<");
                //               code = code.replace("&gt;", ">");
                //               code = code.replace("&#x3D;", "=");
                //               code = code.replace("&#39;", "'");
                //               code = code.replace("&#x2F;", "/");
                wflist.getWfList().get(i).setJavascriptCode(code);
            }

            // ADD INSTALL COMMAND
            if (wflist.getWfList().get(i).getType().equals("node")) {
                start = str.indexOf("<code>npm install ");
                start += 6;
                substr = str.substring(start);
                end = substr.indexOf("</code>");

                String command = substr.substring(0, end);
                wflist.getWfList().get(i).setInstallCommand(command);
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    return wflist;
}

From source file:ke.co.tawi.babblesms.server.servlet.accountmngmt.Login.java

public static boolean validateCaptcha(String gRecaptchaResponse) throws IOException {
    if (gRecaptchaResponse == null || "".equals(gRecaptchaResponse)) {
        return false;
    }//from  www . j  av  a  2  s  . c  o  m

    try {
        URL obj = new URL(url);
        HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

        // add reuqest header
        con.setRequestMethod("POST");
        con.setRequestProperty("User-Agent", USER_AGENT);
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

        String postParams = "secret=" + secret + "&response=" + gRecaptchaResponse;

        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(postParams);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Post parameters : " + postParams);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        // print result
        System.out.println(response.toString());

        //parse JSON response and return 'success' value
        JsonReader jsonReader = Json.createReader(new StringReader(response.toString()));
        JsonObject jsonObject = jsonReader.readObject();
        jsonReader.close();

        return jsonObject.getBoolean("success");
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:org.jembi.rhea.rapidsms.GenerateORU_R01Alert.java

private static void addHTTPBasicAuthProperty(HttpsURLConnection conn) {
    String userpass = username + ":" + password;
    @SuppressWarnings("restriction")
    String basicAuth = "Basic " + new String(DatatypeConverter.printBase64Binary(userpass.getBytes()));
    conn.setRequestProperty("Authorization", basicAuth);
}

From source file:com.coinprism.model.APIClient.java

private static String executeHttpGet(String url) throws IOException, APIException {
    URL target = new URL(url);
    HttpsURLConnection connection = (HttpsURLConnection) target.openConnection();

    connection.setRequestMethod("GET");
    connection.setRequestProperty("User-Agent", userAgent);

    return getHttpResponse(connection);
}

From source file:org.wso2.carbon.identity.application.authentication.endpoint.util.AuthContextAPIClient.java

/**
 * Send mutual ssl https post request and return data
 *
 * @param backendURL   URL of the service
 * @return Received data/*from   w  ww .j  av  a2s .  co  m*/
 * @throws IOException
 */
public static String getContextProperties(String backendURL) {
    InputStream inputStream = null;
    BufferedReader reader = null;
    String response = null;
    URL url = null;

    try {
        url = new URL(backendURL);
        HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
        httpsURLConnection.setSSLSocketFactory(MutualSSLManager.getSslSocketFactory());
        httpsURLConnection.setDoOutput(true);
        httpsURLConnection.setDoInput(true);
        httpsURLConnection.setRequestMethod(HTTP_METHOD_GET);

        httpsURLConnection.setRequestProperty(MutualSSLManager.getUsernameHeaderName(),
                MutualSSLManager.getCarbonLogin());

        inputStream = httpsURLConnection.getInputStream();
        reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
        StringBuilder builder = new StringBuilder();
        String line;

        while (StringUtils.isNotEmpty(line = reader.readLine())) {
            builder.append(line);
        }
        response = builder.toString();
    } catch (IOException e) {
        log.error("Sending " + HTTP_METHOD_GET + " request to URL : " + url + "failed.", e);
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }

        } catch (IOException e) {
            log.error("Closing stream for " + url + " failed", e);
        }
    }
    return response;
}

From source file:com.ibm.mobilefirst.mobileedge.utils.RequestUtils.java

public static void executeTrainRequest(String name, String uuid, List<AccelerometerData> accelerometerData,
        List<GyroscopeData> gyroscopeData, RequestResult requestResult) throws IOException {

    URL url = new URL("https://medge.mybluemix.net/alg/train");

    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    conn.setReadTimeout(5000);/*w  ww .j  a v  a 2s .co  m*/
    conn.setConnectTimeout(10000);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/json");

    conn.setDoInput(true);
    conn.setDoOutput(true);

    JSONObject jsonToSend = createTrainBodyJSON(name, uuid, accelerometerData, gyroscopeData);

    OutputStream outputStream = conn.getOutputStream();
    DataOutputStream wr = new DataOutputStream(outputStream);
    wr.writeBytes(jsonToSend.toString());
    wr.flush();
    wr.close();

    outputStream.close();

    String response = "";

    int responseCode = conn.getResponseCode();

    //Log.e("BBB2","" + responseCode);

    if (responseCode == HttpsURLConnection.HTTP_OK) {
        String line;
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        while ((line = br.readLine()) != null) {
            response += line;
        }
    } else {
        response = "{}";
    }

    handleResponse(response, requestResult);
}

From source file:com.coinprism.model.APIClient.java

private static String executeHttpPost(String url, String body) throws IOException, APIException {
    URL target = new URL(url);
    HttpsURLConnection connection = (HttpsURLConnection) target.openConnection();

    connection.setRequestMethod("POST");
    connection.setRequestProperty("User-Agent", userAgent);
    connection.setRequestProperty("Content-Type", "application/json");

    OutputStream output = null;// w  w w .j ava  2 s  .co  m
    try {
        output = connection.getOutputStream();
        output.write(body.getBytes("UTF-8"));
    } finally {
        if (output != null)
            output.close();
    }

    return getHttpResponse(connection);
}

From source file:org.openmrs.module.rheapocadapter.handler.ConnectionHandler.java

private static void addHTTPBasicAuthProperty(HttpsURLConnection conn) {
    String userpass = username + ":" + password;
    String basicAuth = "Basic " + new String(DatatypeConverter.printBase64Binary(userpass.getBytes()));
    conn.setRequestProperty("Authorization", basicAuth);
}

From source file:dictinsight.utils.io.HttpUtils.java

/**
 * https??post/*ww  w  . ja  v a 2s .  c o m*/
 * @param url
 * @param param
 * @return post?
 */
public static String httpsPostData(String url, String param) {
    class DefaultTrustManager implements X509TrustManager {
        @Override
        public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    }

    BufferedOutputStream brOutStream = null;
    BufferedReader reader = null;

    try {
        SSLContext context = SSLContext.getInstance("SSL");
        context.init(null, new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
        HttpsURLConnection connection = (HttpsURLConnection) (new URL(url)).openConnection();
        connection.setSSLSocketFactory(context.getSocketFactory());
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Proxy-Connection", "Keep-Alive");
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setConnectTimeout(1000 * 15);

        brOutStream = new BufferedOutputStream(connection.getOutputStream());
        brOutStream.write(param.getBytes());
        brOutStream.flush();

        reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String responseContent = "";
        String line = reader.readLine();
        while (line != null) {
            responseContent += line;
            line = reader.readLine();
        }

        return responseContent;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (brOutStream != null)
                brOutStream.close();
            if (reader != null)
                reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}