Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javabeans;
- import java.beans.IntrospectionException;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- public class Main {
- static double[] a;
- public static void main(String[] args) throws IntrospectionException {
- new Main().post();
- new Main().get("54ee88b3255fea04c9e4402108cd8063146d2abb");
- }
- public void post() {
- String query = "http://176.57.217.200/api-token-auth/";
- HttpURLConnection connection = null;
- try {
- connection = (HttpURLConnection) new URL(query).openConnection();
- connection.setRequestMethod("POST");
- // connection.setRequestProperty("username", "admin");
- // connection.setRequestProperty("password", "Cfxfhysq1");
- connection.setDoOutput(true);
- String urlParameters = "username=admin&password=Cfxfhysq1";
- DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
- dataOutputStream.writeBytes(urlParameters);
- dataOutputStream.flush();
- dataOutputStream.close();
- StringBuilder sb = new StringBuilder();
- if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) {
- System.out.println(connection.getResponseCode());
- BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
- String line;
- while ((line = in.readLine()) != null) {
- sb.append(line);
- sb.append("\n");
- }
- System.out.println(sb.toString());
- } else {
- System.out.println("fail: " + connection.getResponseCode() + ", " + connection.getResponseMessage());
- }
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void get(String token) {
- String query = "http://176.57.217.200/api/zakaz/";
- HttpURLConnection connection = null;
- try {
- connection = (HttpURLConnection) new URL(query).openConnection();
- connection.setRequestMethod("GET");
- connection.setUseCaches(false);
- connection.setConnectTimeout(250);
- connection.setReadTimeout(250);
- connection.setRequestProperty("Authorization", "Token " + token);
- connection.connect();
- StringBuilder sb = new StringBuilder();
- if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) {
- System.out.println(connection.getResponseCode());
- BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
- String line;
- while ((line = in.readLine()) != null) {
- sb.append(line);
- sb.append("\n");
- }
- System.out.println(sb.toString());
- } else {
- System.out.println("fail: " + connection.getResponseCode() + ", " + connection.getResponseMessage());
- }
- } catch (Throwable cause) {
- cause.printStackTrace();
- } finally {
- if (connection != null) {
- connection.disconnect();
- }
- }
- }
- public void connPost() {
- }
- // public String toJson(String[] key, String[] value) {
- // String jsonString = "{";
- // for (int i = 0; i < key.length; i++) {
- // if (i > 0)
- // jsonString += ", ";
- // jsonString += "\""+key[i]+"\":\"" + value[i] + "\"";
- // }
- // jsonString += "}";
- //
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement