andriod 串連wcf ,HttpURLConnection FileNotFoundException

來源:互聯網
上載者:User

標籤:.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的含義

 

  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。  

 

andriod 串連wcf ,HttpURLConnection FileNotFoundException

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.