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 int sendMessage(String auth_token, String registrationId, String message) throws IOException {

    StringBuilder postDataBuilder = new StringBuilder();
    postDataBuilder.append(PARAM_REGISTRATION_ID).append("=").append(registrationId);
    postDataBuilder.append("&").append(PARAM_COLLAPSE_KEY).append("=").append("0");
    postDataBuilder.append("&").append("data.payload").append("=")
            .append(URLEncoder.encode("Lars war hier", UTF8));

    byte[] postData = postDataBuilder.toString().getBytes(UTF8);

    // Hit the dm URL.

    URL url = new URL("https://android.clients.google.com/c2dm/send");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);
    conn.setUseCaches(false);//from w  ww .  j  av a2s. c om
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
    conn.setRequestProperty("Content-Length", Integer.toString(postData.length));
    conn.setRequestProperty("Authorization", "GoogleLogin auth=" + auth_token);

    OutputStream out = conn.getOutputStream();
    out.write(postData);
    out.close();

    int responseCode = conn.getResponseCode();
    return responseCode;
}

From source file:Main.java

private static HttpURLConnection getConnection(URL url) throws IOException {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setDoInput(true);/*from   www.j  a v a2s  .  com*/
    conn.setDoOutput(true);
    conn.setRequestProperty("Accept", "text/xml");
    return conn;
}

From source file:Main.java

public static String executePost(String url, String parameters) throws IOException {

    URL request = new URL(url);
    HttpURLConnection connection = (HttpURLConnection) request.openConnection();
    connection.setDoOutput(true);
    connection.setDoInput(true);/*www. j  a v a2  s.  c o m*/
    connection.setInstanceFollowRedirects(false);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("User-Agent", "StripeConnectAndroid");
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.setRequestProperty("charset", "utf-8");
    connection.setRequestProperty("Content-Length", "" + Integer.toString(parameters.getBytes().length));
    connection.setUseCaches(false);

    DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
    wr.writeBytes(parameters);
    wr.flush();
    wr.close();

    String response = streamToString(connection.getInputStream());
    connection.disconnect();
    return response;

}

From source file:Yak_Hax.Yak_Hax_Mimerme.PostRequest.java

public static String PostBodyRequest(String URL, String JSONRaw, String UserAgent) throws IOException {
    String type = "application/json";
    URL u = new URL(URL);
    HttpURLConnection conn = (HttpURLConnection) u.openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", type);
    conn.setRequestProperty("User-Agent", UserAgent);
    OutputStream os = conn.getOutputStream();
    os.write(JSONRaw.getBytes());/*  ww  w .ja  va  2 s. c om*/
    os.flush();
    os.close();

    String response = null;
    DataInputStream input = new DataInputStream(conn.getInputStream());
    while (null != ((response = input.readLine()))) {
        input.close();
        return response;
    }
    return null;
}

From source file:Main.java

private static HttpURLConnection _openPostConnection(String purl) throws IOException {
    URL url = new URL(purl);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoInput(true);//from   w  w w . ja va  2  s  .c o m
    connection.setUseCaches(false);
    connection.setDoOutput(true);
    connection.setRequestMethod("GET");
    connection.setRequestProperty("User-Agent", "Android Client Agent");

    return connection;
}

From source file:Main.java

public static void setUpConnection(HttpURLConnection connection, String method) {
    try {/*w ww .  jav  a  2  s .  c  o  m*/
        connection.setRequestMethod(method);
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");
    } catch (ProtocolException e) {
        e.printStackTrace();
    }

}

From source file:com.monibox.selTestNG.utils.GetNodeIP.java

private static HttpURLConnection createHttpCon(final String url, final String method) throws IOException {
    final HttpURLConnection httpCon;
    httpCon = (HttpURLConnection) new URL(url).openConnection();
    httpCon.setDoOutput(true);
    httpCon.setRequestMethod(method);//w w w.  j a va 2  s .c  o m
    httpCon.setRequestProperty("Content-type", "application/json");
    httpCon.setRequestProperty("accept", "application/json");

    return httpCon;
}

From source file:dk.nsi.minlog.test.utils.TestHelper.java

public static String sendRequest(String url, String action, String docXml, boolean failOnError)
        throws IOException, ServiceException {
    URL u = new URL(url);
    HttpURLConnection uc = (HttpURLConnection) u.openConnection();
    uc.setDoOutput(true);
    uc.setDoInput(true);/*from   w w  w  .  j  a v a  2 s .c  om*/
    uc.setRequestMethod("POST");
    uc.setRequestProperty("SOAPAction", "\"" + action + "\"");
    uc.setRequestProperty("Content-Type", "text/xml; charset=utf-8;");
    OutputStream os = uc.getOutputStream();

    IOUtils.write(docXml, os, "UTF-8");
    os.flush();
    os.close();

    InputStream is;
    if (uc.getResponseCode() != 200) {
        is = uc.getErrorStream();
    } else {
        is = uc.getInputStream();
    }
    String res = IOUtils.toString(is);

    is.close();
    if (uc.getResponseCode() != 200 && (uc.getResponseCode() != 500 || failOnError)) {
        throw new ServiceException(res);
    }
    uc.disconnect();

    return res;
}

From source file:Main.java

public static void post(String actionUrl, String file) {
    try {/*from w w  w.j av  a 2 s. c o m*/
        URL url = new URL(actionUrl);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setUseCaches(false);
        con.setRequestMethod("POST");
        con.setRequestProperty("Charset", "UTF-8");
        con.setRequestProperty("Content-Type", "multipart/form-data;boundary=*****");
        DataOutputStream ds = new DataOutputStream(con.getOutputStream());
        FileInputStream fStream = new FileInputStream(file);
        int bufferSize = 1024; // 1MB
        byte[] buffer = new byte[bufferSize];
        int bufferLength = 0;
        int length;
        while ((length = fStream.read(buffer)) != -1) {
            bufferLength = bufferLength + 1;
            ds.write(buffer, 0, length);
        }
        fStream.close();
        ds.flush();
        InputStream is = con.getInputStream();
        int ch;
        StringBuilder b = new StringBuilder();
        while ((ch = is.read()) != -1) {
            b.append((char) ch);
        }
        new String(b.toString().getBytes("ISO-8859-1"), "utf-8");
        ds.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void connectAndSendHttp(ByteArrayOutputStream baos) {
    try {//w  w w  .j a v  a2 s .  co m

        URL url;
        url = new URL("http://10.0.2.2:8080");

        String charset = "UTF-8";

        HttpURLConnection conn;

        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestProperty("Accept-Charset", charset);
        conn.setRequestProperty("ENCTYPE", "multipart/form-data");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
        OutputStream output = conn.getOutputStream();
        output.write(baos.toByteArray());
        output.close();
        conn.getInputStream();

    } catch (IOException e) {
        e.printStackTrace();
    }
}