I was trying to send a get request to a website but always return error 400 bad request. I'm wondering if my get request is wrong or not. Here it is;
socketOut.println("GET " + req.url + " HTTP/1.1\r\n");
socketOut.flush();
I can do it by using URLConnection but as a second way I'm using socket. So I'm sending this request and readinng bytes. I checked what it returns to me from website and of course it returns bad request error page. Any suggestion? By the way req.url returns a url not something like /asd.html but this www.stackoverflow.com/asd.html
println
may use \n
as new line instead of \r\n
which might be required. Try using \r\n
directly.
HTTP 1.1 requires Host
header. Try:
socketOut.print("GET " + req.url + " HTTP/1.0\r\n\r\n");
or:
socketOut.print("GET " + req.url + " HTTP/1.1\r\n");
socketOut.print("Host: " + req.host + "\r\n");
socketOut.print("\r\n");