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。  
相关文章
相关标签/搜索