当我使用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&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),所以它需要的主机头正确识别目标网站。
这是您必须发送的最低工作要求
out.println("GET /Faculty.aspx HTTP/1.1");
out.println("Host: sst.umt.edu.pk");
当然,可以(必须?)对主机值进行参数化设置,以避免重复您的自我。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句