update
This commit is contained in:
parent
2992f4f408
commit
4f46de8d00
3330 changed files with 394553 additions and 76939 deletions
105
.config/Code/User/History/-79eaee52/h0Wp.java
Normal file
105
.config/Code/User/History/-79eaee52/h0Wp.java
Normal file
|
@ -0,0 +1,105 @@
|
|||
package com.rutils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.net.http.HttpRequest.BodyPublishers;
|
||||
import java.net.http.HttpResponse.BodyHandlers;
|
||||
import java.security.KeyStore;
|
||||
|
||||
import javax.net.ssl.KeyManagerFactory;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManagerFactory;
|
||||
|
||||
|
||||
public class HttpsUtil{
|
||||
|
||||
static HttpClient client;
|
||||
|
||||
static{
|
||||
try{
|
||||
KeyStore keyStore = KeyStore.getInstance("PKCS12");
|
||||
keyStore.load(HttpsUtil.class.getClassLoader().getResourceAsStream("ksclient.p12"), "client7548".toCharArray());
|
||||
|
||||
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
||||
tmf.init(keyStore);
|
||||
|
||||
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
|
||||
kmf.init(keyStore, "client7548".toCharArray());
|
||||
|
||||
SSLContext sslContext = SSLContext.getInstance("TLS");
|
||||
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
|
||||
|
||||
client = HttpClient.newBuilder()
|
||||
.sslContext(sslContext)
|
||||
.build();
|
||||
}
|
||||
catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static int registerUser(String username, String password, String passwordAgain){
|
||||
if(password.equals(passwordAgain) && username.length() != 0 && password.length() != 0){
|
||||
String jsonPayload = String.format("{\"username\":\"%s\", \"password\":\"%s\"}", username, password);
|
||||
|
||||
try{
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://rafayahmad.serverminecraft.net:25565/register"))
|
||||
.header("Content-Type", "application/json")
|
||||
.POST(BodyPublishers.ofString(jsonPayload))
|
||||
.build();
|
||||
|
||||
HttpResponse<String> httpResponse = client.send(request, BodyHandlers.ofString());
|
||||
int response = Integer.parseInt(httpResponse.body());
|
||||
|
||||
return response;
|
||||
}
|
||||
catch(URISyntaxException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (InterruptedException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static int verifyCredentials(String username, String password){
|
||||
try{
|
||||
String jsonPayload = String.format("{\"username\":\"%s\", \"password\":\"%s\"}", username, password);
|
||||
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("https://rafayahmad.serveminecraft.net:25565/login"))
|
||||
.header("Content-Type", "application/json")
|
||||
.POST(BodyPublishers.ofString(jsonPayload))
|
||||
.build();
|
||||
|
||||
HttpResponse<String> httpResponse = client.send(request, BodyHandlers.ofString());
|
||||
|
||||
int response = Integer.parseInt(httpResponse.body());
|
||||
return response;
|
||||
}
|
||||
catch(URISyntaxException e1){
|
||||
System.err.println("Invalid Uri provided for http request");
|
||||
e1.printStackTrace();
|
||||
}
|
||||
catch(InterruptedException e2){
|
||||
System.err.println("sending request interuppterd Eroor");
|
||||
e2.printStackTrace();
|
||||
}
|
||||
catch(IOException e3){
|
||||
System.err.println("io exeception when sending http request");
|
||||
e3.printStackTrace();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue