標籤:.com zed case when and pre rom dia out ret
https://stackoverflow.com/questions/17991347/java-eofexception-when-getinputstream-from-post/18151239#18151239
If you use
conn.getInputStream()
everytime, it will throw a java.io.FileNotFoundException
in the case when your request is unsuccessful, basically for any HTTP response code of 400 or above. In this case, your response body lies in
conn.getErrorStream()
Thus, you have to check the HTTP response code before you decide which stream to read from:
int status = conn.getResponseCode();BufferedInputStream in;if (status >= 400 ) { in = new BufferedInputStream( conn.getErrorStream() );} else { in = new BufferedInputStream( conn.getInputStream() );}
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String str;
while ((str = reader.readLine()) != null) {
sb.append(str);
}
這樣就能看到錯誤資訊啦。
或者在伺服器端看WCF的報錯來看錯誤資訊。
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Warning" propagateActivity="true">
<listeners>
<add name="xml" />
</listeners>
</source>
</sources>
<sharedListeners>
<add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="D:\wcf.svclog" />
</sharedListeners>
</system.diagnostics>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
setDoInput和setDoOutput的含義
- public void setDoInput(boolean doinput)將此 URLConnection 的 doInput 欄位的值設定為指定的值。
- URL 串連可用於輸入和/或輸出。如果打算使用 URL 串連進行輸入,則將 DoInput 標誌設定為 true;如果不打算使用,則設定為 false。預設值為 true。
- public void setDoOutput(boolean dooutput)將此 URLConnection 的 doOutput 欄位的值設定為指定的值。
- URL 串連可用於輸入和/或輸出。如果打算使用 URL 串連進行輸出,則將 DoOutput 標誌設定為 true;如果不打算使用,則設定為 false。預設值為 false。
- httpUrlConnection.setDoOutput(true);以後就可以使用conn.getOutputStream().write()
- httpUrlConnection.setDoInput(true);以後就可以使用conn.getInputStream().read();
-
- get請求用不到conn.getOutputStream(),因為參數直接追加在地址後面,因此預設是false。
- post請求(比如:檔案上傳)需要往服務區傳輸大量的資料,這些資料是放在http的body裡面的,因此需要在建立串連以後,往服務端寫資料。
-
- 因為總是使用conn.getInputStream()擷取服務端的響應,因此預設值是true。
andriod 串連wcf ,HttpURLConnection FileNotFoundException