Advertisement
Guest User

Untitled

a guest
Apr 8th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.net.HttpURLConnection;
  4. import java.net.URL;
  5.  
  6. public class Json {
  7.     public static void main(String args[]) {
  8.         String query = "http://cs.saject.ru/cs108/dataStorage.php";
  9.         HttpURLConnection connection = null;
  10.    
  11.         try {
  12.             String data = "descr=test&user=test&pass=test&note=test&addkey=test"; //строка для присоединения к данным
  13.             byte[] dataBytes = data.getBytes("UTF-8");
  14.            
  15.             connection = (HttpURLConnection) new URL(query).openConnection();
  16.             connection.setRequestMethod("POST");
  17.             connection.setUseCaches(false);
  18.             connection.setConnectTimeout(250);
  19.             connection.setReadTimeout(250);
  20.             connection.setDoOutput(true);
  21.             connection.getOutputStream().write(dataBytes);
  22.            
  23.             connection.connect();
  24.            
  25.             StringBuilder sb = new StringBuilder();
  26.            
  27.             if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) {
  28.                 System.out.println("Подключение установлено");
  29.                 BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
  30.                 String line;
  31.                 String s[];
  32.                
  33.                 while ((line = in.readLine()) != null) {
  34.                     /*s = line.split(",");
  35.                     System.out.println(s[0] + " " + s[1] + " " + s[2] + " " + s[3] + " " + s[4]);*/
  36.                    
  37.                     sb.append(line);
  38.                     sb.append("\n");
  39.                 }
  40.                
  41.                 System.out.println(sb.toString());
  42.             } else {
  43.                 System.out.println("fail: " + connection.getResponseCode() + ", " + connection.getResponseMessage());
  44.             }
  45.         } catch (Exception e) {
  46.             e.printStackTrace();
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement