andriod 鏈接wcf ,HttpURLConnection FileNotFoundException

https://stackoverflow.com/questions/17991347/java-eofexception-when-getinputstream-from-post/18151239#18151239java

If you use服務器

conn.getInputStream()app

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 inide

conn.getErrorStream()post

Thus, you have to check the HTTP response code before you decide which stream to read from:ui

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);
}

這樣就能看到錯誤信息啦。this

或者在服務器端看WCF的報錯來看錯誤信息。spa

<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>code

 

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------xml

 

setDoInput和setDoOutput的含義

 

  1. public void setDoInput(boolean doinput)將此 URLConnection 的 doInput 字段的值設置爲指定的值。    
  2. URL 鏈接可用於輸入和/或輸出。若是打算使用 URL 鏈接進行輸入,則將 DoInput 標誌設置爲 true;若是不打算使用,則設置爲 false。默認值爲 true。  
  3. public void setDoOutput(boolean dooutput)將此 URLConnection 的 doOutput 字段的值設置爲指定的值。    
  4. URL 鏈接可用於輸入和/或輸出。若是打算使用 URL 鏈接進行輸出,則將 DoOutput 標誌設置爲 true;若是不打算使用,則設置爲 false。默認值爲 false。    
 
 
  1. httpUrlConnection.setDoOutput(true);之後就能夠使用conn.getOutputStream().write()  
  2. httpUrlConnection.setDoInput(true);之後就能夠使用conn.getInputStream().read();  
  3.   
  4. get請求用不到conn.getOutputStream(),由於參數直接追加在地址後面,所以默認是false。  
  5. post請求(好比:文件上傳)須要往服務區傳輸大量的數據,這些數據是放在http的body裏面的,所以須要在創建鏈接之後,往服務端寫數據。  
  6.   
  7. 由於老是使用conn.getInputStream()獲取服務端的響應,所以默認值是true。  
相關文章
相關標籤/搜索