我无法理解http请求的响应

时间:2019-04-06 00:29:56

标签: java http

为什么我的代码会出现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)

2 个答案:

答案 0 :(得分:3)

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);

答案 1 :(得分:1)

HTTP动词应该区分大小写。

这里是指向HTTP/1.1 specification的链接。

使用“获取”而不是“获取”。