C# 从网页中读取JSON一开始会抛出错误,但之后会起作用
我有一个从网页获取JSON字符串并对其进行解析的应用程序,等等。我遇到的问题是,第一次运行该应用程序时会抛出以下错误: A first chance exception of type 'System.Net.Http.HttpRequestException' occurred in mscorlib.dll An error occurred while sending the request. System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 128.0.46.69:80 at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) --- End of inner exception stack trace --- at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar) 我使用了splitpage模板。我也在MSDN上发布了这个问题,但我得到的唯一答案是版主说我的网络或我尝试访问的webvpage有问题C# 从网页中读取JSON一开始会抛出错误,但之后会起作用,c#,.net,json,windows-store-apps,C#,.net,Json,Windows Store Apps,我有一个从网页获取JSON字符串并对其进行解析的应用程序,等等。我遇到的问题是,第一次运行该应用程序时会抛出以下错误: A first chance exception of type 'System.Net.Http.HttpRequestException' occurred in mscorlib.dll An error occurred while sending the request. System.Net.WebException: Unable to connect to th
我的网络和网页都没有问题第一次总是第二次,这真的很奇怪。您在清单上添加了专用网络访问(
privateNetworkClientServer
)吗?是的,我已经添加了。同样的问题,之后我删除了它…同样的问题为什么你会担心第一次机会的例外?忽略它们。我不希望这样做,因为它可能会在应用程序向公众发布后的第一次启动时造成问题。
private async Task GetSampleDataAsync()
{
if (this._groups.Count != 0) return;
int loopNo = 0;
while (true)
{
loopNo++;
try
{
// Fetchibg JSON file from http
var client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(new Uri("http://etc/data.php"));
var jsonString = await response.Content.ReadAsStringAsync();
JsonObject jsonObject = JsonObject.Parse(jsonString);
JsonArray jsonArray = jsonObject["Groups"].GetArray();
foreach (JsonValue groupValue in jsonArray)
{
JsonObject groupObject = groupValue.GetObject();
SampleDataGroup group = new SampleDataGroup(groupObject["UniqueId"].GetString(),
groupObject["Title"].GetString(),
groupObject["Subtitle"].GetString(),
groupObject["ImagePath"].GetString());
foreach (JsonValue itemValue in groupObject["Items"].GetArray())
{
JsonObject itemObject = itemValue.GetObject();
group.Items.Add(new SampleDataItem(itemObject["UniqueId"].GetString(),
itemObject["Title"].GetString(),
itemObject["Subtitle"].GetString(),
itemObject["ImagePath"].GetString(),
itemObject["Content"].GetString()));
}
this.Groups.Add(group);
}
break;
}
catch (Exception exp)
{
Debug.WriteLine(exp.InnerException);
}
}
}