gpt4 book ai didi

android - Java.IO.EOFException 通过授权代理从 OkHttpClient 读取

转载 作者:搜寻专家 更新时间:2023-11-01 08:52:45 30 4
gpt4 key购买 nike

尝试从 HttpUrlConnection 读取数据时出现 Java.IO.EOFException(访问 InputStream 时)我将 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 时,它使用代理授权(但我有理由尽可能不使用 mono 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)

如有任何帮助,我们将不胜感激。

谢谢,

尼古拉

最佳答案

今天看到同样的错误。我的 URL 字符串有一些空格。将其更改为 %20 后,问题消失。

关于android - Java.IO.EOFException 通过授权代理从 OkHttpClient 读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21620290/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com