简体   繁体   English

从套接字读取后续响应

[英]Reading subsequent response from a socket

I am trying to read bytes into chars from a server which is not maintained by me. 我正在尝试从不由我维护的服务器中将字节读取为字符。 Here am the client. 这是客户。 My issue is am not getting the required response from the request sent to the server. 我的问题是无法从发送到服务器的请求中获得所需的响应。 From my understanding, to detect the end of a message, there are three common ways: 根据我的理解,检测消息的结尾有三种常见方法:

*Closing the connection at the end of the message. *在消息末尾关闭连接。

*Putting the length of the message before the data itself *将消息的长度放在数据本身之前

*Using a separator; *使用分隔符; some value which will never occur in the normal data 一些在正常数据中永远不会出现的值

So this what I have done so far.Am using sockets to achieve writing to the server like this: 所以这是我到目前为止所做的事情。我使用套接字来实现向服务器的写入,如下所示:

Socket outgoing = new Socket(Host, Port);

String request = "GET http://www.firtRequest.com/ HTTP/1.1\r\nHost: www.firstRequest.com\r\n\r\n" + "GET http://www.secondRequest.com/ HTTP/1.1\r\nHost: www.secondRequest.com\r\n\r\n";
outgoing.getOutputStream().write(request.getBytes());
outgoing.getOutputStream().flush();

Using getInputStream() to read from the socket server,I should get two reponses back but the second response carries a html tag which from my understanding isn't part of the resonse so am guessing am not reading till the end of the stream for the first request sent to the server or am not reading the reponses properly altogether. 使用getInputStream()从套接字服务器读取,我应该返回两个响应,但是第二个响应带有一个html标签,据我所知,这不是响应的一部分,所以我猜测直到流的末尾才读取发送到服务器的第一个请求,或者完全没有正确阅读响应。

Output1: 输出1:

HTTP/1.1 200 OK
Date: mon,24 Aug 2015 09:02:30 GMT
Content-Type: application/json
Content-Length: 42

Output2: 输出2:

<!DOCTYPE html PUBLIC "-//W3C"
  ....
<head>
 ....
</head>
<body>
 ...
</body>
   HTTP/1.1 200 OK

Here is my read method in which am trying to detect the end of the stream using "\\r\\n\\r\\n" tag in the reponse or when the stream hits -1. 这是我的读取方法,其中尝试使用响应中的"\\r\\n\\r\\n"标签或当流达到-1时检测流的结尾。

public static String ReadStream(InputStream inputStream) throws IOException {
    StringBuilder builder = new StringBuilder();
    while (true) {
        int rdL = inputStream.read();
        if (rdL == -1) {
            break;
        }
        // Convert the bytes read into characters
        builder.append((char) rdL);
        if (builder.indexOf("\r\n\r\n") != -1) {
            // EOS detected
            break;
        }
    }
    return builder.toString();
}

Any pointers to what am doing wrong to be getting that html tag? 任何获取该html标记在做什么错的指针? Thanks 谢谢

there are three common ways: 共有三种常见方法:

There's only one 'common way' with HTTP, and that is to implement RFC 2616 correctly. HTTP只有一种“通用方式”,那就是正确实现RFC 2616。 You haven't made the slightest attempt here. 您在这里没有做任何尝试。 Look it up. 查一下 But there's no good reason to try to implement HTTP yourself when HttpURLConnection already exists, not to mention numerous third-party HTTP APIs. 但是没有充分的理由尝试在HttpURLConnection已经存在时自己实现HTTP,更不用说众多第三方HTTP API了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM