List of usage examples for java.io DataOutputStream write
public synchronized void write(int b) throws IOException
b
) to the underlying output stream. From source file:Main.java
public static void main(String[] args) throws IOException { int[] buf = { 65, 66, 67, 68, 69, 70, 71 }; ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); for (int i : buf) { dos.write(i); }//from ww w .j av a2 s . c om dos.flush(); for (byte b : baos.toByteArray()) { char c = (char) b; System.out.print(c); } }
From source file:Main.java
public static void main(String[] args) throws IOException { byte[] buf = { 12, 11, 22, 33, 44 }; FileOutputStream fos = new FileOutputStream("c:/test.txt"); DataOutputStream dos = new DataOutputStream(fos); int size = 0; for (byte b : buf) { dos.write(b); size = dos.size();//from w w w .j a v a 2 s.c o m System.out.print("Size: " + size + "; "); } }
From source file:Main.java
public static void main(String args[]) throws Exception { String sessionCookie = null;/*from w w w .ja va 2 s.c om*/ URL url = new java.net.URL("http://127.0.0.1/yourServlet"); URLConnection con = url.openConnection(); if (sessionCookie != null) { con.setRequestProperty("cookie", sessionCookie); } con.setUseCaches(false); con.setDoOutput(true); con.setDoInput(true); ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(byteOut); out.flush(); byte buf[] = byteOut.toByteArray(); con.setRequestProperty("Content-type", "application/octet-stream"); con.setRequestProperty("Content-length", "" + buf.length); DataOutputStream dataOut = new DataOutputStream(con.getOutputStream()); dataOut.write(buf); dataOut.flush(); dataOut.close(); DataInputStream in = new DataInputStream(con.getInputStream()); int count = in.readInt(); in.close(); if (sessionCookie == null) { String cookie = con.getHeaderField("set-cookie"); if (cookie != null) { sessionCookie = parseCookie(cookie); System.out.println("Setting session ID=" + sessionCookie); } } System.out.println(count); }
From source file:org.apache.oltu.oauth2.client.OAuthClientTest.java
public static void main(String[] args) throws OAuthSystemException, IOException { String callback = "http://localhost:8080/"; String clientId = "131804060198305"; String secret = "3acb294b071c9aec86d60ae3daf32a93"; String host = "http://smoke-track.herokuapp.com"; host = "http://localhost:3000"; String authUri = host + "/oauth/authorize"; String tokenUri = host + "/oauth/token"; String appUri = host + "/habits"; callback = "http://localhost:8080"; clientId = "728ad798943fff1afd90e79765e9534ef52a5b166cfd25f055d1c8ff6f3ae7fd"; secret = "3728e0449052b616e2465c04d3cbd792f2d37e70ca64075708bfe8b53c28d529"; clientId = "e42ae40e269d9a546316f93e42edf52a18934c6a68de035f8b615343c4b81eb0"; secret = "3b1a720c311321c29852dc4735517f6ece0c991d4be677539cb430c7ee4d1097"; try {// w w w . ja va2 s . co m OAuthClientRequest request = OAuthClientRequest.authorizationLocation(authUri).setClientId(clientId) .setRedirectURI(callback).setResponseType("code").buildQueryMessage(); //in web application you make redirection to uri: System.out.println("Visit: " + request.getLocationUri() + "\nand grant permission"); System.out.print("Now enter the OAuth code you have received in redirect uri "); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String code = br.readLine(); request = OAuthClientRequest.tokenLocation(tokenUri).setGrantType(GrantType.AUTHORIZATION_CODE) .setClientId(clientId).setClientSecret(secret).setRedirectURI(callback).setCode(code) .buildBodyMessage(); OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient()); OAuthJSONAccessTokenResponse oAuthResponse = oAuthClient.accessToken(request, OAuthJSONAccessTokenResponse.class); System.out.println("Access Token: " + oAuthResponse.getAccessToken() + ", Expires in: " + oAuthResponse.getExpiresIn()); URL appURL = new URL(appUri); HttpURLConnection con = (HttpURLConnection) appURL.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/json"); con.setRequestProperty("Accept", "application/json"); con.setRequestProperty("Accept-Encoding", "gzip, deflate"); con.setRequestProperty("Authorization", "Bearer " + oAuthResponse.getAccessToken()); JSONObject body = new JSONObject(); body.put("color", "green"); body.put("name", "Java Test"); body.put("description", "Test Habit"); byte[] bodyBytes = body.toString().getBytes(); con.setRequestProperty("Content-Length", Integer.toString(bodyBytes.length)); con.setInstanceFollowRedirects(false); con.setUseCaches(false); con.setDoInput(true); con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.write(bodyBytes); wr.flush(); wr.close(); //InputStream is = con.getInputStream(); int status = con.getResponseCode(); System.out.println("Status: " + status); } catch (OAuthProblemException e) { System.out.println("OAuth error: " + e.getError()); System.out.println("OAuth error description: " + e.getDescription()); } catch (JSONException e) { e.printStackTrace(); } }
From source file:cn.jumper.study.http.ClientFormLogin.java
public static void main(String[] args) throws Exception { BasicCookieStore cookieStore = new BasicCookieStore(); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build(); HttpHost proxy = new HttpHost("192.168.10.3", 8080, "http"); RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); try {/* www. jav a 2 s . co m*/ HttpGet httpget = new HttpGet("http://www.ksf-food.com/admin/Login.asp"); httpget.setConfig(config); CloseableHttpResponse response1 = httpclient.execute(httpget); try { HttpEntity entity = response1.getEntity(); System.out.println("Login form get: " + response1.getStatusLine()); EntityUtils.consume(entity); System.out.println("Initial set of cookies:"); List<Cookie> cookies = cookieStore.getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); } } } finally { response1.close(); } String code = ""; try { HttpUriRequest httpgetCode = RequestBuilder.get() .setUri("http://www.ksf-food.com/admin/inc/checkcode.asp").setConfig(config).build(); /* * HttpGet httpgetCode = new HttpGet( * "http://www.qufuev.com/admin/inc/checkcode.asp"); * httpgetCode.setConfig(config); */ System.out.println("Executing request " + httpgetCode.getRequestLine()); System.out.println("========================================================"); System.out.println("==httpget header =="); for (Header header : httpgetCode.getAllHeaders()) { System.out.println(header.getName() + ":" + header.getValue()); } System.out.println("==httpget header =="); ResponseHandler<String> responseHandler = new ResponseHandler<String>() { public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); System.out.println("==respons header =="); for (Header header : response.getAllHeaders()) { System.out.println(header.getName() + ":" + header.getValue()); } System.out.println("==respons header =="); String fileName = System.currentTimeMillis() + ""; DataOutputStream dataOutputStream = new DataOutputStream( new FileOutputStream("d://test//e3//" + fileName + ".jpg")); dataOutputStream.write(EntityUtils.toByteArray(entity)); dataOutputStream.close(); return ImageTest.getAllOcr("d://test//e3//" + fileName + ".jpg"); } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; code = httpclient.execute(httpgetCode, responseHandler); System.out.println("ClientFormLogin.main()-CheckCode:" + code); System.out.println("----------------------------------------"); } catch (IOException e) { e.printStackTrace(); } HttpUriRequest login = RequestBuilder.post() .setUri(new URI("http://www.ksf-food.com/admin/Admin_ChkLogin.asp")) .addParameter("UserName", "username").addParameter("Password", "password") .addParameter("CheckCode", code).setConfig(config).build(); System.out.println("========================================================"); System.out.println("==httpget header =="); for (Header header : login.getAllHeaders()) { System.out.println(header.getName() + ":" + header.getValue()); } CloseableHttpResponse response2 = httpclient.execute(login); try { HttpEntity entity = response2.getEntity(); System.out.println("Login form post: " + response2.getStatusLine()); // EntityUtils.consume(entity); System.out.println("ClientFormLogin.main():\\n" + EntityUtils.toString(entity, "GBK")); System.out.println("Post logon cookies:"); List<Cookie> cookies = cookieStore.getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); } } } finally { response2.close(); } } finally { httpclient.close(); } }
From source file:com.tc.simple.apn.quicktests.Test.java
/** * @param args//from w w w . j ava 2 s. com */ public static void main(String[] args) { SSLSocket socket = null; try { String host = "gateway.sandbox.push.apple.com"; int port = 2195; String token = "de7f197546e41a76684f8e2d89f397ed165298d7772f4bd9b0f39c674b185b0f"; System.out.println(token.toCharArray().length); //String token = "8cebc7c08f79fa62f0994eb4298387ff930857ff8d14a50de431559cf476b223"; KeyStore keyStore = KeyStore.getInstance("PKCS12"); keyStore.load(Test.class.getResourceAsStream("egram-dev-apn.p12"), "xxxxxxxxx".toCharArray()); KeyManagerFactory keyMgrFactory = KeyManagerFactory .getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyMgrFactory.init(keyStore, "xxxxxxxxx".toCharArray()); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(keyMgrFactory.getKeyManagers(), null, null); SSLSocketFactory socketFactory = sslContext.getSocketFactory(); socket = (SSLSocket) socketFactory.createSocket(host, port); String[] cipherSuites = socket.getSupportedCipherSuites(); socket.setEnabledCipherSuites(cipherSuites); socket.startHandshake(); char[] t = token.toCharArray(); byte[] b = Hex.decodeHex(t); OutputStream outputstream = socket.getOutputStream(); String payload = "{\"aps\":{\"alert\":\"yabadabadooo\"}}"; int expiry = (int) ((System.currentTimeMillis() / 1000L) + 7200); ByteArrayOutputStream bout = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bout); //command dos.writeByte(1); //id dos.writeInt(900); //expiry dos.writeInt(expiry); //token length. dos.writeShort(b.length); //token dos.write(b); //payload length dos.writeShort(payload.length()); //payload. dos.write(payload.getBytes()); byte[] byteMe = bout.toByteArray(); socket.getOutputStream().write(byteMe); socket.setSoTimeout(900); InputStream in = socket.getInputStream(); System.out.println(APNErrors.getError(in.read())); in.close(); outputstream.close(); } catch (Exception e) { e.printStackTrace(); } finally { try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
From source file:Main.java
public static void writeShort(final DataOutputStream output, final short value) throws IOException { output.write(value >> 0); output.write(value >> 8);//from w w w .ja v a 2s .co m }
From source file:Main.java
public static void writeInt(final DataOutputStream output, final int value) throws IOException { output.write(value >> 0); output.write(value >> 8);//www . j a v a2s . c om output.write(value >> 16); output.write(value >> 24); }
From source file:Main.java
public static void writeFile(File file, String string) throws IOException { file.getParentFile().mkdirs();//from w w w .j a v a 2 s .c o m DataOutputStream dout = new DataOutputStream(new FileOutputStream(file)); dout.write(string.getBytes()); dout.close(); }
From source file:Main.java
public static byte[] getBodyBytes(String ip, String port, String key) throws NumberFormatException, IOException { String[] ipArr = ip.split("\\."); byte[] ipByte = new byte[4]; ipByte[0] = (byte) (Integer.parseInt(ipArr[0]) & 0xFF); ipByte[1] = (byte) (Integer.parseInt(ipArr[1]) & 0xFF); ipByte[2] = (byte) (Integer.parseInt(ipArr[2]) & 0xFF); ipByte[3] = (byte) (Integer.parseInt(ipArr[3]) & 0xFF); ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.write(ipByte); dos.writeShort(Short.parseShort(port)); dos.writeByte(key.getBytes().length); dos.write(key.getBytes());//w w w . j a va 2s. c om byte[] bs = baos.toByteArray(); baos.close(); dos.close(); return bs; }