java read防止挂起,Java,套接字,BufferedReader和readline挂起...... :(

I'm not a Java programmer at all. I try to avoid it at all costs actually, but it is required that I use it for a class (in the school sense). The teacher requires that we use Socket(), BufferedReader(), PrintWriter() and various other things including BufferedReader()'s readLine() method.

Basically, this is the problem I'm having. The documentation clearly states that readLine should return a null at the end of the input stream, but that's not what's happening.

Socket link = new Socket(this.address, 80);

BufferedReader in = new BufferedReader( new InputStreamReader( link.getInputStream() ));

PrintWriter out = new PrintWriter( new PrintWriter( link.getOutputStream(), true ));

out.print("GET blah blah blah"); // http request by hand

out.flush(); // send the get please

while( (s=in.readLine()) != null ) {

// prints the html correctly, hooray!!

System.out.println(s);

}

Instead of finishing at the end of the HTML, I get a blank line, a 0 and

another blank line and then the next in.readLine() hangs forever. Why?

Where's my null?

I tried out.close() to see if maybe Yahoo! was doing a persistent http

session or something (which I don't think it would without the header that

we're willing to do it).

All the Java sockets examples I'm finding on the net seem to indicate the

while loop is the correct form. I just don't know enough Java to debug

this.

解决方案

Your problem is the content encoding “chunked”. This is used when the length of the content requested from the web server is not known at the time the response is started. It basically consists of the number of bytes being sent, followed by CRLF, followed by the bytes. The end of a response is signalled by the exact sequence you are seeing. The web server is now waiting for your next request (this is also called “request pipelining”).

You have several possibilities:

Use HTTP version 1.0. This will cause the webserver to automatically close the connection when a response has been sent completely.

Specify the “Connection: close” header when sending your request. This will also close the connection.

Parse content encoding “chunked” correctly and simply treat this as if the response is now complete—which it is.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值