通过Java套接字的HTTPS GET / HTTP / 1.1请求

时间:2017-07-31 11:48:14

标签: java sockets https

我正在尝试使用Java套接字编程请求HTTPS页面。我使用的代码是

PrintWriter s_out = null;

s_out = new PrintWriter(socketData.getOutputStream(), true);

String message = "GET / HTTP/1.1\r\nHost:stackoverflow.com\r\n";

s_out.println(message);

回应是:

HTTP/1.1301MovedPermanentlyLocation:https://stackoverflow.com/X-Request-Guid:b49f01e7-8c3f-4276-b9fe-e325684f0f88Content-Length:143Accept-Ranges:bytesDate:Mon,31Jul201707:15:48GMTVia:1.1varnishConnection:keep-aliveX-Served-By:cache-sin18033-SINX-Cache:MISSX-Cache-Hits:0X-Timer:S1501485348.066340,VS0,VE366Vary:Fastly-SSLX-DNS-Prefetch-Control:offSet-Cookie:prov=929f70a3-e93d-2948-7098-74624f7c4e3c;domain=.stackoverflow.com;expires=Fri,01-Jan-205500:00:00GMT;path=/;HttpOnly<html><head><title>Objectmoved</title></head><body><h2>Objectmovedto<ahref="https://stackoverflow.com/">here</a>.</h2></body></html>

我不知道要求的语法

GET / HTTP/1.1\r\nHost:stackoverflow.com\r\n中的

https://stackoverflow.com

您能否建议调用https页面的语法是什么?

感谢。

1 个答案:

答案 0 :(得分:1)

请求看起来“非常正确”,但似乎正在使用不安全的连接而不是安全连接。

基于连接的实现

首先,请考虑使用更高级别的抽象,例如HttpsURLConnection (Java Platform SE 8)类。

请查看useHttpsConnection()方法的实现。

基于套接字的实现

要建立HTTPS连接SSLSocket (Java Platform SE 8)  class应该间接使用:可以使用SSLSocketFactory (Java Platform SE 8)类创建实例。

请查看useSslSocket()方法的实现。

草案源代码(Java 8)

package com.brunov.test.console;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import javax.net.SocketFactory;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;

public class Program {
    public static void main(final String[] args) throws IOException {
        useHttpsConnection();
        useSslSocket();
    }

    /**
     * Connection-based implementation.
     */
    private static void useHttpsConnection() throws IOException {
        final URL url = new URL("https://stackoverflow.com/");
        final HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        connection.setRequestMethod("GET");

        try (final InputStream inputStream = connection.getInputStream()) {
            final String response = readAsString(inputStream);
            System.out.println(response);
        }
    }

    /**
     * Socket-based implementation.
     */
    private static void useSslSocket() throws IOException {
        final SocketFactory socketFactory = SSLSocketFactory.getDefault();
        try (final Socket socket = socketFactory.createSocket("stackoverflow.com", 443)) {
            final String request = "GET / HTTP/1.1\r\nConnection: close\r\nHost:stackoverflow.com\r\n\r\n";

            final OutputStream outputStream = socket.getOutputStream();
            outputStream.write(request.getBytes(StandardCharsets.US_ASCII));

            final InputStream inputStream = socket.getInputStream();
            final String response = readAsString(inputStream);
            System.out.println(response);
        }
    }

    /**
     * Helper method.
     * Read a stream to the end into a string.
     */
    private static String readAsString(final InputStream inputStream) throws IOException {
        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        final byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, bytesRead);
        }
        return outputStream.toString(StandardCharsets.UTF_8.name());
    }
}

希望这有帮助。