2016-11-28 8 views
0

정확히 일치하지는 않지만 정확히 this 문제가 발생합니다.입력을 읽은 후에 출력을 쓸 수 없습니다. 이 문제가 발생했으나 확실하지는 않음

나는 로그에서 Cannot write output after reading input을 발견했으며, 위의 설명에 따르면 getResponseCode() 다음에 getOutputStream()이 있기 때문에 발생한다고 생각합니다.

이것이 내가 기록한 오류의 원인일까요?

HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

if(conn.getResponseCode() == 0){ 
    logger.debug("Success"); 
} else {     
    logger.debug("Time out set for 30 seconds"); 
} 
String input = writer.getBuffer().toString(); 
OutputStream os = conn.getOutputStream(); 
os.write(input.getBytes()); 
+0

코드는 어디에 입력 읽은 후 출력을 쓸 수 없습니다? 이 오류가 발생하는 줄 주변에 스 니펫을 게시 할 수 있습니까? –

답변

0

StringWriter writer = new StringWriter(); 

String pushURL = "Your URL"; 

URL url = new URL(pushURL); 

HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

conn.setDoOutput(true); 

conn.setRequestMethod("POST"); 

conn.setRequestProperty("Content-Type", "application/restService"); 

conn.setConnectTimeout(30000); 

conn.setReadTimeout(30000); 

if(conn.getResponseCode() == 0){ 
    logger.debug("Success"); 
} else {     
logger.debug("Time out set for 30 seconds"); 
} 

String input = writer.getBuffer().toString(); 

OutputStream os = conn.getOutputStream(); 

os.write(input.getBytes()); 

os.flush(); 

때문에 here 위치한 다음의 java.net.ProtocolException 발생합니다 다음 코드 :

HTTP 프로토콜은 요청 - 응답 패턴을 기반으로합니다 : 당신이 보내 당신의 먼저 요청하고 서버가 응답합니다. 서버가 응답하면 더 이상 콘텐츠를 보낼 수 없으며 의미가 없습니다. (서버가 당신이 보내려하는 것이 무엇인지 알기 전에 서버가 어떻게 응답 코드를 제공 할 수 있습니까?)

그래서 server.getResponseCode()를 호출하면 요청이 완료되었다는 것을 서버에 효과적으로 알립니다. 그것을 처리 할 수 ​​있습니다. 더 많은 데이터를 보내려면 새 요청을 시작해야합니다.

따라서, 영업 이익은 그의 java.net.ProtocolExceptionException.getMessages() 수익률

을 생성 conn.getResponseCode()conn.getOutputStream();를 호출