gpt4 book ai didi

java - 无法使用 Java 套接字获取 .aspx 页面

转载 作者:行者123 更新时间:2023-11-29 04:49:27 25 4
gpt4 key购买 nike

当我使用 Java scoket 获取任何 html 页面时,它工作正常,但是当我使用相同的代码获取任何 .aspx 页面时,它不起作用。我在下面发布了代码及其示例输出。我必须只使用 Java Socket 来完成它。如何修复它以获取具有 .aspx 的网页????

获取 www.google.com/index.html 的代码

import  java.net.*; 
import java.io.*;
import java.util.*;
class ASD {
public static void main(String[] args) throws Exception {
try {
Socket socket = new Socket("www.google.com",80);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
out.println("GET /index.html HTTP/1.0\r\n\r\n");
out.println();
out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inputLine;
int count = 0;
while ((inputLine = in.readLine()) != null) {
count++;
System.out.println(count);
System.out.println(inputLine);
}
in.close();
System.out.println("PRINTING HERE!!!");
} catch (Exception e) {
e.printStackTrace();
}
}
}

获取 www.google.com/index.html 的输出

1
HTTP/1.0 302 Found
2
Location: http://www.google.com.pk/?gws_rd=cr&ei=127lVpLELIPmuQSjgbLAAw
3
Cache-Control: private
4
Content-Type: text/html; charset=UTF-8
5
P3P: CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info."
6
Date: Sun, 13 Mar 2016 13:44:55 GMT
7
Server: gws
8
Content-Length: 262
9
X-XSS-Protection: 1; mode=block
10
X-Frame-Options: SAMEORIGIN
11
Set-Cookie: NID=77=XaaOVLXLNU5jxAljCoPSDpSp-J9mW6MXGtpIvp9vtftaGfNBqz5oWW03SIO0FSDb3eNgAWoDdXI3NbrZVoui_djlaa3zdT1ekB7szd6rDgNw-J6DeRcgmZ_N_h4uBwKc; expires=Mon, 12-Sep-2016 13:44:55 GMT; path=/; domain=.google.com; HttpOnly
12

13
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
14
<TITLE>302 Moved</TITLE></HEAD><BODY>
15
<H1>302 Moved</H1>
16
The document has moved
17
<A HREF="http://www.google.com.pk/?gws_rd=cr&amp;ei=127lVpLELIPmuQSjgbLAAw">here</A>.
18
</BODY></HTML>
PRINTING HERE!!!

获取 sst.umt.edu.pk/Faculty.aspx 的代码

import  java.net.*; 
import java.io.*;
import java.util.*;
class ASD {
public static void main(String[] args) throws Exception {
try {
Socket socket = new Socket("sst.umt.edu.pk",80);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
out.println("GET /Faculty.aspx HTTP/1.0\r\n\r\n");
out.println();
out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inputLine;
int count = 0;
while ((inputLine = in.readLine()) != null) {
count++;
System.out.println(count);
System.out.println(inputLine);
}
in.close();
System.out.println("PRINTING HERE!!!");
} catch (Exception e) {
e.printStackTrace();
}
}
}

获取 sst.umt.edu.pk/Faculty.aspx 的输出

1
HTTP/1.1 404 Not Found
2
Content-Type: text/html; charset=us-ascii
3
Server: Microsoft-HTTPAPI/2.0
4
Date: Sun, 13 Mar 2016 13:43:25 GMT
5
Connection: close
6
Content-Length: 315
7

8
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
9
<HTML><HEAD><TITLE>Not Found</TITLE>
10
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
11
<BODY><h2>Not Found</h2>
12
<hr><p>HTTP Error 404. The requested resource is not found.</p>
13
</BODY></HTML>
PRINTING HERE!!!

最佳答案

很久以前 HTTP 就不再那么简单了。

我不是 IIS 方面的专家,但显然,发出 HTTP 1.0 请求的处理方式与 HTTP 1.1 请求的处理方式不同.

此外,您的教职员工站点在 Apache 语言中称为虚拟主机(有关 IIS,请参阅 this),因此它需要 Host header 正确识别目标网站。

这是您必须发送的最小工作请求

out.println("GET /Faculty.aspx  HTTP/1.1"); 
out.println("Host: sst.umt.edu.pk");

当然,可以(必须?)对 Host 值进行参数化以避免复制您自己。

关于java - 无法使用 Java 套接字获取 .aspx 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35971077/

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