+7(960) 250-82-68 info@mirossa.ru


 mirossa        1С           C         PHP       JAVA  


Скр
 
 

http запрос:

//компиляция
//cd "C:\Program Files\Java\jdk1.8.0_151\bin"
//javac.exe SocketClient.java

//java.exe SocketClient


import java.net.*;
import java.io.*;
 
/**
 * This program demonstrates a client socket application that connects to
 * a web server and send a HTTP HEAD request.
 *
 * @author www.codejava.net
 */
public class SocketClient {
 
    public static void main(String[] args) {
        // if (args.length < 1) return;
 
        // URL url;
 
        // try {
            // url = new URL(args[0]);
        // } catch (MalformedURLException ex) {
            // ex.printStackTrace();
            // return;
        // }
 
        // String hostname = url.getHost();
		String hostname = "mirossa.ru";
        int port = 80;
 
        try (Socket socket = new Socket(hostname, port)) {
 
            OutputStream output = socket.getOutputStream();
            PrintWriter writer = new PrintWriter(output, true);
 
            // writer.println("HEAD " + url.getPath() + " HTTP/1.1");
			writer.println("GET / HTTP/1.1");
            writer.println("Host: " + hostname);
            writer.println("User-Agent: Mozilla/5.0 Firefox/65.0");
            writer.println("Accept: text/html");
            writer.println("Accept-Language: en-US");
            writer.println("Connection: close");
            writer.println();
 
            InputStream input = socket.getInputStream();
 
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
 
            String line;
 
            while ((line = reader.readLine()) != null) { // .. Content-Length: 333
                System.out.println(line);
            }
        } catch (UnknownHostException ex) {
 
            System.out.println("Server not found: " + ex.getMessage());
 
        } catch (IOException ex) {
 
            System.out.println("I/O error: " + ex.getMessage());
        }
    }
}

http запрос + SSL:

//компиляция
//cd "C:\Program Files\Java\jdk1.8.0_151\bin"
//javac.exe SSLSocketClient.java

//java.exe SSLSocketClient

import java.net.*;
import java.io.*;
import javax.net.ssl.*;

/*
 * This example demostrates how to use a SSLSocket as client to
 * send a HTTP request and get response from an HTTPS server.
 * It assumes that the client is not behind a firewall
 */

public class SSLSocketClient {

    public static void main(String[] args) throws Exception {
        try {
            SSLSocketFactory factory =
                (SSLSocketFactory)SSLSocketFactory.getDefault();
            SSLSocket socket =
                (SSLSocket)factory.createSocket("domain.ru", 443);

            /*
             * send http request
             *
             * Before any application data is sent or received, the
             * SSL socket will do SSL handshaking first to set up
             * the security attributes.
             *
             * SSL handshaking can be initiated by either flushing data
             * down the pipe, or by starting the handshaking by hand.
             *
             * Handshaking is started manually in this example because
             * PrintWriter catches all IOExceptions (including
             * SSLExceptions), sets an internal error flag, and then
             * returns without rethrowing the exception.
             *
             * Unfortunately, this means any error messages are lost,
             * which caused lots of confusion for others using this
             * code.  The only way to tell there was an error is to call
             * PrintWriter.checkError().
             */
			
            socket.startHandshake();
			
            
			PrintWriter out = new PrintWriter(
                                  new BufferedWriter(
                                  new OutputStreamWriter(
                                  socket.getOutputStream())));

            out.println("GET / HTTP/1.1");
			out.println("Host: domain.ru");
			out.println("User-Agent: Mozilla/5.0 Firefox/65.0");
            out.println();
            out.flush();

            /*
             * Make sure there were no surprises
             */
            if (out.checkError())
                System.out.println(
                    "SSLSocketClient:  java.io.PrintWriter error");

            /* read response */
            BufferedReader in = new BufferedReader(
                                    new InputStreamReader(
                                    socket.getInputStream()));
									
									
			

            String inputLine;
            while ((inputLine = in.readLine()) != null) // .. Content-Length: 333
                System.out.println(inputLine);
				
			
			
            in.close();
            out.close();
            socket.close();
			

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

//author https://docs.oracle.com/javase/10/security/
//sample-code-illustrating-secure-socket-connection-client-and-server.htm
//#JSSEC-GUID-B9103D0C-3E6A-4301-B558-461E4CB23DC9