Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么不成功;带套接字的HTTP GET请求;?_Java_Android_Sockets_Http_Get - Fatal编程技术网

Java 为什么不成功;带套接字的HTTP GET请求;?

Java 为什么不成功;带套接字的HTTP GET请求;?,java,android,sockets,http,get,Java,Android,Sockets,Http,Get,我想在我的Android应用程序上发送GET消息。之后我想收到回复为200 OK。但我没有做到。我收到了408个请求超时或什么都没有。你能帮我吗 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,

我想在我的Android应用程序上发送GET消息。之后我想收到回复为200 OK。但我没有做到。我收到了408个请求超时或什么都没有。你能帮我吗

    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);
    }

首先,您忘了在末尾添加另一行
\r\n
来正确完成请求

其次,您正在读取从服务器获得的所有信息,而不确定响应的实际长度

请使用HTTP库,或者通过学习学习正确使用HTTP协议。

(套接字需要一些努力;您可能更喜欢不同的方法。标准的JSE方法是URL.openConnection。)

指定编码,否则它是默认的eNotPortable

new InputStreamReader(socket.getInputStream(), "Windows-1252"));
相反方向也一样:

requestmsg.getBytes("Windows-1252")
这是Windows Latin-1,即使指定了更有限的Latin-1“ISO-8859-1”,浏览器也会接受它。稍后检查编码

发送头最好不要说它准备使用压缩:

//requestmsg += "Accept-Encoding: gzip, deflate, sdch\r\n";
并用空行关闭请求标头:

requestmsg += "\r\n";

BufferedOutputStream比DataOutputStream IMHO更合适。

事实上,正如其他人已经告诉您的那样,您需要一个结尾
requestmsg+=“\r\n”

您最好删除行
requestmsg+=“接受编码:gzip,deflate,sdch\r\n”
as
readLine()
无法处理zip内容

但是您的代码也会导致
SocketTimeoutException
。你应该分开接

如果您访问我的服务器,并且与
String host=“nl3.php.net”一起使用,那么您的代码就可以了