gpt4 book ai didi

java - 正确的 HTTP GET 请求

转载 作者:行者123 更新时间:2023-12-01 13:43:09 24 4
gpt4 key购买 nike

除了我知道 HTTP GET 请求要求从服务器读取网页这一事实之外,我在理解 HTTP GET 请求的概念时遇到了一些困难。今天我编写了一个类,尝试使用 HTTP GET 请求来访问网页上的 html Material 。让我包括类(class)并解释我的困惑:

    import java.io.*;
import java.net.*;

public class HTMLFetcher
{
private static final int PORT = 80;
private URL url;


public HTMLFetcher(String url) throws Exception // url = http://www.-----.com/birds.html
{
this.url = new URL(url);
fetch(this.url.getHost());
}

private String createRequest(URL url) { // Is there a problem with this request?
String request = "GET" + "/index.html" + "HTTP/1.1\n";
request += "Host: www.cs.usfca.edu\n";
request += "Connection: close";
request += "\r\n";
return request;
}

public void fetch(String urlDomain) throws Exception {

System.out.println(urlDomain + ":" + PORT);

// TODO: create a new socket here for a given urlDomain and a given PORT
Socket socket = new Socket(urlDomain, PORT);

// TODO: create PrintWriter for the socket's output stream
PrintWriter writer =
new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));

BufferedReader reader =
new BufferedReader(new InputStreamReader(socket.getInputStream()));

String request = createRequest(urlDomain); // createRequest is complaining that it is a string and not a URL
System.out.println(request);
writer.write(request);
writer.flush();

StringBuilder string = new StringBuilder();
boolean htmlFound = false;
String line;
while ((line = reader.readLine()) != null) {
if (!htmlFound) {
if (line.toLowerCase().startsWith("<html>")) {
htmlFound = true;
} else {
continue;
}
}
System.out.println("This is each line: " + line);
string.append(line + "\n");
}

reader.close();
writer.close();
socket.close();

//System.out.println(string.toString());
System.out.println("[done]");
}
}

所以基本上我很困惑,当 createRequest 方法需要 URL 时,如何将 String urlDomain 发送到该方法中? createMethod 参数对于 HTTP 请求是必需的吗?我是否正确设置了请求?

现在它输出以下内容:

www.cs.usfca.edu:80
GET/index.htmlHTTP/1.1
Host: www.cs.usfca.edu
Connection: close

This is each line: <html><head>
This is each line: <title>501 Method Not Implemented</title>
This is each line: </head><body>
This is each line: <h1>Method Not Implemented</h1>
This is each line: <p>GET/index.htmlHTTP/1.1 to /index.html not supported.<br />
This is each line: </p>
This is each line: <hr>
This is each line: <address>Apache/2.2.15 (CentOS) Server at www.cs.usfca.edu Port 80</address>
This is each line: </body></html>
[done]

感谢您的帮助。如果我可以更具体,请告诉我。谢谢。

最佳答案

据我了解,当网站位于共享托管服务器上时,将使用请求中的主机 header ,其中多个域将映射到同一 IP,并且服务器需要 Host header 来识别请求路由到的虚拟服务器。因此,最好将其包含在请求中。

顺便说一句,在当前代码中,请求字符串中没有空格。这就是为什么您收到错误 html 作为响应。

private String createRequest(String url) { // Is there a problem with this request? 
String request = "GET " + "/ " + "HTTP/1.1\r\n";
request += "Host: www.cs.usfca.edu\n";
request += "\r\n";
return request;
}

另外,不要这样检查

if (line.toLowerCase().startsWith("<html>")) 

改为使用

if (line.toLowerCase().startsWith("<html")) 

顺便说一句,为什么你一定要这么艰难呢?改用 HTTPUrlConnection。

关于java - 正确的 HTTP GET 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20536017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com