- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 Xamarin.Forms 项目出现以下错误:
at System.Threading.CancellationToken.ThrowOperationCanceledException () [0x00010] in <46c2fa109b574c7ea6739f9fe2350976>:0
at System.Threading.CancellationToken.ThrowIfCancellationRequested () [0x00008] in <46c2fa109b574c7ea6739f9fe2350976>:0
at Xamarin.Android.Net.AndroidClientHandler+<>c__DisplayClass44_0.<ConnectAsync>b__0 () [0x0004f] in /Users/vsts/agent/2.155.1/work/1/s/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:343
at System.Threading.Tasks.Task.InnerInvoke () [0x0000f] in <46c2fa109b574c7ea6739f9fe2350976>:0
at System.Threading.Tasks.Task.Execute () [0x00000] in <46c2fa109b574c7ea6739f9fe2350976>:0
--- End of stack trace from previous location where exception was thrown ---
at Xamarin.Android.Net.AndroidClientHandler.DoProcessRequest (System.Net.Http.HttpRequestMessage request, Java.Net.URL javaUrl, Java.Net.HttpURLConnection httpConnection, System.Threading.CancellationToken cancellationToken, Xamarin.Android.Net.AndroidClientHandler+RequestRedirectionState redirectState) [0x000e4] in /Users/vsts/agent/2.155.1/work/1/s/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:393
at Xamarin.Android.Net.AndroidClientHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x00285] in /Users/vsts/agent/2.155.1/work/1/s/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:286
at System.Net.Http.HttpClient.FinishSendAsyncBuffered (System.Threading.Tasks.Task`1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) [0x0017e] in <814c177e4f174da89876fafdde15d02e>:0
at BUCOLogin.Constants.RestService.GetBranchesAsync (System.String uri) [0x00047] in C:\VST\BFR Login\Login\Constants\RestService.cs:23 }
经过一些调查后,似乎一个 token 被取消了,但我不确定这个 token 位于何处,甚至不确定它被取消的原因。我正在使用不需要 APIKey 的开放 API 请求:
常量.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace BUCOLogin.Constants
{
public static class Constants
{
public const string OpenBabbageEndpoint = "http://IP/ILDTEST/XI/Core/GetBranches";
//public const string OpenBabbageAPIKey = "f6a04a6c3fbc534c295f6a5e8548e0f6";
}
}
单步执行代码时,它会在以下代码处停顿/等待一段时间,然后再给我异常:
string content = await response.Content.ReadAsStringAsync();
此代码位于 RestService.cs 中:
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace BUCOLogin.Constants
{
public class RestService
{
HttpClient _client;
public RestService()
{
_client = new HttpClient();
}
public async Task<GetBranches> GetBranchesAsync(string uri)
{
GetBranches branchesData = null;
try
{
HttpResponseMessage response = await _client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync();
branchesData = JsonConvert.DeserializeObject<GetBranches>(content);
}
}
catch (Exception ex)
{
Debug.WriteLine("\tERROR {0}", ex.Message);
}
return branchesData;
}
}
}
GetBranches.cs:
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
namespace BUCOLogin.Constants
{
public class GetBranches
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("number")]
public string Number { get; set; }
[JsonProperty("postcode")]
public string Postcode { get; set; }
[JsonProperty("manager")]
public string Manager { get; set; }
[JsonProperty("phone")]
public string Phone { get; set; }
[JsonProperty("address")]
public string Address { get; set; }
[JsonProperty("open")]
public string Open { get; set; }
[JsonProperty("close")]
public string Close { get; set; }
[JsonProperty("opensat")]
public string Opensat { get; set; }
[JsonProperty("closesat")]
public string Closesat { get; set; }
}
}
uri 请求确实会在 Postman 上返回结果,所以我不确定为什么会收到此异常以及如何继续。
提前致谢。
使用@Nkosi 代码更新 RestService.cs 后,出现以下错误:
{System.OperationCanceledException: The operation was canceled.
at System.Threading.CancellationToken.ThrowOperationCanceledException () [0x00010] in <46c2fa109b574c7ea6739f9fe2350976>:0
at System.Threading.CancellationToken.ThrowIfCancellationRequested () [0x00008] in <46c2fa109b574c7ea6739f9fe2350976>:0
at Xamarin.Android.Net.AndroidClientHandler+<>c__DisplayClass44_0.<ConnectAsync>b__0 () [0x0004f] in /Users/vsts/agent/2.155.1/work/1/s/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:343
at System.Threading.Tasks.Task.InnerInvoke () [0x0000f] in <46c2fa109b574c7ea6739f9fe2350976>:0
at System.Threading.Tasks.Task.Execute () [0x00000] in <46c2fa109b574c7ea6739f9fe2350976>:0
--- End of stack trace from previous location where exception was thrown ---
at Xamarin.Android.Net.AndroidClientHandler.DoProcessRequest (System.Net.Http.HttpRequestMessage request, Java.Net.URL javaUrl, Java.Net.HttpURLConnection httpConnection, System.Threading.CancellationToken cancellationToken, Xamarin.Android.Net.AndroidClientHandler+RequestRedirectionState redirectState) [0x000e4] in /Users/vsts/agent/2.155.1/work/1/s/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:393
at Xamarin.Android.Net.AndroidClientHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x00285] in /Users/vsts/agent/2.155.1/work/1/s/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:286
at System.Net.Http.HttpClient.FinishSendAsyncBuffered (System.Threading.Tasks.Task`1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) [0x0017e] in <814c177e4f174da89876fafdde15d02e>:0
at BUCOLogin.Constants.RestService.GetBranchesAsync (System.String uri) [0x00046] in C:\Visual Studio Training\BUCO FR Login\FacialRecognitionLogin\Constants\RestService.cs:18 }
添加 _client.Timeout = TimeSpan.FromSeconds(300);
后:
{Java.Net.SocketTimeoutException: failed to connect to /IP (port 791) from /IP (port 60644) after 86400000ms: isConnected failed: ETIMEDOUT (Connection timed out) ---> Android.Systems.ErrnoException: isConnected failed: ETIMEDOUT (Connection timed out)
--- End of inner exception stack trace ---
at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <dec6115f7d4449ee8ce1bcacaa8cb579>:0
at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeAbstractVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00014] in <dec6115f7d4449ee8ce1bcacaa8cb579>:0
at Java.Net.HttpURLConnectionInvoker.Connect () [0x00000] in /Users/vsts/agent/2.155.1/work/1/s/src/Mono.Android/obj/Release/android-28/mcw/Java.Net.HttpURLConnection.cs:714
at Xamarin.Android.Net.AndroidClientHandler+<>c__DisplayClass44_0.<ConnectAsync>b__0 () [0x0005a] in /Users/vsts/agent/2.155.1/work/1/s/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:344
at System.Threading.Tasks.Task.InnerInvoke () [0x0000f] in <46c2fa109b574c7ea6739f9fe2350976>:0
at System.Threading.Tasks.Task.Execute () [0x00000] in <46c2fa109b574c7ea6739f9fe2350976>:0
--- End of stack trace from previous location where exception was thrown ---
at Xamarin.Android.Net.AndroidClientHandler.DoProcessRequest (System.Net.Http.HttpRequestMessage request, Java.Net.URL javaUrl, Java.Net.HttpURLConnection httpConnection, System.Threading.CancellationToken cancellationToken, Xamarin.Android.Net.AndroidClientHandler+RequestRedirectionState redirectState) [0x000e4] in /Users/vsts/agent/2.155.1/work/1/s/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:393
at Xamarin.Android.Net.AndroidClientHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x00285] in /Users/vsts/agent/2.155.1/work/1/s/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:286
at System.Net.Http.HttpClient.FinishSendAsyncBuffered (System.Threading.Tasks.Task`1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) [0x0017e] in <814c177e4f174da89876fafdde15d02e>:0
at BUCOLogin.Constants.RestService.GetBranchesAsync (System.String uri) [0x00066] in C:\Visual Studio Training\BUCO FR Login\FacialRecognitionLogin\Constants\RestService.cs:21
--- End of managed Java.Net.SocketTimeoutException stack trace ---
java.net.SocketTimeoutException: failed to connect to /IP (port 791) from /IP (port 60644) after 86400000ms: isConnected failed: ETIMEDOUT (Connection timed out)
at libcore.io.IoBridge.isConnected(IoBridge.java:276)
at libcore.io.IoBridge.connectErrno(IoBridge.java:187)
at libcore.io.IoBridge.connect(IoBridge.java:129)
at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:137)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:391)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:231)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:213)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
at java.net.Socket.connect(Socket.java:621)
at com.android.okhttp.internal.Platform.connectSocket(Platform.java:145)
at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:141)
at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:112)
at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184)
at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)
Caused by: android.system.ErrnoException: isConnected failed: ETIMEDOUT (Connection timed out)
at libcore.io.IoBridge.isConnected(IoBridge.java:267)
... 18 more
}
最佳答案
您可以尝试增加 HttpClient.Timeout
以避免 OperationCanceledException
。
为了
Java.Net.SocketTimeoutException: failed to connect to /IP (port 791) from /IP (port 60644) after 86400000ms: isConnected failed: ETIMEDOUT (Connection timed out) ---> Android.Systems.ErrnoException: isConnected failed: ETIMEDOUT (Connection timed out)
确保您的设备可以通过网络访问它所连接的 IP/端口。
关于c# - CancellationToken.ThrowOperationCanceledException异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58450320/
我是一名优秀的程序员,十分优秀!