1

I want to send GET message at my Android application. After I want to receive GET response as 200 OK. But I didn't accomplish. I received 408 Request Time-outDate or nothing. Can you help me?

String requestmsg = "GET / HTTP/1.1\r\n";
requestmsg += "Host: www.ktu.edu.tr\r\n";
requestmsg += "Connection: keep-alive\r\n";
requestmsg += "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n";
requestmsg += "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW 64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36\r\n";
requestmsg += "Accept-Encoding: gzip, deflate, sdch\r\n";
requestmsg += "Accept-Language: en-US,en;q=0.8,en-GB;q=0.6\r\n";


DataOutputStream dos = null;
BufferedReader dis = null;
try {

    Log.d("ClientActivity", "Connecting...");
    String addr = InetAddress.getByName("www.ktu.edu.tr").getHostAddress().toString();
    Socket socket = new Socket(addr, 80);
    String data = "";

    try {
        Log.d("ClientActivity", "C: Sending command.");

        dos = new DataOutputStream(socket.getOutputStream());
        dis = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        dos.write(requestmsg.getBytes());
        Log.i("ClientActivity", "RequestMsg Sent");

        StringBuilder sb = new StringBuilder();

        while ((data = dis.readLine()) != null) {
            sb.append(data);
        }
        Log.i("ClientActivity", "C: Sent.");
        Log.i("ClientActivity", "C: Received " + sb.toString());
    } catch (Exception e) {
        Log.e("ClientActivity", "S: Error", e);

    }

    socket.close();
    Log.d("ClientActivity", "C: Closed.");
} catch (Exception e) {
    Log.e("ClientActivity", "C: Error", e);
}
FatihCeng
  • 37
  • 7

1 Answers1

0
  • You're missing a Content-Length header
  • You're missing a blank line at the end of the request headers
  • You're not reading the response correctly according to its content-length or chunking either.

In short, don't implement HTTP yourself. Use a URL and HttpURLConnection. It takes care of all these details and many more.

user207421
  • 305,947
  • 44
  • 307
  • 483