gpt4 book ai didi

java - 我无法理解 http 请求的响应

转载 作者:行者123 更新时间:2023-12-01 17:45:44 25 4
gpt4 key购买 nike

为什么它给我一个 405 错误,尽管代码应该在逻辑上工作并且链接有一个有效的下载我从互联网上复制了一些工作代码,看看发生了什么,就像这个主题一样 How to download an image with a Java socket HTTP/1.1 request?而且它也没有给我回应 200

抱歉,代码很乱,我花了大约 2 天的时间试图找出问题

//package htmlconnection;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import javax.net.SocketFactory;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
public class MyClass {
public static void main(String args[])throws UnknownHostException, IOException {
try {
URL url =new URL("http://www.ncert.nic.in/NCERTS/l/jemh1an.pdf");
String path=url.getPath();
String domain=url.getHost();
System.out.println(path);
System.out.println(domain);
Socket socket = new Socket(domain,80);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
System.out.println(socket.isConnected());
out.println("Get "+path+" HTTP/1.1\n" +"Host: "+domain);
out.println();
out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null && inputLine.trim() != "0") {
System.out.println(inputLine);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

//http://www.ncert.nic.in/NCERTS/l/jemh1an.pdf
//http://www.peoplelikeus.org/piccies/codpaste/codpaste-teachingpack.pdf

结果是

/NCERTS/l/jemh1an.pdf
www.ncert.nic.in
true
HTTP/1.1 405 Method Not Allowed
Allow: GET, HEAD, OPTIONS, TRACE
Content-Type: text/html
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET
X-Xss-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
Date: Sat, 06 Apr 2019 00:19:13 GMT
Content-Length: 1293

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>405 - HTTP verb used to access this page is not allowed.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;}
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;}
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;}
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
<div class="content-container"><fieldset>
<h2>405 - HTTP verb used to access this page is not allowed.</h2>
<h3>The page you are looking for cannot be displayed because an invalid method (HTTP verb) was used to attempt access.</h3>
</fieldset></div>
</div>
</body>
</html>
Exception in thread "main" java.net.SocketException: Connection reset
at java.base/java.net.SocketInputStream.read(SocketInputStream.java:186)
at java.base/java.net.SocketInputStream.read(SocketInputStream.java:140)
at java.base/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.base/java.io.InputStreamReader.read(InputStreamReader.java:185)
at java.base/java.io.BufferedReader.fill(BufferedReader.java:161)
at java.base/java.io.BufferedReader.readLine(BufferedReader.java:326)
at java.base/java.io.BufferedReader.readLine(BufferedReader.java:392)
at htmlconnection.Download.main(Download.java:39)

最佳答案

GET 请求必须全部大写。您可以看到收到 405,这意味着此处不接受 HTTP 请求方法:

<title>405 - HTTP verb used to access this page is not allowed.</title>

更改此:

            out.println("Get "+path+" HTTP/1.1\n" +"Host: "+domain);

对此:

            out.println("GET "+path+" HTTP/1.1\n" +"Host: "+domain);

关于java - 我无法理解 http 请求的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55544816/

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