andriod 连接wcf ,HttpURLConnection FileNotFoundException

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. get请求用不到conn.getOutputStream(),因为参数直接追加在地址后面,因此默认是false。
  4. post请求(比如:文件上传)需要往服务区传输大量的数据,这些数据是放在http的body里面的,因此需要在建立连接以后,往服务端写数据。
  5. 因为总是使用conn.getInputStream()获取服务端的响应,因此默认值是true。
时间: 2024-08-05 10:53:12

andriod 连接wcf ,HttpURLConnection FileNotFoundException的相关文章

Java网络连接之HttpURLConnection、HttpsURLConnection

工具类包含两个方法: http请求.https请求 直接看代码: package com.jtools; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.ConnectException; import java.net.HttpURLConnection; import

Android问题-DelphiXE5开发Andriod连接Webservice乱码问题

问题现象:在使用DelphiXE5开发Andriod连接Webservice乱码. 问题原因:数据类型不同. 问题处理:为了不让广大朋友再烦恼,我就把解决办法写出来吧,把数据库中我们要查询的字段类型改成nvarchar就行了.

Android网络连接之HttpURLConnection和HttpClient

1.概念   HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.在 JDK 的 java.net 包中已经提供了访问 HTTP 协议的基本功能:HttpURLConnection.但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活. 除此之外,在Android中,androidSDK中集成了Apache的HttpClient模块,用来提供高效的.最新的.功能丰富的支持 HTTP 协

zz 试图将 JSON 发送到.NET web 服务使用 HttpURLConnection FileNotFoundException

http://www.itstrike.cn/Question/29e46085-7fd4-4deb-afd7-4572ed5d591a.html https://stackoverflow.com/questions/20987525/filenotfoundexception-trying-to-send-json-to-net-webservice-using-httpurlconnec I am trying to parse a response from an InputStream

Android Studio通过Ksoap2连接WCF

<环境> Android Studio:1.1 Visual Studio:2013 Ksoap2:3.4 运行:Server2012R2 + IIS8.0 + xiaomi4.4.2 <目的> 通过VS建立一个简单的WCF服务HelloService,并提供服务方法SayHello,AS中通过Ksoap2连接到该服务并调用SayHello方法,并得到返回数据. 参考: http://blog.sina.com.cn/s/blog_87131d9a0101rmwg.html htt

android 连接 wcf rest注意点

1.datacontract 的元素要有默认值,null 值序列化为json时会有问题,异常并不会在调试中跳出,只是tcp tracer中不会response信息,然后 android 客户端报System.EOF错误 [DataContract] public class UserData { private long userId = (long)0; private string username = string.Empty; private string nickName = stri

Andirod&mdash;&mdash;网络连接(HttpURLConnection)

Android中使用HTTP协议访问网络的方法主要分为两种: 使用HttpURLConnection 使用HttpClient 本文主要内容是HttpURLConnection的使用. HttpURLConnection的使用流程大致分为以下几步: 1. 获取HttpURLConnection对象conn 2. 设置conn的相关属性: a) setRequestMethod--GET/POST b) setConnectTimeout c) setReadTimeout 3. 获取输入流或者设

连接WCF报EntityFramework.SqlServer 错误的解决方法

现象: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. M

JAVA HTTP连接(HttpURLConnection)中使用代理(Proxy)及其验证(Authentication)

public static void main(String[] args) { // TODO Auto-generated method stub try { URL url = new URL("http://www.baidu.com"); // 创建代理服务器 InetSocketAddress addr = new InetSocketAddress("172.21.1.8",80); Proxy proxy = new Proxy(Proxy.Type