Im trying to connect to http://www.google.com using socket but the result is:
HTTP/1.0 302 Found
Location: http://www.google.com.ph/
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Here’s my Java test code:
Socket sock = new Socket();
try {
sock.connect(new InetSocketAddress("www.google.com", 80));
InputStream in = sock.getInputStream();
OutputStream out = sock.getOutputStream();
out.write(new String("GET /\r\n").getBytes());
byte[] readBuffer = new byte[4096];
while(true) {
int readSize = in.read(readBuffer);
if(readSize < 1) break;
System.out.println(new String(readBuffer, 0, readSize));
}
sock.close();
} catch(Exception e) {
}
What’s wrong with this? Btw, I need to achieve this using only sockets. tnx
www.google.comwill redirect you based on your IP’s location. In your case, in the Philippines, you will be redirected towww.google.com.ph. Try to request that domain directly instead.You need to specify the HTTP version (in this case 1.1) and along with that, the Host header field (required by that version).
Your request doesn’t specify the version, so it used 1.0.