Example usage for java.io DataOutputStream writeBytes

List of usage examples for java.io DataOutputStream writeBytes

Introduction

In this page you can find the example usage for java.io DataOutputStream writeBytes.

Prototype

public final void writeBytes(String s) throws IOException 

Source Link

Document

Writes out the string to the underlying output stream as a sequence of bytes.

Usage

From source file:org.slc.sli.sif.zis.MockZis.java

private void postMessage(URL url, String xmlMessage) {
    DataOutputStream outStream = null;
    DataInputStream inStream = null;

    try {//from  w ww. j a  va  2s  .  c o m
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/xml;charset=utf-8");
        connection.setRequestProperty("Content-Length", "" + xmlMessage.length());

        connection.setDoOutput(true);
        connection.setDoInput(true);

        outStream = new DataOutputStream(connection.getOutputStream());

        // Send request
        outStream.writeBytes(xmlMessage);
        outStream.flush();

        // Get Response
        inStream = new DataInputStream(connection.getInputStream());
        StringWriter writer = new StringWriter();
        IOUtils.copy(inStream, writer, "UTF-8");
        String response = writer.toString();
        LOG.info("Response to agent POST: " + response);

    } catch (Exception e) {
        LOG.error("Error POSTing message: ", e);
    } finally {
        try {
            if (outStream != null) {
                outStream.close();
            }
            if (inStream != null) {
                inStream.close();
            }
        } catch (IOException e) {
            LOG.error("Error closing streams: ", e);
        }
    }
}

From source file:com.otisbean.keyring.Ring.java

/**
 * Export data to the specified file.//from w  w w  . j a v a  2s  . co  m
 * 
 * @param outFile Path to the output file
 * @throws IOException
 * @throws GeneralSecurityException
 */
public void save(String outFile) throws IOException, GeneralSecurityException {
    log("save(" + outFile + ")");
    if (outFile.startsWith("http")) {
        URL url = new URL(outFile);
        URLConnection urlConn = url.openConnection();
        urlConn.setDoInput(true);
        urlConn.setDoOutput(true);
        urlConn.setUseCaches(false);
        urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        DataOutputStream dos = new DataOutputStream(urlConn.getOutputStream());
        String message = "data=" + URLEncoder.encode(getExportData().toJSONString(), "UTF-8");
        dos.writeBytes(message);
        dos.flush();
        dos.close();

        // the server responds by saying 
        // "OK" or "ERROR: blah blah"

        BufferedReader br = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
        String s = br.readLine();
        if (!s.equals("OK")) {
            StringBuilder sb = new StringBuilder();
            sb.append("Failed to save to URL '");
            sb.append(url);
            sb.append("': ");
            while ((s = br.readLine()) != null) {
                sb.append(s);
            }
            throw new IOException(sb.toString());
        }
        br.close();
    } else {
        Writer writer = getWriter(outFile);
        getExportData().writeJSONString(writer);
        closeWriter(writer, outFile);
    }
}

From source file:com.constellio.app.services.appManagement.AppManagementService.java

InputStream getInputForPost(String url, String infoSent) throws IOException {
    URL obj = new URL(url);

    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("POST");
    con.setDoOutput(true);//from  ww  w .  j  a  v a  2s  .c  o  m

    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(infoSent);
    wr.flush();
    wr.close();

    return con.getInputStream();
}

From source file:com.almunt.jgcaap.systemupdater.MainActivity.java

public String getOS() {
    String outputString = "";
    try {//from  ww w  .j a  v  a2s. c  o m
        Process su = Runtime.getRuntime().exec("sh");
        DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
        InputStream inputStream = su.getInputStream();
        outputStream.writeBytes("getprop ro.cm.version" + "\nprint alemun@romania\n");
        outputStream.flush();
        outputString = "";
        while (outputString.endsWith("alemun@romania\n") == false) {
            if (inputStream.available() > 0) {
                byte[] dstInput = new byte[inputStream.available()];
                inputStream.read(dstInput);
                String additionalString = new String(dstInput);
                outputString += additionalString;
            }
        }
        outputString = "Current ROM: " + outputString.substring(0, outputString.length() - 15);
        su.destroy();
    } catch (IOException e) {
        currentRomcardView.setVisibility(CardView.GONE);
    }
    if (outputString.replaceAll("jgcaap", "").length() < outputString.length())
        while (outputString.endsWith("bacon") == false)
            outputString = outputString.substring(0, outputString.length() - 1);
    else
        currentRomcardView.setVisibility(CardView.GONE);
    return outputString;
}

From source file:com.dao.ShopThread.java

