大概的意思就是 程序连接中止:接受失败,然后我就开始检查代码,用原生的http
请求也是能正常返回的,浏览器,和crul http://localhost:8801
也是正常返回,那我就感觉很奇怪,为什么借助httpClient
就会失败呢?
网上查了下这个报错大概意思就直接意思: 客户端和服务端建立tcp的短连接,每次客户端发送一次请求, 服务端响应后关闭与客户端的连接. 如果客户端在服务端关闭连接后,没有释放连接,继续试图发送请求和接收响应. 这个时候就会出错.
于是我开始想,会不会是服务端关闭太快导致客户端正在读的时候,服务端已经关闭了,导致报错。果断在关闭的时候阻塞线程一秒试试Thread.sleep(1000)
,就解决了。 所以这个问题报错的原因就是如上面红色字体描述的那样,在我这里就是服务端关闭太快了。
示例代码:
服务端代码
package org.geekbang.netty.server;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class HttpServer01 {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8801);
while (true) {
try {
Socket socket = serverSocket.accept();
service(socket);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void service(Socket socket) {
try {
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
printWriter.println("HTTP/1.1 200 OK");
printWriter.println("Content-Type:text/html;charset=utf-8");
printWriter.println("Connection: keep-alive");
String body = "hello,nio1";
printWriter.println("Content-Length: " + body.getBytes().length);
printWriter.println();
printWriter.write(body);
printWriter.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
客户端代码:
package org.geekbang.netty.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class HttpClientHelper {
public static CloseableHttpClient httpclient = HttpClients.createDefault();
public static String getAsString(String url) throws IOException {
HttpGet httpGet = new HttpGet(url);
try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
System.out.println("see see what get this: " + response.getStatusLine());
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, "UTF-8");
}
}
public static String httpRequest(String requestUrl, String requestMethod, String outputStr) {
StringBuffer buffer = null;
try {
URL url = new URL(requestUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod(requestMethod);
conn.connect();
if (null != outputStr) {
OutputStream os = conn.getOutputStream();
os.write(outputStr.getBytes("utf-8"));
os.close();
}
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
buffer = new StringBuffer();
String line = null;
while ((line = br.readLine()) != null) {
buffer.append(line);
}
} catch (Exception e) {
e.printStackTrace();
}
return buffer.toString();
}
public static void main(String[] args) throws IOException {
String url = "http://www.baidu.com";
String url1 = "http://localhost:8801";
String resultString1 = HttpClientHelper.getAsString(url1);
log.info("get调用8801返回结果:{}", resultString1);
}
}