如何在C#中实现关于API响应的错误转到下一步?
我有一个ID的列表,我正在遍历每个ID以获得详细信息。 在少数情况下,API调用失败。所以我想跳过失败的API调用,继续下一个ID API调用;类似于错误转到下一步如何在C#中实现关于API响应的错误转到下一步?,c#,C#,我有一个ID的列表,我正在遍历每个ID以获得详细信息。 在少数情况下,API调用失败。所以我想跳过失败的API调用,继续下一个ID API调用;类似于错误转到下一步 foreach (var item in ID) { try { List<string> resultList = new List<string>(); url = string.Format(HttpContext.Current.Session["url
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)
{
}
}
foreach(ID中的变量项)
{
尝试
{
列表结果列表=新列表();
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”;
添加(“缓存控制”、“无缓存”);
////得到回应。
var response1=request1.GetResponse();
var deserializer1=新的DataContractJsonSerializer(typeof(PropertyValue[]);
var result1=(PropertyValue[])反序列化器1.ReadObject(response1.GetResponseStream());
}
捕获(例外情况除外)
{
}
}
什么是可能的解决方案,即使API调用失败,foreach循环也会进行下一个ID API调用。看起来您只需要将
结果列表
移到循环之外。其他一切看起来都很好
List<string> resultList = new List<string>();
foreach (var item in ID)
{
try
{
// Your Call Here
// Add Result to resultList here
}
catch
{
}
}
List resultList=new List();
foreach(ID中的变量项)
{
尝试
{
//你的电话在这里
//在此处将结果添加到结果列表
}
抓住
{
}
}
看起来您只需要将结果列表移动到循环之外。其他一切看起来都很好
List<string> resultList = new List<string>();
foreach (var item in ID)
{
try
{
// Your Call Here
// Add Result to resultList here
}
catch
{
}
}
List resultList=new List();
foreach(ID中的变量项)
{
尝试
{
//你的电话在这里
//在此处将结果添加到结果列表
}
抓住
{
}
}
事实上,这一点更清楚
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
{
}
}
Dictionary resultList=new Dictionary();
foreach(ID中的变量项)
{
添加(item,string.Empty);
尝试
{
//你的电话在这里
//在此处将结果添加到结果列表
var apiCallResult=apiCall(项目);
结果列表[项目]=apiCallResult;
}
抓住
{
}
}
您可以在字典中查询没有结果的ID
s。实际上,这更清楚
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
{
}
}
Dictionary resultList=new Dictionary();
foreach(ID中的变量项)
{
添加(item,string.Empty);
尝试
{
//你的电话在这里
//在此处将结果添加到结果列表
var apiCallResult=apiCall(项目);
结果列表[项目]=apiCallResult;
}
抓住
{
}
}
您可以在字典中查询没有结果的ID
s。这个空的catch块将来会困扰您!为了放大@lasithapetsawadu所说的内容,您可以在catch块中记录失败的调用。循环将继续,因为try-catch位于foreach中。也就是说,你不必实现任何东西。这段代码有什么问题吗?那个空的catch块将来会困扰你!为了放大@lasithapetsawadu所说的内容,您可以在catch块中记录失败的调用。循环将继续,因为try-catch位于foreach中。也就是说,你不需要实现任何东西。这段代码有什么问题?