private String getPayKey(HttpURLConnection conn, String type) throws Exception {
    LogUtil.debugPrintf("");
    String keyurl = conn.getURL().toString();
    String postParams = getKeyDynamicParams(keyurl, type);
    HttpURLConnection loginConn = getHttpPostConn(keyurl);
    loginConn.setRequestProperty("Accept-Encoding", "deflate");// ??
    loginConn.setRequestProperty("Referer", keyurl);
    loginConn.setRequestProperty("Host", "danbao.5173.com");
    loginConn.setRequestProperty("Pragma", "no-cache");
    loginConn.setRequestProperty("Content-Length", Integer.toString(postParams.length()));
    LogUtil.debugPrintf("?keyHEADER===" + loginConn.getRequestProperties());
    LogUtil.debugPrintf("?keypostParams===" + postParams);
    DataOutputStream wr = new DataOutputStream(loginConn.getOutputStream());
    wr.writeBytes(postParams);
    wr.flush();//from  w  w w.j  av  a 2s .  c o  m
    wr.close();
    BufferedReader in = new BufferedReader(new InputStreamReader(loginConn.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer(2000);
    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    String payurl = response.toString();
    LogUtil.debugPrintf("keyHtml:" + payurl);
    payurl = payurl.substring(payurl.indexOf("action") + "action".length(), payurl.indexOf("></form>"));
    payurl = payurl.substring(payurl.indexOf("\"") + 1, payurl.lastIndexOf("\""));
    Map<String, List<String>> header = loginConn.getHeaderFields();
    LogUtil.debugPrintf("?key?HEADER===" + header);
    List<String> cookie = header.get("Set-Cookie");
    if (cookie == null || cookie.size() == 0) {
        LogUtil.debugPrintf("?keycookie----------->");
    } else {
        LogUtil.debugPrintf("?keycookie----------->");
        LogUtil.debugPrintf("cookie====" + cookie);
        setCookies(cookie);
    }
    LogUtil.debugPrintf("?key?----------->");
    return payurl;
}

From source file:com.benefit.buy.library.http.query.callback.AbstractAjaxCallback.java

private static void writeData(DataOutputStream dos, String name, String filename, InputStream is, Progress p)
        throws IOException {
    dos.writeBytes(twoHyphens + boundary + lineEnd);
    dos.writeBytes("Content-Disposition: form-data; name=\"" + name + "\";" + " filename=\"" + filename + "\""
            + lineEnd);//from  w w  w. j av a 2 s .  co m
    //added to specify type
    dos.writeBytes("Content-Type: application/octet-stream");
    dos.writeBytes(lineEnd);
    dos.writeBytes("Content-Transfer-Encoding: binary");
    dos.writeBytes(lineEnd);
    dos.writeBytes(lineEnd);
    // AQUtility.copy(is, dos);
    AQUtility.copy(is, dos, is.available(), p);
    dos.writeBytes(lineEnd);
}

From source file:com.dao.ShopThread.java

private boolean doPTShop() throws Exception {
    long startTime = System.currentTimeMillis();
    // loadCookie();
    boolean result = true;
    LogUtil.webPrintf("??...");
    String postParams = addPTDynamicParams();
    LogUtil.webPrintf("???");
    LogUtil.webPrintf("?...");
    HttpURLConnection loginConn = getHttpPostConn(this.ptshopurl);
    loginConn.setRequestProperty("Host", "danbao.5173.com");
    loginConn.setRequestProperty("Content-Length", Integer.toString(postParams.length()));
    LogUtil.debugPrintf("?HEADER===" + loginConn.getRequestProperties());
    DataOutputStream wr = new DataOutputStream(loginConn.getOutputStream());
    wr.writeBytes(postParams);
    wr.flush();/* ww w . j a v  a2s  .  c  om*/
    wr.close();
    int responseCode = loginConn.getResponseCode();
    LogUtil.debugPrintf("\nSending 'POST' request to URL : " + this.ptshopurl);
    LogUtil.debugPrintf("Post parameters : " + postParams);
    LogUtil.debugPrintf("Response Code : " + responseCode);
    Map<String, List<String>> header = loginConn.getHeaderFields();
    LogUtil.debugPrintf("??HEADER===" + header);
    List<String> cookie = header.get("Set-Cookie");
    if (cookie == null || cookie.size() == 0) {
        result = false;
        LogUtil.webPrintf("?!");
    } else {
        LogUtil.webPrintf("??!");
        LogUtil.debugPrintf("cookie====" + cookie);
        LogUtil.debugPrintf("Location :" + loginConn.getURL().toString());
        setCookies(cookie);
        LogUtil.webPrintf("?...");
        String payurl = getPayKey(loginConn, "db");
        LogUtil.webPrintf("??");
        if (payurl != null && !payurl.equals("")) {
            LogUtil.debugPrintf("?url:" + payurl);
            LogUtil.webPrintf("...");
            pay(payurl);
            LogUtil.webPrintf("?");
        } else {
            LogUtil.webPrintf("?");
            LogUtil.debugPrintf("?url");
        }
    }
    return result;
}

From source file:com.dao.ShopThread.java

private boolean doJSShop() throws Exception {
    LogUtil.debugPrintf("?----------->");
    // loadCookie();
    boolean result = true;
    LogUtil.webPrintf("??...");
    String postParams = addJSDynamicParams();
    LogUtil.webPrintf("???");
    LogUtil.webPrintf("?...");
    HttpURLConnection loginConn = getHttpPostConn(this.jsshopurl);
    loginConn.setRequestProperty("Content-Length", Integer.toString(postParams.length()));
    LogUtil.debugPrintf("?HEADER===" + loginConn.getRequestProperties());
    DataOutputStream wr = new DataOutputStream(loginConn.getOutputStream());
    wr.writeBytes(postParams);
    wr.flush();/*w  w w.  j  av  a 2 s . c  o  m*/
    wr.close();
    int responseCode = loginConn.getResponseCode();
    LogUtil.debugPrintf("\nSending 'POST' request to URL : " + this.jsshopurl);
    LogUtil.debugPrintf("Post parameters : " + postParams);
    LogUtil.debugPrintf("Response Code : " + responseCode);
    Map<String, List<String>> header = loginConn.getHeaderFields();
    LogUtil.debugPrintf("??HEADER===" + header);
    List<String> cookie = header.get("Set-Cookie");
    if (cookie == null || cookie.size() == 0) {
        result = false;
        LogUtil.webPrintf("?!");
    } else {
        LogUtil.webPrintf("??!");
        LogUtil.debugPrintf("cookie====" + cookie);
        LogUtil.debugPrintf("Location :" + loginConn.getURL().toString());
        setCookies(cookie);
        LogUtil.webPrintf("?...");
        String payurl = getPayKey(loginConn, "js");
        LogUtil.webPrintf("??");
        if (payurl != null && !payurl.equals("")) {
            LogUtil.debugPrintf("?url:" + payurl);
            LogUtil.webPrintf("...");
            pay(payurl);
            LogUtil.webPrintf("?");
        } else {
            LogUtil.webPrintf("?");
            LogUtil.debugPrintf("?url");
        }
    }
    LogUtil.debugPrintf("??----------->");
    // System.out.println(list.toHtml());
    return result;
}

From source file:com.pinterest.deployservice.chat.HipChatManager.java

@Override
public void send(String from, String room, String message, String color) throws Exception {
    HashMap<String, String> params = new HashMap<>();
    params.put(ROOM_ID_KEY, URLEncoder.encode(room, "UTF-8"));
    params.put(USER_KEY, URLEncoder.encode(from, "UTF-8"));
    params.put(MESSAGE_KEY, URLEncoder.encode(message, "UTF-8"));
    if (color != null) {
        params.put(COLOR_KEY, color);/*from  w ww.  ja va  2s  .c  om*/
    }

    final String paramsToSend = this.constructQuery(params);
    String url = requestURL;
    DataOutputStream output = null;
    HttpURLConnection connection = null;
    for (int i = 0; i < TOTAL_RETRY; i++) {
        try {
            URL requestUrl = new URL(url);
            connection = (HttpURLConnection) requestUrl.openConnection();
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Content-Length", Integer.toString(paramsToSend.getBytes().length));
            connection.setRequestProperty("Content-Language", "en-US");
            output = new DataOutputStream(connection.getOutputStream());
            output.writeBytes(paramsToSend);
            return;
        } catch (Exception e) {
            LOG.error("Failed to send Hipchat message to room " + room, e);
        } finally {
            IOUtils.closeQuietly(output);
            if (connection != null) {
                connection.disconnect();
            }
        }
        Thread.sleep(1000);
    }
    LOG.error("Failed to send Hipchat message to room " + room);
}

From source file:com.intacct.ws.APISession.java

/**
  * You won't normally use this function, but if you just want to pass a fully constructed XML document
  * to Intacct, then use this function./*from w  w  w . j  a v a  2 s. c  o m*/
  *
  * @param String body     a Valid XML string
  * @param String endPoint URL to post the XML to
  *
  * @throws exception
  * @return String the raw XML returned by Intacct
  */
public static String execute(String body, String endpoint) throws IOException {
    StringBuffer response = null;
    HttpURLConnection connection = null;

    // Create connection
    URL url = new URL(endpoint);
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    connection.setRequestProperty("Content-Length", "" + Integer.toString(body.getBytes().length));
    connection.setRequestProperty("Content-Language", "en-US");

    connection.setUseCaches(false);
    connection.setDoInput(true);
    connection.setDoOutput(true);

    //Send request
    DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
    /*
     wr.writeBytes ("fName=" + URLEncoder.encode("???", "UTF-8") + body +
                "&lName=" + URLEncoder.encode("???", "UTF-8"));
    */

    wr.writeBytes("xmlrequest=" + URLEncoder.encode(body, "UTF-8"));
    wr.flush();
    wr.close();

    //Get Response   
    InputStream is = connection.getInputStream();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));

    String line;
    response = new StringBuffer();
    while ((line = rd.readLine()) != null) {
        response.append(line);
        response.append('\r');
    }
    rd.close();

    if (connection != null) {
        connection.disconnect();
    }

    return response.toString();
}