Taimour
Taimour

Reputation: 459

Unable to fetch .aspx page with Java sockets

When I fetch any html page with Java scoket it works fine but when I fetch any .aspx page with the same code, it doesn't work. I am posting both codes and their sample output below. I have to do it using Java Socket only.How can I fix it to fetch the webpage having .aspx ????

Code to fetch 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(); 
} 
} 
}

Output of fetching 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!!!

Code to fetch 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(); 
} 
} 
}

Output of fetching 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!!!

Upvotes: 1

Views: 101

Answers (1)

Margaret Bloom
Margaret Bloom

Reputation: 44066

HTTP has stop being so simple a long time ago.

I'm not an expert on IIS but evidently, making an HTTP 1.0 request is not handled the same as an HTTP 1.1 one.

Also, your faculty site is what, in Apache language, is called a Virtual Host (See this for IIS), so it need the Host header to correctly identify the destination web site.

This is the minimal working request you have to send

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

Of course, the Host value can (must?) be parametrized to avoid duplicating your self.

Upvotes: 3

Related Questions