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

Unable to fetch .aspx page with Java sockets

当我使用 Java scoket 获取任何 html 页面时,它工作正常,但是当我使用相同代码获取任何 .aspx 页面时,它不起作用。我在下面发布了代码及其示例输出。 我必须使用 Java Socket only.How 来完成它我可以修复它以获取具有 .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 值可以(必须?)参数化以避免复制您自己。