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.aiven.seafox.controller.http.volley.toolbox.HurlStack.java

@SuppressWarnings("deprecation")
/* package */ static void setConnectionParametersForRequest(HttpURLConnection connection, Request<?> request)
        throws IOException, AuthFailureError {
    switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST:
        // This is the deprecated way that needs to be handled for backwards compatibility.
        // If the request's post body is null, then the assumption is that the request is
        // GET.  Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            // Prepare output. There is no need to set Content-Length explicitly,
            // since this is handled by HttpURLConnection using the size of the prepared
            // output stream.
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.write(postBody);/*  w  w  w.ja va 2 s.c o m*/
            out.close();
        }
        break;
    case Method.GET:
        // Not necessary to set the request method because connection defaults to GET but
        // being explicit here.
        connection.setRequestMethod("GET");
        break;
    case Request.Method.DELETE:
        connection.setRequestMethod("DELETE");
        break;
    case Request.Method.POST:
        connection.setRequestMethod("POST");
        addBodyIfExists(connection, request);
        break;
    case Method.PUT:
        connection.setRequestMethod("PUT");
        addBodyIfExists(connection, request);
        break;
    case Method.HEAD:
        connection.setRequestMethod("HEAD");
        break;
    case Method.OPTIONS:
        connection.setRequestMethod("OPTIONS");
        break;
    case Method.TRACE:
        connection.setRequestMethod("TRACE");
        break;
    case Method.PATCH:
        connection.setRequestMethod("PATCH");
        addBodyIfExists(connection, request);
        break;
    default:
        throw new IllegalStateException("Unknown method type.");
    }
}

From source file:com.spotworld.spotapp.widget.utils.volley.toolbox.HurlStack.java

@SuppressWarnings("deprecation")
/* package */static void setConnectionParametersForRequest(HttpURLConnection connection, Request<?> request)
        throws IOException, AuthFailureError {
    switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST:
        // This is the deprecated way that needs to be handled for backwards compatibility.
        // If the request's post body is null, then the assumption is that the request is
        // GET. Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            // Prepare output. There is no need to set Content-Length explicitly,
            // since this is handled by HttpURLConnection using the size of the prepared
            // output stream.
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.write(postBody);//from   ww  w.  jav  a  2 s  .c  om
            out.close();
        }
        break;
    case Method.GET:
        // Not necessary to set the request method because connection defaults to GET but
        // being explicit here.
        connection.setRequestMethod("GET");
        break;
    case Method.DELETE:
        connection.setRequestMethod("DELETE");
        break;
    case Method.POST:
        connection.setRequestMethod("POST");
        addBodyIfExists(connection, request);
        break;
    case Method.PUT:
        connection.setRequestMethod("PUT");
        addBodyIfExists(connection, request);
        break;
    case Method.HEAD:
        connection.setRequestMethod("HEAD");
        break;
    case Method.OPTIONS:
        connection.setRequestMethod("OPTIONS");
        break;
    case Method.TRACE:
        connection.setRequestMethod("TRACE");
        break;
    case Method.PATCH:
        connection.setRequestMethod("PATCH");
        addBodyIfExists(connection, request);
        break;
    default:
        throw new IllegalStateException("Unknown method type.");
    }
}

From source file:com.android.fancyblurdemo.volley.toolbox.HurlStack.java

@SuppressWarnings("deprecation")
/* package */ static void setConnectionParametersForRequest(HttpURLConnection connection, Request<?> request)
        throws IOException, AuthFailureError {
    switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST:
        // This is the deprecated way that needs to be handled for backwards compatibility.
        // If the request's post body is null, then the assumption is that the request is
        // GET.  Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            // Prepare output. There is no need to set Content-Length explicitly,
            // since this is handled by HttpURLConnection using the size of the prepared
            // output stream.
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.write(postBody);//  w  ww .  jav a2  s.  c  o m
            out.close();
        }
        break;
    case Method.GET:
        // Not necessary to set the request method because connection defaults to GET but
        // being explicit here.
        connection.setRequestMethod("GET");
        break;
    case Method.DELETE:
        connection.setRequestMethod("DELETE");
        break;
    case Method.POST:
        connection.setRequestMethod("POST");
        addBodyIfExists(connection, request);
        break;
    case Method.PUT:
        connection.setRequestMethod("PUT");
        addBodyIfExists(connection, request);
        break;
    case Method.HEAD:
        connection.setRequestMethod("HEAD");
        break;
    case Method.OPTIONS:
        connection.setRequestMethod("OPTIONS");
        break;
    case Method.TRACE:
        connection.setRequestMethod("TRACE");
        break;
    case Method.PATCH:
        addBodyIfExists(connection, request);
        connection.setRequestMethod("PATCH");
        break;
    default:
        throw new IllegalStateException("Unknown method type.");
    }
}

From source file:com.iframe.source.publics.http.volley.toolbox.HurlStack.java

@SuppressWarnings("deprecation")
/* package */static void setConnectionParametersForRequest(HttpURLConnection connection, Request<?> request)
        throws IOException, AuthFailureError {
    switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST:
        // This is the deprecated way that needs to be handled for backwards compatibility.
        // If the request's post body is null, then the assumption is that the request is
        // GET.  Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            // Prepare output. There is no need to set Content-Length explicitly,
            // since this is handled by HttpURLConnection using the size of the prepared
            // output stream.
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.write(postBody);//  w  w w  .  ja va2s  .  c  o  m
            out.close();
        }
        break;
    case Method.GET:
        // Not necessary to set the request method because connection defaults to GET but
        // being explicit here.
        connection.setRequestMethod("GET");
        break;
    case Method.DELETE:
        connection.setRequestMethod("DELETE");
        break;
    case Method.POST:
        connection.setRequestMethod("POST");
        addBodyIfExists(connection, request);
        break;
    case Method.PUT:
        connection.setRequestMethod("PUT");
        addBodyIfExists(connection, request);
        break;
    case Method.HEAD:
        connection.setRequestMethod("HEAD");
        break;
    case Method.OPTIONS:
        connection.setRequestMethod("OPTIONS");
        break;
    case Method.TRACE:
        connection.setRequestMethod("TRACE");
        break;
    case Method.PATCH:
        addBodyIfExists(connection, request);
        connection.setRequestMethod("PATCH");
        break;
    default:
        throw new IllegalStateException("Unknown method type.");
    }
}

From source file:com.chen.cy.talkimage.network.xvolley.XHurlStack.java

@SuppressWarnings("deprecation")
/* package */ static void setConnectionParametersForRequest(HttpURLConnection connection, Request<?> request)
        throws IOException, AuthFailureError {
    switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST:
        // This is the deprecated way that needs to be handled for backwards compatibility.
        // If the request's post body is null, then the assumption is that the request is
        // GET.  Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            // Prepare output. There is no need to set Content-Length explicitly,
            // since this is handled by HttpURLConnection using the size of the prepared
            // output stream.
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.write(postBody);/* ww  w .  j a va2  s .  c  o  m*/
            out.close();
        }
        break;
    case Method.GET:
        // Not necessary to set the request method because connection defaults to GET but
        // being explicit here.
        connection.setRequestMethod("GET");
        break;
    case Method.DELETE:
        connection.setRequestMethod("DELETE");
        break;
    case Method.POST:
        connection.setRequestMethod("POST");
        addBodyIfExists(connection, request);
        break;
    case Method.PUT:
        connection.setRequestMethod("PUT");
        addBodyIfExists(connection, request);
        break;
    //            case Method.HEAD:
    //                connection.setRequestMethod("HEAD");
    //                break;
    //            case Method.OPTIONS:
    //                connection.setRequestMethod("OPTIONS");
    //                break;
    //            case Method.TRACE:
    //                connection.setRequestMethod("TRACE");
    //                break;
    //            case Method.PATCH:
    //                addBodyIfExists(connection, request);
    //                connection.setRequestMethod("PATCH");
    //                break;
    default:
        throw new IllegalStateException("Unknown method type.");
    }
}

From source file:com.adaptris.core.http.client.net.MetadataAuthorizationHeader.java

@Override
public void configureConnection(HttpURLConnection conn) {
    if (!StringUtils.isBlank(headerValue)) {
        conn.addRequestProperty(HttpConstants.AUTHORIZATION, headerValue);
    }/*from  ww w.j  ava2  s. co  m*/
}

From source file:com.netflix.curator.ensemble.exhibitor.DefaultExhibitorRestClient.java

