尝试从HttpUrlConnection读取数据时(访问InputStream时)我收到了Java.IO.EOFException 我使用xamarin和OkHttp库,应用程序在Android 4.2.2的genymotion模拟器上运行
以下是示例代码(C#):
// ...
var okHttpClient = CreateHttpClient();
var conn = okHttpClient.Open(new URL(address));
var inputStream = conn.InputStream; //here is the exception
// ...
private OkHttpClient CreateHttpClient()
{
OkHttpClient client = new OkHttpClient();
client.SetProxy(new Proxy(Proxy.Type.Http, new InetSocketAddress(Host, Port)));
client.SetAuthenticator(new MyAuthenticator(User, Password));
return client;
}
private class MyAuthenticator: Java.Lang.Object, IOkAuthenticator
{
private readonly string _user;
private readonly string _password;
public MyAuthenticator(string user, string password)
{
_user = user;
_password = password;
}
public OkAuthenticatorCredential Authenticate(Proxy p0, URL p1, IList<OkAuthenticatorChallenge> p2)
{
return OkAuthenticatorCredential.Basic(null, null); //this is never called
}
public OkAuthenticatorCredential AuthenticateProxy(Proxy p0, URL p1, IList<OkAuthenticatorChallenge> p2)
{
return OkAuthenticatorCredential.Basic(_user, _password);
}
}
当代理未经授权时,一切正常。
当我使用.Net mono WebClient时,它可以使用代理授权(但我有理由不使用单一WebClient,如果可能的话)
解决方法conn.SetRequestProperty(“Connection”,“close”);没有用。
堆栈跟踪:
Download Error: Java.IO.EOFException: Exception of type 'Java.IO.EOFException' was thrown.
at Android.Runtime.JNIEnv.CallObjectMethod (IntPtr jobject, IntPtr jmethod) [0x00063] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.1-branch/d23a19bf/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:177
at Java.Net.URLConnection.get_InputStream () [0x00043] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.1-branch/d23a19bf/source/monodroid/src/Mono.Android/platforms/android-14/src/generated/Java.Net.URLConnection.cs:754
at MachiningCloud.AndroidNet.WebClient.DownloadFile (System.String address, System.String fileName) [0x0002c] in c:\Projects\iDPTech\Main\Source\DPTech.ToolSelector.Ios\MachiningCloud.AndroidNet\WebClient.cs:63
at UrlImageViewHelper.UrlImageViewHelper.DownloadImageToFile (System.String url, System.String filename) [0x00007] in c:\Projects\iDPTech\Main\Source\DPTech.ToolSelector.Ios\MonoDroid.UrlImageViewHelper.Android\UrlImageViewHelper.cs:387
at UrlImageViewHelper.UrlImageViewHelper.RunInBackgroundFunc (UrlImageViewHelper.TaskParams[] taskParams) [0x001c3] in c:\Projects\iDPTech\Main\Source\DPTech.ToolSelector.Ios\MonoDroid.UrlImageViewHelper.Android\UrlImageViewHelper.cs:349
--- End of managed exception stack trace ---
java.io.EOFException
at com.squareup.okhttp.internal.Util.readAsciiLine(Util.java:342)
at com.squareup.okhttp.internal.http.RawHeaders.fromBytes(RawHeaders.java:311)
at com.squareup.okhttp.Connection.makeTunnel(Connection.java:313)
at com.squareup.okhttp.Connection.upgradeToTls(Connection.java:127)
at com.squareup.okhttp.Connection.connect(Connection.java:107)
at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
at com.squareup.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:351)
at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:297)
at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:180)
at com.squareup.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:246)
at urlimageviewhelper.AnonymousAsyncTask_3.n_doInBackground(Native Method)
at urlimageviewhelper.AnonymousAsyncTask_3.doInBackground(AnonymousAsyncTask_3.java:29)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
任何帮助都将不胜感激。
谢谢,
尼古拉
答案 0 :(得分:2)
今天看到同样的错误。我的URL字符串有一些空格。将其更改为%20后,问题就消失了。
答案 1 :(得分:1)
要使用HTTP代理来访问HTTPS URL,需要构建隧道。这是失败的;代理不创建隧道。您确认您使用的代理是否支持您尝试访问的网址?