code: public JSONObject loginToMC(String mcUrl, String mcUserName, String mcPassword, String proxyAddress, String proxyUsername, String proxyPassword) {
  
          JSONObject returnObject = new JSONObject();
          try {
              Map<String, String> headers = new HashMap<String, String>();
              headers.put(Constants.ACCEPT, "application/json");
              headers.put(Constants.CONTENT_TYPE, "application/json;charset=UTF-8");
  
              JSONObject sendObject = new JSONObject();
              sendObject.put("name", mcUserName);
              sendObject.put("password", mcPassword);
              sendObject.put("accountName", "default");
              HttpResponse response = HttpUtils.post(HttpUtils.setProxyCfg(proxyAddress, proxyUsername, proxyPassword), mcUrl + Constants.LOGIN_URL, headers, sendObject.toJSONString().getBytes());
  
              if (response != null && response.getHeaders() != null) {
                  Map<String, List<String>> headerFields = response.getHeaders();
                  List<String> hp4mSecretList = headerFields.get(Constants.LOGIN_SECRET);
                  String hp4mSecret = null;
                  if (hp4mSecretList != null && hp4mSecretList.size() != 0) {
                      hp4mSecret = hp4mSecretList.get(0);
                  }
                  List<String> setCookieList = headerFields.get(Constants.SET_COOKIE);
                  String setCookie = null;
                  if (setCookieList != null && setCookieList.size() != 0) {
                      setCookie = setCookieList.get(0);
                      for(String str : setCookieList){
                          if(str.contains(Constants.JSESSIONID) && str.startsWith(Constants.JSESSIONID)){
                              setCookie = str;
                              break;
                          }
                      }
                  }
                  String jsessionId = getJSESSIONID(setCookie);
                  returnObject.put(Constants.JSESSIONID, jsessionId);
                  returnObject.put(Constants.LOGIN_SECRET, hp4mSecret);
                  returnObject.put(Constants.COOKIE, Constants.JESEEIONEQ + jsessionId);
              }
  
          } catch (Exception e) {
              e.printStackTrace();
          }
          return returnObject;
      }
docs: Login to MC