@Override
public String getRaw(String hostname, int port, String uriPath, String mimeType) throws Exception {
    URI uri = new URI(useSsl ? "https" : "http", null, hostname, port, uriPath, null, null);
    HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
    connection.addRequestProperty("Accept", mimeType);
    StringBuilder str = new StringBuilder();
    InputStream in = new BufferedInputStream(connection.getInputStream());
    try {// w w w  .j av a2  s  . c om
        for (;;) {
            int b = in.read();
            if (b < 0) {
                break;
            }
            str.append((char) (b & 0xff));
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
    return str.toString();
}

From source file:org.wisdom.openid.connect.request.UserInfoRequest.java

public UserInfoResponse execute() throws IOException {

    HttpURLConnection connection = (HttpURLConnection) userInfoEndpoint.openConnection();
    connection.addRequestProperty("Authorization", format("Bearer %s", accessToken));

    connection.setUseCaches(false);/*from   ww w.j  ava  2s  . c o m*/
    connection.setDoInput(true);
    connection.setDoOutput(true);

    //Get Response
    JsonNode node = new ObjectMapper().readTree(connection.getInputStream());
    return new UserInfoResponse(node.get("sub").asText(), node);

}

From source file:com.smilehacker.exvolley.toolbox.HurlStack.java

@SuppressWarnings("deprecation")
/* package */ static void setConnectionParametersForRequest(HttpURLConnection connection, Request<?> request)
        throws IOException, AuthFailureError {
    switch (request.getMethod()) {
    case Request.Method.DEPRECATED_GET_OR_POST:
        // This is the deprecated way that needs to be handled for backwards compatibility.
        // If the request's post body is null, then the assumption is that the request is
        // GET.  Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            // Prepare output. There is no need to set Content-Length explicitly,
            // since this is handled by HttpURLConnection using the size of the prepared
            // output stream.
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.write(postBody);/*from  w ww  .  ja v a  2s . c o m*/
            out.close();
        }
        break;
    case Request.Method.GET:
        // Not necessary to set the request method because connection defaults to GET but
        // being explicit here.
        connection.setRequestMethod("GET");
        break;
    case Request.Method.DELETE:
        connection.setRequestMethod("DELETE");
        break;
    case Request.Method.POST:
        connection.setRequestMethod("POST");
        addBodyIfExists(connection, request);
        break;
    case Request.Method.PUT:
        connection.setRequestMethod("PUT");
        addBodyIfExists(connection, request);
        break;
    case Request.Method.HEAD:
        connection.setRequestMethod("HEAD");
        break;
    case Request.Method.OPTIONS:
        connection.setRequestMethod("OPTIONS");
        break;
    case Request.Method.TRACE:
        connection.setRequestMethod("TRACE");
        break;
    case Request.Method.PATCH:
        addBodyIfExists(connection, request);
        connection.setRequestMethod("PATCH");
        break;
    default:
        throw new IllegalStateException("Unknown method type.");
    }
}

From source file:tectonicus.world.World.java

public static ArrayList<Player> loadPlayers(File worldDir, PlayerSkinCache playerSkinCache) {
    File playersDir = Minecraft.findPlayersDir(worldDir);

    System.out.println("Loading players from " + playersDir.getAbsolutePath());

    ArrayList<Player> players = new ArrayList<Player>();
    File[] playerFiles = playersDir.listFiles();
    if (playerFiles != null) {
        for (File playerFile : playerFiles) {
            if (playerFile.getName().endsWith(".dat")) {
                try {
                    Player player = new Player(playerFile);

                    CacheEntry ce = playerSkinCache.getCacheEntry(player.getUUID());
                    if (ce != null) {
                        final long age = System.currentTimeMillis() - ce.fetchedTime;
                        if (age < 1000 * 60 * 60 * 60) // one hour in ms
                        {//from  w  ww.j a v  a2 s .c  om
                            player.setName(ce.playerName);
                            player.setSkinURL(ce.skinURL);
                        }
                    } else {
                        if (player.getUUID().equals(player.getName())) {
                            player.setSkinURL("http://www.minecraft.net/skin/" + player.getName() + ".png");
                        } else {
                            String urlString = "https://sessionserver.mojang.com/session/minecraft/profile/"
                                    + player.getUUID();
                            URL url = new URL(urlString);
                            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                            connection.setRequestMethod("GET");
                            connection.addRequestProperty("Content-Type", "application/json");
                            connection.setReadTimeout(15 * 1000);
                            connection.connect();

                            BufferedReader reader = new BufferedReader(
                                    new InputStreamReader(connection.getInputStream()));
                            StringBuilder builder = new StringBuilder();

                            String line = null;
                            while ((line = reader.readLine()) != null) {
                                builder.append(line + "\n");
                            }
                            reader.close();
                            JSONObject obj = new JSONObject(builder.toString());
                            player.setName(obj.getString("name"));
                            JSONObject textures = obj.getJSONArray("properties").getJSONObject(0);
                            byte[] decoded = DatatypeConverter
                                    .parseBase64Binary(textures.get("value").toString());
                            obj = new JSONObject(new String(decoded, "UTF-8"));
                            boolean hasSkin = obj.getJSONObject("textures").has("SKIN");
                            String textureUrl = null;
                            if (hasSkin == true)
                                textureUrl = obj.getJSONObject("textures").getJSONObject("SKIN")
                                        .getString("url");
                            player.setSkinURL(textureUrl);
                        }
                    }

                    players.add(player);
                    System.out.println("Loaded " + player.getName());
                } catch (Exception e) {
                    System.err.println("Couldn't load player info from " + playerFile.getName());
                    System.err.println(
                            "You are only allowed to contact the Mojang session server once per minute per player.  Wait for a minute and try again.");
                    //e.printStackTrace();
                }
            }
        }
    }

    System.out.println("\tloaded " + players.size() + " players");

    return players;
}