2014-02-07 19 views
1

當嘗試從HttpUrlConnection讀取數據時(訪問InputStream時),我得到Java.IO.EOFException 我使用了帶有OkHttp庫的xamarin,該應用程序在genymotion上運行仿真器與Android 4.2.2Java.IO.EOFException通過具有授權的代理從OkHttpClient讀取

下面是示例代碼(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單WebClient的,它與代理授權的作品(但我有理由不使用單聲道的WebClient如果可能的話)

解決方法conn.SetRequestProperty(「連接」,「關閉」);沒有幫助。

堆棧跟蹤:

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

你有沒有試過ModernHttpClient:https://github.com/paulcbetts/ModernHttpClient? – Cheesebaron

+0

@ Cheesebaron,實際上,我使用從安裝的ModernHttpClient的NuGet包中的OkHttp。我不知道如何將代理設置傳遞給HttpClient或OkHttpNetworkHandler(通過頭文件?),所以使用OkHttpClient(OkHttpNetworkHandler也使用它) –

+0

我有類似的問題。我實際上使用直接的HttpURLConnection(如果你看看你的調用棧,OkHttpClient也使用它)。但是我發現這隻發生在HTTS網址上,它可以很好地處理HTTP網址。 @NicolaiShestakov自2014年發佈以來,你有沒有發現任何東西? –

回答

1

要使用HTTP代理到達它需要建立一個隧道的HTTPS URL。這是失敗的;代理不會創建隧道。您是否確認您使用的代理支持您嘗試訪問的網址?

+0

它通過.NET WebClient類來工作,所以它必須與代理一致OK –

+0

你能用wireshark捕獲線數據嗎?看看您的代理髮送的OkHttp無法處理的內容會很有趣。 –

+0

@JesseWilson我有類似的問題。它適用於HTTP,但不適用於HTTPS。我在本地機器上使用Fiddler,並使用'HttpURLConnection'。我很確定這不是Fiddler的問題,因爲如果我在C#控制檯應用程序中使用具有相同代理和相同憑據的HttpWebRequest,它對HTTP和HTTPS都可以正常工作 –

2

今天看到相同的錯誤。我的URL字符串有一些空格。將其更改爲%20後,問題消失。