我有ID的列表,我正在遍歷這些ID中的每一個(gè)以獲取詳細(xì)信息。在少數(shù)情況下,API調(diào)用失敗。因此,我想跳過失敗的API調(diào)用,并希望繼續(xù)進(jìn)行下一個(gè)ID API調(diào)用;類似于on error goto next。foreach (var item in ID){ try { List<string> resultList = new List<string>(); url = string.Format(HttpContext.Current.Session["url"].ToString() + ConfigurationManager.AppSettings["propertyURL"].ToString(), HttpContext.Current.Session["ObjectID"].ToString(), item); var request1 = WebRequest.Create(url); request1.Headers["X-Authentication"] = HttpContext.Current.Session["vaultToken"].ToString(); request1.Method = "GET"; request.Headers.Add("Cache-Control", "no-cache"); //// Get the response. var response1 = request1.GetResponse(); var deserializer1 = new DataContractJsonSerializer(typeof(PropertyValue[])); var result1 = (PropertyValue[])deserializer1.ReadObject(response1.GetResponseStream()); } catch (Exception ex) { }}有什么可能的解決方案,即使API調(diào)用失敗,foreach循環(huán)也將用于下一個(gè)ID API調(diào)用。
2 回答

aluckdog
TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超7個(gè)贊
實(shí)際上,這更加清楚。
Dictionary<int, string> resultList = new Dictionary<int, string>();
foreach (var item in ID)
{
resultList.Add(item,string.Empty);
try
{
// Your Call Here
// Add Result to resultList here
var apiCallResult = apiCall(item);
resultList[item] = apiCallResult;
}
catch
{
}
}
您可以在字典中查詢ID沒有結(jié)果的。
- 2 回答
- 0 關(guān)注
- 164 瀏覽
添加回答
舉報(bào)
0/150
提交
取消