Truman Purnell
Truman Purnell

Reputation: 305

Getting 301 when trying to send GET request with raw sockets

Why am I getting 301 for every URL I try to access? I am just trying to use raw sockets as en exercise! I have listed the reponse below. Stack overflow is making me type more words because my response is mostly code. So here are some more words

    Socket s = new Socket(InetAddress.getByName("stackoverflow.com"), 80);
    PrintWriter pw = new PrintWriter(s.getOutputStream());
    pw.println("GET / HTTP/1.1\r");
    pw.println("Host: stackoverflow.com\r");
    pw.println("\r");

    pw.flush();

    BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
    String t;
    while ((t = br.readLine()) != null)
        System.out.println(t);
    br.close();

HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=utf-8
Location: https://stackoverflow.com/
X-Request-Guid: 5d0b1d72-5b10-4aa2-a563-4fd3d6b155cb
Content-Security-Policy: upgrade-insecure-requests
Content-Length: 143
Accept-Ranges: bytes
Date: Mon, 20 Aug 2018 06:19:15 GMT
Via: 1.1 varnish
Connection: keep-alive
X-Served-By: cache-dfw18637-DFW
X-Cache: MISS
X-Cache-Hits: 0
X-Timer: S1534745955.491886,VS0,VE37
Vary: Fastly-SSL
X-DNS-Prefetch-Control: off
Set-Cookie: prov=0bf5c623-b16b-d4b6-e822-504daf42c16e; 
domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; 
path=/; HttpOnly

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="https://stackoverflow.com/">here</a>.</h2>
</body></html>

Upvotes: 0

Views: 153

Answers (1)

SKi
SKi

Reputation: 8466

The HTTP server want to redirect the client to use HTTPS. Nowadays it is hard to find well known HTTP server, that won't do that.

Try for example "jquery.com", it does not redirect to HTTPS.

BTW: "raw socket" is not correct term for a TCP socket. Usually term 'raw socket' is used for raw IP sockets (SOCK_RAW).

Upvotes: 3

Related Questions