我的大學作業是使用 TCP 套接字和 HTTPGET請求通過 URL 從任何 Web 服務器獲取網頁。
我沒有收到HTTP/1.0 200 OK任何服務器的回應。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.util.Scanner;
import java.net.*;
public class DCCN042 {
public static void main(String[] args) {
Scanner inpt = new Scanner(System.in);
System.out.print("Enter URL: ");
String url = inpt.next();
TCPConnect(url);
}
public static void TCPConnect(String url) {
try {
String hostname = new URL(url).getHost();
System.out.println("Loading contents of Server: " hostname);
InetAddress ia = InetAddress.getByName(hostname);
String ip = ia.getHostAddress();
System.out.println(ip " is IP Adress for " hostname);
String path = new URL(url).getPath();
System.out.println("Requested Path on the server: " path);
Socket socket = new Socket(ip, 80);
// Create input and output streams to read from and write to the server
PrintStream out = new PrintStream(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// Follow the HTTP protocol of GET <path> HTTP/1.0 followed by an empty line
if (hostname ! = url) {
//Request Line
out.println("GET " path " HTTP/1.1");
out.println("Host: " hostname);
//Header Lines
out.println("User-Agent: Java/13.0.2");
out.println("Accept-Language: en-us");
out.println("Accept: */*");
out.println("Connection: keep-alive");
out.println("Accept-Encoding: gzip, deflate, br");
// Blank Line
out.println();
} else {
//Request Line
out.println("GET / HTTP/1.0");
out.println("Host: " hostname);
//Header Lines
out.println("User-Agent: Java/13.0.2");
out.println("Accept-Language: en-us");
out.println("Accept: */*");
out.println("Connection: keep-alive");
out.println("Accept-Encoding: gzip, deflate, br");
// Blank Line
out.println();
}
// Read data from the server until we finish reading the document
String line = in.readLine();
while (line != null) {
System.out.println(line);
line = in.readLine();
}
// Close our streams
in.close();
out.close();
socket.close();
} catch (Exception e) {
System.out.println("Invalid URl");
e.printStackTrace();
}
}
}
我創建了一個 TCP 套接字并傳遞我從Web 服務器接收到的 IP 地址InetAddress.getHostAddress()和埠80,并使用getPath()和getHost()將路徑和主機名與 URL 分開,并在 HTTPGET請求中使用相同的路徑和主機名。
來自服務器的回應:
Enter URL: https://stackoverflow.com/questions/33015868/java-simple-http-get-request-using-tcp-sockets
Loading contents of Server: stackoverflow.com
151.101.65.69 is IP Adress for stackoverflow.com
Requested Path on the server: /questions/33015868/java-simple-http-get-request-using-tcp-sockets
HTTP/1.1 301 Moved Permanently
cache-control: no-cache, no-store, must-revalidate
location: https://stackoverflow.com/questions/33015868/java-simple-http-get-request-using-tcp-sockets
x-request-guid: 5f2af765-40c2-49ca-b9a1-daa321373682
feature-policy: microphone 'none'; speaker 'none'
content-security-policy: upgrade-insecure-requests; frame-ancestors 'self' https://stackexchange.com
Accept-Ranges: bytes
Transfer-Encoding: chunked
Date: Mon, 27 Dec 2021 15:00:17 GMT
Via: 1.1 varnish
Connection: keep-alive
X-Served-By: cache-qpg1263-QPG
X-Cache: MISS
X-Cache-Hits: 0
X-Timer: S1640617217.166650,VS0,VE338
Vary: Fastly-SSL
X-DNS-Prefetch-Control: off
Set-Cookie: prov=149aa0ef-a3a6-8001-17c1-128d6d4b7273; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
0
我的要求是獲取此網頁的 HTML 源代碼和HTTP/1.0 200 OK回應。
uj5u.com熱心網友回復:
發生這種情況是因為您使用的是Socket帶有硬編碼 port的純文本80。這意味著,與在輸入中使用 ahttp或httpsurl無關,您是通過不安全協議進行請求的http。
在這種情況下,服務器會告訴您,正如塞繆爾·杰克遜 (Samuel L. Jackson) 會說的那樣:“嘿,mf!您正試圖通過不安全的協議 HTTP 與我聯系。使用安全的 mf,f HTTPS。 ”,等等,它以301回應(這僅表示“使用此 URL,而不是原始 URL”),Location標頭指向正確的 URL,即https。
顯然301 Location是相同的 URL,但事實并非如此,因為在您的代碼中您是硬編碼的http,并且服務器回應重定向到https.
要使您的代碼與 一起使用https,而不是簡單地Socket使用:
SSLSocketFactory factory = (SSLSocketFactory)SSLSocketFactory.getDefault();
SSLSocket socket = (SSLSocket)factory.createSocket(ia, 443);
做筆記,我不是使用ip,因為https你需要的證書對應的域名,如果你使用的IP,你會得到一個CertificateExpiredException。
現在,是使用Socket還是SSLSocket必須根據用戶輸入以編程方式管理的東西。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/395383.html
下一篇:配置自動生成的按鈕以顯示不同的值
