java compile get post http


Получить  ИП адресс через http  - bash

mkdir PrintIp
cd PrintIp

for var in \
https://repo1.maven.org/maven2/org/apache/httpcomponents/fluent-hc/4.5.12/fluent-hc-4.5.12.jar \
https://repo1.maven.org/maven2/org/apache/httpcomponents/httpcore/4.4.13/httpcore-4.4.13.jar \
https://repo1.maven.org/maven2/org/apache/httpcomponents/httpclient/4.5.3/httpclient-4.5.3.jar \
https://repo1.maven.org/maven2/commons-logging/commons-logging/1.2/commons-logging-1.2.jar 
do
#wget $var w.jar
#curl $var -o w.jar -s
curl $var -o w.jar
jar xf w.jar
rm w.jar
done

cat <<EOT> PrintIp.java
import org.apache.http.client.fluent.Executor;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;

public class PrintIp {
    static public void main(String[] args) {
        try {
            Executor executor = Executor.newInstance();
//            executor.auth("user","pass"); // digets auth
            Request request = Request
                    .Get("http://icanhazip.com/")
//                    .bodyString("{\"test\":123}", ContentType.APPLICATION_JSON)
                    .addHeader("Accept", "application/json; charset=utf-8")
                    .addHeader("Content-type", "application/json; charset=utf-8");
            String res = executor.execute(request).returnContent().asString();
            System.out.println(res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
EOT
cat <<EOT> MANIFEST.MF
Manifest-Version: 1.0
Main-Class: PrintIp
EOT
# компиляция 
javac -cp . *.java
# сборка
jar cf PrintIp.jar .
# запуск
java -cp PrintIp.jar PrintIp
# запуск через jar (без манифеста не работает)
java -jar PrintIp.jar 

Получить  ИП адресс через http  - cmd

 

mkdir test
cd test
curl https://repo1.maven.org/maven2/org/apache/httpcomponents/fluent-hc/4.5.12/fluent-hc-4.5.12.jar -o w.jar -s
jar xf w.jar
curl https://repo1.maven.org/maven2/org/apache/httpcomponents/httpcore/4.4.13/httpcore-4.4.13.jar -o ww.jar -s
jar xf ww.jar
curl https://repo1.maven.org/maven2/org/apache/httpcomponents/httpclient/4.5.3/httpclient-4.5.3.jar -o www.jar -s
jar xf www.jar
curl https://repo1.maven.org/maven2/commons-logging/commons-logging/1.2/commons-logging-1.2.jar -o wwww.jar -s
jar xf wwww.jar

cat <<EOT> TestHttp.java

import org.apache.http.client.fluent.Executor;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;

public class TestHttp {
    static public void main(String[] args) {
        try {
            Executor executor = Executor.newInstance();
//            executor.auth("user","pass"); // digets auth
            Request request = Request
                    .Get("http://icanhazip.com/")
//                    .bodyString("{\"test\":123}", ContentType.APPLICATION_JSON)
                    .addHeader("Accept", "application/json; charset=utf-8")
                    .addHeader("Content-type", "application/json; charset=utf-8");
            String res = executor.execute(request).returnContent().asString();
            System.out.println(res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
EOT
javac -cp '*' *.java
java -cp . TestHttp

https from localhost

package test;

import org.apache.http.client.fluent.Executor;
import org.apache.http.client.fluent.Request;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

public class TestHttp {
    static public void main(String[] args) {
        try {
            TrustManager[] trustAllCerts = new TrustManager[] {
                    new X509TrustManager() {
                        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                            return null;
                        }
                        public void checkClientTrusted(X509Certificate[] certs, String authType) {  }
                        public void checkServerTrusted(X509Certificate[] certs, String authType) {  }
                    }
            };
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new SecureRandom());
            CloseableHttpClient httpClient = HttpClients.custom().setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).setSslcontext(sc).build();
            Executor executor = Executor.newInstance(httpClient);
//            executor.auth("user","pass"); // digets auth
            Request request = Request
                    .Post("https://laravel.myy/api/wowza/auth")
                    .bodyString("{\"test\":123}", ContentType.APPLICATION_JSON)
                    .addHeader("Accept", "application/json; charset=utf-8")
                    .addHeader("Content-type", "application/json; charset=utf-8");
            String res = executor.execute(request).returnContent().asString();
            System.out.println(res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

PrintPATH

mkdir PrintPATH
cd PrintPATH

cat <<EOT> PrintPATH.java
public class PrintPATH {
    static public void main(String[] args) {
        try {
            String __DIR__ = (new java.io.File(PrintPATH.class.getProtectionDomain().getCodeSource().getLocation().getPath())).getParent();
            System.out.println(__DIR__);
            // еще способ но первый лучше
            String command = "pwd";           
            String res = (new java.io.BufferedReader(new java.io.InputStreamReader(Runtime.getRuntime().exec(command).getInputStream()))).readLine();
            System.out.println(res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
EOT
javac -cp . *.java 
jar cf PrintPATH.jar . 
java -cp PrintPATH.jar PrintPATH

ShellExec

mkdir ShellExec
cd ShellExec

cat <<EOT> ShellExec.java
public class ShellExec {
    static public void main(String[] args) {
        try {
            String s = null;
            String command = "pwd";
            Process proc = Runtime.getRuntime().exec(command);
            java.io.BufferedReader stdInput = new java.io.BufferedReader(new java.io.InputStreamReader(proc.getInputStream()));
            java.io.BufferedReader stdError = new java.io.BufferedReader(new java.io.InputStreamReader(proc.getErrorStream()));
            //
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
EOT
javac -cp . *.java 
jar cf ShellExec.jar . 
java -cp ShellExec.jar ShellExec

PrintCPU - вывод загрженности CPU B linux

mkdir PrintCPU
cd PrintCPU

cat <<EOT> $(pwd)/PrintCPU.sh
#!/bin/bash
awk '{u=\$2+\$4; t=\$2+\$4+\$5; if (NR==1){u1=u; t1=t;} else print (\$2+\$4-u1) * 100 / (t-t1) "%"; }' \\
<(grep 'cpu ' /proc/stat) <(sleep 1;grep 'cpu ' /proc/stat)
EOT
chmod +x $(pwd)/PrintCPU.sh

cat <<EOT> PrintCPU.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class PrintCPU {
    static public void main(String[] args) {
        try {
            String __DIR__ = (new java.io.File(PrintCPU.class.getProtectionDomain().getCodeSource().getLocation().getPath())).getParent();
            String command = __DIR__ + "/PrintCPU.sh";
            String s = null;
            Process proc = Runtime.getRuntime().exec(command);
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
            //
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
EOT
javac -cp . *.java 
jar cf PrintCPU.jar . 
java -cp PrintCPU.jar PrintCPU

 

 

 

 

 

 

 

 

                    URL url = new URL(publishCheckUrl);
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    con.setRequestMethod("POST");
                    con.setRequestProperty("Content-Type", "application/json");
                    con.setDoOutput(true);
                    Map<String, String> payload = new HashMap<>();
                    payload.put("jti", token);
                    ObjectMapper objectMapper = new ObjectMapper();
                    try (OutputStream reqOutput = con.getOutputStream()) {
                        String requestBody = objectMapper.writeValueAsString(payload);
                        byte[] requestBodyBytes = requestBody.getBytes();
                        reqOutput.write(requestBodyBytes, 0, requestBodyBytes.length);
                    }
                    int status = con.getResponseCode();
                    InputStream stream = con.getInputStream();
                    stream.close();
                    if (status == 200) {
                        isValid = true;
                    }
                    con.disconnect();