我正在使用Xamarin Forms来处理Web服务。我制作了一个简单的Web服务,它托管在IIS Express上(VS 2015附带)。 Web服务处理员工信息。
最初使用Windows模拟器时效果很好。但我有问题让它与Android模拟器一起工作。求助于我从一个人那里得到了以下指示,
按照说明操作后,它不仅在Android模拟器中不起作用,而且UWP应用程序也停止工作。
以下是失败的地方
public class RestClient<T>
{
private const string WebServiceUrl = "http://localhost:52792/api/Employees/";
//private const string WebServiceUrl = "http://services.groupkt.com/country/get/all/";
public async Task<List<T>> GetAsync()
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.ExpectContinue = false;
var json = "";
try
{
**json = await httpClient.GetStringAsync(WebServiceUrl);
}
catch (HttpRequestException e)
{
var error = "";
error=e.StackTrace;
error = e.Source;
error = e.HelpLink;
error = e.ToString();
}
var taskModels = JsonConvert.DeserializeObject<List<T>>(json);
return taskModels;
}
以下是抛出的错误,
System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Runtime.InteropServices.COMException: The text associated with this error code could not be found.
A connection with the server could not be established
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpHandlerToFilter.<SendAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpClientHandler.<SendAsync>d__86.MoveNext()
--- End of inner exception stack trace ---
at System.Net.Http.HttpClientHandler.<SendAsync>d__86.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpClient.<FinishSendAsync>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpClient.<GetContentAsync>d__32`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Plugin.RestClient.RestClient`1.<GetAsync>d__1.MoveNext()
我不知道这里出了什么问题。
更新
这在Windows和Windows Phone应用程序中仍然可以正常工作 UWP应用程序失败。
答案 0 :(得分:0)
在您的RestClient类中,URL http://localhost:52792/api/Employees/
指向设备的localhost,而不是托管IIS Express的计算机。
答案 1 :(得分:0)
如果您的UWP应用尝试通过本地主机访问服务,请确认它具有环回豁免。
有关更多详细信息,请参见UWP Enable local network loopback。特别要注意的是,有些人发现环回豁免表可能已损坏,清除所有豁免并重新应用任何要求似乎都可以解决。
https://docs.microsoft.com/en-us/previous-versions/windows/apps/dn640582(v=win.10)