Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.net.URL;
- import java.net.URLEncoder;
- import javax.net.ssl.HostnameVerifier;
- import javax.net.ssl.HttpsURLConnection;
- import javax.net.ssl.SSLContext;
- import javax.net.ssl.SSLSession;
- import javax.net.ssl.TrustManager;
- import javax.net.ssl.X509TrustManager;
- public class Main {
- private static final String my_url = "https://10.1.0.10:8090/login.xml";
- private static final String url2 = "https://www.google.com";
- private static final String url3 = "https://www.facebook.com";
- @SuppressWarnings("deprecation")
- public static void main(String[] args) throws IOException {
- String line;
- URL url = new URL(my_url);
- certificate_and_verification();//Some random certificate validation check
- HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
- connection.setDoOutput(true);
- connection.setRequestMethod("POST");
- connection.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
- connection.setRequestProperty( "charset", "utf-8");
- connection.setUseCaches( false );
- BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- while((line = br.readLine() )!= null)
- {
- System.out.println(line);
- }
- // int responseCode = connection.getResponseCode();
- /*BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- if (responseCode == HttpsURLConnection.HTTP_OK){
- System.out.println("Connection successful");
- }
- while((line = br.readLine() )!= null)
- {
- System.out.println(line);
- }
- */
- //Let's test only for f2014866 id
- String POST_PARAMS = "username="+ URLEncoder.encode("f2014866")+ "&password="+URLEncoder.encode("555")+"&mode="+ 191;
- OutputStream os = connection.getOutputStream();
- os.write(POST_PARAMS.getBytes());
- os.flush();
- os.close();
- BufferedReader br1 = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- while((line = br1.readLine() )!= null)
- {
- System.out.println(line);
- }
- }
- private static void certificate_and_verification() {
- /*Certificate testing*/
- TrustManager[] trustAllCerts = new TrustManager[]{
- new X509TrustManager() {
- public java.security.cert.X509Certificate[] getAcceptedIssuers()
- {
- return null;
- }
- public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
- {
- //No need to implement.
- }
- public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
- {
- //No need to implement.
- }
- }
- };
- // Install the all-trusting trust manager
- try
- {
- SSLContext sc = SSLContext.getInstance("SSL");
- sc.init(null, trustAllCerts, new java.security.SecureRandom());
- HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
- }
- catch (Exception e)
- {
- System.out.println(e);
- }
- //Installing host name verifier
- // Create all-trusting host name verifier
- HostnameVerifier allHostsValid = new HostnameVerifier() {
- @Override
- public boolean verify(String hostname, SSLSession session) {
- return true; }
- };
- // Install the all-trusting host verifier
- HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
- /* Done */
- }
- }
Add Comment
Please, Sign In to add comment