Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. package mk.plugin.napthe.util;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.DataOutputStream;
  5. import java.io.InputStreamReader;
  6. import java.net.Authenticator;
  7. import java.net.HttpURLConnection;
  8. import java.net.PasswordAuthentication;
  9. import java.net.URL;
  10. import java.util.Base64;
  11.  
  12. import com.google.gson.JsonObject;
  13.  
  14. import mk.plugin.napthe.config.ConfigValue;
  15. import mk.plugin.napthe.object.Card;
  16.  
  17. public class CardUtils {
  18.  
  19. public static final String GatevnURL = "https://sv.gatevn.net/api/insertCardInfo";
  20.  
  21. public static String toJson(Card card) {
  22. JsonObject cac = new JsonObject();
  23. cac.addProperty("UserTransactionID", ConfigValue.USER_TRANSACTION_ID.get());
  24. cac.addProperty("ProviderID", card.getCardType().getCode());
  25. cac.addProperty("CardSerial", card.getSeri());
  26. cac.addProperty("CardPin", card.getPin());
  27. cac.addProperty("CardUserAmount", card.getCardPrice().getValue());
  28. cac.addProperty("ApiKey", ConfigValue.API_KEY.get());
  29.  
  30. return cac.toString();
  31. }
  32.  
  33. public static String post(String data) {
  34. try {
  35. URL url = new URL(GatevnURL);
  36. Authenticator.setDefault(new Authenticator() {
  37. @Override
  38. protected PasswordAuthentication getPasswordAuthentication() {
  39. return new PasswordAuthentication(ConfigValue.API_USER.get(), ConfigValue.API_KEY.get().toCharArray());
  40. }
  41. });
  42. HttpURLConnection con = (HttpURLConnection) url.openConnection();
  43. con.setDoInput(true);
  44. con.setDoOutput(true);
  45. con.setInstanceFollowRedirects(true);
  46. con.setRequestMethod("POST");
  47. con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  48. con.setRequestProperty("charset", "utf-8");
  49. con.setRequestProperty("Content-Length", Integer.toString(data.getBytes().length));
  50. con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11");
  51. // con.setRequestProperty("Authorization", "Basic " + getAuthantication(ConfigValue.API_USER.get(), ConfigValue.API_KEY.get()));
  52. con.setUseCaches(false);
  53. con.connect();
  54.  
  55. DataOutputStream dos = new DataOutputStream(con.getOutputStream());
  56. dos.write(data.getBytes());
  57. dos.close();
  58.  
  59. BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
  60. StringBuilder result = new StringBuilder();
  61.  
  62. String line = null;
  63. while((line = reader.readLine()) != null) {
  64. result.append(line);
  65. }
  66.  
  67. con.disconnect();
  68.  
  69. return result.toString();
  70. }
  71. catch (Exception e) {
  72. e.printStackTrace();
  73. }
  74.  
  75. return null;
  76. }
  77.  
  78. public static String getAuthantication(String username, String password) {
  79. String auth = new String(Base64.getEncoder().encode(new String(username + ":" + password).getBytes()));
  80. return auth;
  81. }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement