Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. package com.javabeans;
  2.  
  3. import java.beans.IntrospectionException;
  4. import java.io.BufferedReader;
  5. import java.io.DataOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.net.HttpURLConnection;
  9. import java.net.MalformedURLException;
  10. import java.net.URL;
  11.  
  12. public class Main {
  13.  
  14. static double[] a;
  15.  
  16. public static void main(String[] args) throws IntrospectionException {
  17.  
  18. new Main().post();
  19. new Main().get("54ee88b3255fea04c9e4402108cd8063146d2abb");
  20. }
  21.  
  22. public void post() {
  23. String query = "http://176.57.217.200/api-token-auth/";
  24. HttpURLConnection connection = null;
  25. try {
  26. connection = (HttpURLConnection) new URL(query).openConnection();
  27. connection.setRequestMethod("POST");
  28. // connection.setRequestProperty("username", "admin");
  29. // connection.setRequestProperty("password", "Cfxfhysq1");
  30. connection.setDoOutput(true);
  31. String urlParameters = "username=admin&password=Cfxfhysq1";
  32.  
  33. DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
  34. dataOutputStream.writeBytes(urlParameters);
  35. dataOutputStream.flush();
  36. dataOutputStream.close();
  37.  
  38. StringBuilder sb = new StringBuilder();
  39.  
  40. if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) {
  41. System.out.println(connection.getResponseCode());
  42. BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
  43. String line;
  44. while ((line = in.readLine()) != null) {
  45. sb.append(line);
  46. sb.append("\n");
  47. }
  48. System.out.println(sb.toString());
  49. } else {
  50. System.out.println("fail: " + connection.getResponseCode() + ", " + connection.getResponseMessage());
  51. }
  52.  
  53. } catch (MalformedURLException e) {
  54. e.printStackTrace();
  55. } catch (IOException e) {
  56. e.printStackTrace();
  57. }
  58. }
  59.  
  60. public void get(String token) {
  61. String query = "http://176.57.217.200/api/zakaz/";
  62. HttpURLConnection connection = null;
  63. try {
  64. connection = (HttpURLConnection) new URL(query).openConnection();
  65. connection.setRequestMethod("GET");
  66. connection.setUseCaches(false);
  67. connection.setConnectTimeout(250);
  68. connection.setReadTimeout(250);
  69. connection.setRequestProperty("Authorization", "Token " + token);
  70.  
  71. connection.connect();
  72.  
  73. StringBuilder sb = new StringBuilder();
  74.  
  75. if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) {
  76. System.out.println(connection.getResponseCode());
  77. BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
  78. String line;
  79. while ((line = in.readLine()) != null) {
  80. sb.append(line);
  81. sb.append("\n");
  82. }
  83. System.out.println(sb.toString());
  84. } else {
  85. System.out.println("fail: " + connection.getResponseCode() + ", " + connection.getResponseMessage());
  86. }
  87.  
  88.  
  89. } catch (Throwable cause) {
  90. cause.printStackTrace();
  91. } finally {
  92. if (connection != null) {
  93. connection.disconnect();
  94. }
  95. }
  96. }
  97.  
  98. public void connPost() {
  99.  
  100. }
  101.  
  102. // public String toJson(String[] key, String[] value) {
  103. // String jsonString = "{";
  104. // for (int i = 0; i < key.length; i++) {
  105. // if (i > 0)
  106. // jsonString += ", ";
  107. // jsonString += "\""+key[i]+"\":\"" + value[i] + "\"";
  108. // }
  109. // jsonString += "}";
  110. //
  111. // }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement