java.io.IOException: Premature EOF

 

http訪問第三方系統的接口時,小几率拋出下面的異常:java

java.io.IOException: Premature EOF
at sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:565)
at com.xxxxx.util.HttpUtil.getHttpsContent(HttpUtil.java:194)web

相關代碼以下:json

public static JSONObject getMsgRetry(String url, JSONObject json) throws IOException{
		url = url + "?a=1";
		for(Entry<String, Object> entry : json.entrySet()){
			String key = entry.getKey();
			Object value = entry.getValue();
			url = url+"&"+key+"="+value;
		}
		HttpsURLConnection conn = HttpUtil.initHttpsConnectionKeepAlive(url, "GET");
		String result = "";
		try{
			result = HttpUtil.getHttpsContent(conn, "utf-8");
		}catch(Exception e){
			logger.error("interface_error, let's retry.");
			logger.error(e.toString());
			try {
				TimeUnit.SECONDS.sleep(2); // 延遲2秒,再重試 
			} catch (InterruptedException e1) {
				logger.error(e1.toString());
			}
			result = HttpUtil.getHttpsContent(conn, "utf-8");
		}finally{
			conn.disconnect();	
		}
		return JSONObject.parseObject(result);
	}

 

public static String getHttpsContent(HttpsURLConnection conn,
			String characterCode) throws IOException {
		InputStream inputStream = conn.getInputStream();
		InputStreamReader inputStreamReader = new InputStreamReader(
				inputStream, characterCode);
		BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
		String str = null;
		StringBuffer buffer = new StringBuffer();
		while ((str = bufferedReader.readLine()) != null) {
			buffer.append(str);
		}
		// 釋放資源
		bufferedReader.close();
		inputStreamReader.close();
		inputStream.close();
		conn.disconnect();

		return buffer.toString();
	}

 

上面的代碼中:app

while ((str = bufferedReader.readLine()) != null) {
			buffer.append(str);
		}

  

while語句有時會拋出異常:url

java.io.IOException: Premature EOF.net

at sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:565)code

搜索發現,這個是廣泛性的一個問題,解決方法:server

https://stackoverflow.com/questions/13210108/reading-a-web-page-in-java-ioexception-premature-eofblog

代碼以下修改:接口

public static String getHttpsContent(HttpsURLConnection conn, String characterCode) throws IOException {
		InputStream inputStream = conn.getInputStream();
		InputStreamReader inputStreamReader = new InputStreamReader(
				inputStream, characterCode);
		BufferedReader bufferedReader = new BufferedReader(
				inputStreamReader);
		// fix bug:  java.io.IOException: Premature EOF
		//        at sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:565)
		// https://stackoverflow.com/questions/13210108/reading-a-web-page-in-java-ioexception-premature-eof
		StringBuffer sb = new StringBuffer();
		int BUFFER_SIZE = 1024;
		char[] buffer = new char[BUFFER_SIZE]; // or some other size, 
		int charsRead = 0;
		while ( (charsRead  = bufferedReader.read(buffer, 0, BUFFER_SIZE)) != -1) {
			sb.append(buffer, 0, charsRead);
		}

		return sb.toString();
	}

搞定。緣由是第三方接口可能沒有發送http協議須要的結束行。

 The issue for you it may be that the server is not pushing that last end line character

相關文章
相關標籤/搜索