andriod 连接wcf ,HttpURLConnection FileNotFoundException
2017-07-25 23:09
363 查看
https://stackoverflow.com/questions/17991347/java-eofexception-when-getinputstream-from-post/18151239#18151239
If you use
everytime, it will throw a
Thus, you have to check the HTTP response code before you decide which stream to read from:
这样就能看到错误信息啦。
或者在服务器端看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。
If you use
conn.getInputStream()
everytime, it will throw a
java.io.FileNotFoundExceptionin 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。
相关文章推荐
- HttpURLConnection.getInputStream java.io.FileNotFoundException
- 使用HttpURLConnection下载文件时出现 java.io.FileNotFoundException彻底解决办法
- android4.0中HttpURLConnection抛异常java.io.FileNotFoundException
- HttpURLConnection抛异常java.io.FileNotFoundException
- Android4.0以上HttpURLConnection出FileNotFoundException异常解决
- HTTPUrlConnection 出现 FileNotFoundException 的问题
- 使用HttpURLConnection下载文件时出现 java.io.FileNotFoundException彻底解决办法
- 关于HttpURLConnection遇到ava.io.FileNotFoundException的解决方法
- 使用HttpURLConnection下载文件时出现 java.io.FileNotFoundException彻底解决办法
- SPSite site = new SPSite(url); 连接site的时候遇到"FileNotFoundException" 异常的解决方法
- 解决java java.io.FileNotFoundException: http:\localhost:8080 (文件名、目录名或卷)问题
- J2EE进阶(十九)FileNotFoundException: http://hibernate.org/dtd/hibernate-mapping-3.0.dtd
- android开源项目SmartImageView设置网络图片url时抛出java.io.FileNotFoundException
- struts2 java.io.FileNotFoundException: http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd
- tomcat下载文件时,URL含有文字参数与空格参数出错(java.io.FileNotFoundException错误) 解决方法(转)
- java.io.FileNotFoundException: http://www.opensymphony.com/xwork/xwork-valid
- java.io.FileNotFoundException: http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd问题终结原因
- java.net.URL处理中文URL,导致java.io.FileNotFoundException的解决!
- java.io.FileNotFoundException: URL [jndi:/localhost/myBlogSys/WEB-INF/classes/] cannot be resolved t
- java.io.FileNotFoundException: url(拒绝访问。)问题