Example usage for java.net HttpURLConnection setDoOutput

List of usage examples for java.net HttpURLConnection setDoOutput

Introduction

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

Prototype

public void setDoOutput(boolean dooutput) 

Source Link

Document

Sets the value of the doOutput field for this URLConnection to the specified value.

Usage

From source file:Main.java

public static String deleteUrl(String url, Bundle params) throws MalformedURLException, IOException {
    System.setProperty("http.keepAlive", "false");
    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    conn.setRequestProperty("User-Agent", System.getProperties().getProperty("http.agent") + " agent");
    conn.setRequestMethod("DELETE");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setDoOutput(false);
    conn.setDoInput(true);//from w w w  .  ja va2s .  c o  m
    //conn.setRequestProperty("Connection", "Keep-Alive");
    conn.connect();

    String response = "";
    try {
        response = read(conn.getInputStream());
    } catch (FileNotFoundException e) {
        // Error Stream contains JSON that we can parse to a FB error
        response = read(conn.getErrorStream());
    }
    return response;
}

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);
        connection.addRequestProperty(HTTP.CONTENT_TYPE, request.getBodyContentType());
        DataOutputStream out = new DataOutputStream(connection.getOutputStream());
        out.write(body);/* www  .  jav a2  s  .co  m*/
        out.close();
    }
}

From source file:me.prokopyl.commandtools.migration.UUIDFetcher.java

private static HttpURLConnection createConnection() throws IOException {
    URL url = new URL(PROFILE_URL);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setUseCaches(false);//  w w w  . j a  va  2 s .  c  o m
    connection.setDoInput(true);
    connection.setDoOutput(true);
    return connection;
}

From source file:de.mas.telegramircbot.utils.images.ImgurUploader.java

public static String uploadImageAndGetLink(String clientID, byte[] image) throws IOException {
    URL url;// ww w .j a v  a  2s  .c  o  m
    url = new URL(Settings.IMGUR_API_URL);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    String dataImage = Base64.getEncoder().encodeToString(image);
    String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(dataImage, "UTF-8");

    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Authorization", "Client-ID " + clientID);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    conn.connect();
    StringBuilder stb = new StringBuilder();
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();

    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        stb.append(line).append("\n");
    }
    wr.close();
    rd.close();

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(ImgurResponse.class, new ImgurResponseDeserializer());
    Gson gson = gsonBuilder.create();

    // The JSON data
    try {
        ImgurResponse response = gson.fromJson(stb.toString(), ImgurResponse.class);
        return response.getLink();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return stb.toString();
}

From source file:me.sonarbeserk.lockup.utils.UUIDFetcher.java

private static HttpURLConnection createConnection(int page) throws Exception {
    URL url = new URL(PROFILE_URL + page);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setUseCaches(false);//ww w .  j a  v  a2s  . c  om
    connection.setDoInput(true);
    connection.setDoOutput(true);
    return connection;
}

From source file:com.baasbox.service.push.providers.GCMServer.java

public static void validateApiKey(String apikey)
        throws MalformedURLException, IOException, PushInvalidApiKeyException {
    Message message = new Message.Builder().addData("message", "validateAPIKEY").build();
    Sender sender = new Sender(apikey);

    List<String> deviceid = new ArrayList<String>();
    deviceid.add("ABC");

    Map<Object, Object> jsonRequest = new HashMap<Object, Object>();
    jsonRequest.put(JSON_REGISTRATION_IDS, deviceid);
    Map<String, String> payload = message.getData();
    if (!payload.isEmpty()) {
        jsonRequest.put(JSON_PAYLOAD, payload);
    }/* www  . ja va 2s. com*/
    String requestBody = JSONValue.toJSONString(jsonRequest);

    String url = com.google.android.gcm.server.Constants.GCM_SEND_ENDPOINT;
    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();

    byte[] bytes = requestBody.getBytes();

    conn.setDoOutput(true);
    conn.setUseCaches(false);
    conn.setFixedLengthStreamingMode(bytes.length);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/json");
    conn.setRequestProperty("Authorization", "key=" + apikey);
    OutputStream out = conn.getOutputStream();
    out.write(bytes);
    out.close();

    int status = conn.getResponseCode();
    if (status != 200) {
        if (status == 401) {
            throw new PushInvalidApiKeyException("Wrong api key");
        }
        if (status == 503) {
            throw new UnknownHostException();
        }
    }

}

From source file:com.baasbox.android.HttpUrlConnectionClient.java

private static void addBody(HttpRequest request, HttpURLConnection connection) throws IOException {
    InputStream in = request.body;
    if (in != null) {
        connection.setDoOutput(true);
        copyStream(in, connection.getOutputStream());
    }// w  ww.  j a  v  a  2 s  .  c o m
}

From source file:com.publicuhc.pluginframework.util.UUIDFetcher.java

protected static HttpURLConnection getConnection() throws IOException {
    URL url = new URL(PROFILE_URL);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

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

    connection.setUseCaches(false);//from w  w  w .j a  v a  2  s .c om
    connection.setDoInput(true);
    connection.setDoOutput(true);

    return connection;
}

From source file:com.nxt.zyl.data.volley.toolbox.HurlStack.java

private static void addBodyIfExists(HttpURLConnection connection, final Request<?> request)
        throws IOException, AuthFailureError {
    connection.setDoOutput(true);
    connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getBodyContentType());

    if (request instanceof MultiPartRequest) {
        final UploadMultipartEntity multipartEntity = ((MultiPartRequest<?>) request).getMultipartEntity();
        // /*  w w w  . j  av  a2  s  .co m*/
        connection.setFixedLengthStreamingMode((int) multipartEntity.getContentLength());
        multipartEntity.writeTo(connection.getOutputStream());
    } else {
        byte[] body = request.getBody();
        if (body != null) {
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.write(body);
            out.close();
        }
    }
}

From source file:com.flexdesktop.connections.restfulConnection.java

public static String postRESTful(String RESTfull_URL, String data) {
    String state = "";
    try {//from  www . j  a v a  2  s  . c o  m
        URL url = new URL(RESTfull_URL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        //Send request
        postRequest(connection, data);
        //Get Response
        state = postResponse(connection);

        if (connection != null) {
            connection.disconnect();
        }
    } catch (Exception ex) {
        Logger.getLogger(com.flexdesktop.connections.restfulConnection.class.getName()).log(Level.SEVERE, null,
                ex);
    }
    System.out.println(state);
    return state;
}