Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. package com.lovecoding.miit.API;
  2.  
  3. import java.io.BufferedOutputStream;
  4. import java.io.BufferedWriter;
  5. import java.io.DataOutputStream;
  6. import java.io.IOException;
  7. import java.io.OutputStream;
  8. import java.io.OutputStreamWriter;
  9. import java.net.HttpURLConnection;
  10. import java.net.URL;
  11.  
  12. /**
  13.  * Created by alex on 21.02.18.
  14.  */
  15.  
  16. public class RequestUtil {
  17.  
  18.     private final static int CONNECTION_TIMEOUTREAD=15000;
  19.     private final static int CONNECTION_TIMEOUT=20000;
  20.  
  21.  
  22.     public static int getAuth(String login, String password) {
  23.         HttpURLConnection urlcon = null;
  24.         OutputStream wr = null;
  25.         int responseCode=-1;
  26.         String query = "j_username=" + login + "&j_password=" + password + "&submit=%D0%92%D0%BE%D0%B9%D1%82%D0%B8";
  27.         try {
  28.             urlcon = (HttpURLConnection) new URL("https://my.miit.ru/cabinet/hello/j_security_check").openConnection();
  29.             urlcon.setReadTimeout(CONNECTION_TIMEOUTREAD);
  30.             urlcon.setConnectTimeout(CONNECTION_TIMEOUT);
  31.             urlcon.setRequestMethod("POST");
  32.             urlcon.setDoOutput(true);
  33.             wr = urlcon.getOutputStream();
  34.             BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(wr,"UTF-8"));
  35.             bufferedWriter.write(query);
  36.             bufferedWriter.flush();
  37.             bufferedWriter.close();
  38.             responseCode=urlcon.getResponseCode();
  39.         } catch (IOException ioe) {
  40.  
  41.         } finally {
  42.             try {
  43.                 if (urlcon != null) {
  44.                     urlcon.disconnect();
  45.                 }
  46.                 if (wr != null) {
  47.                     wr.close();
  48.                 }
  49.             } catch (IOException ioe) {
  50.             }
  51.         }
  52.         return responseCode;
  53.     }
  54.  
  55.  
  56.  
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement