简体   繁体   中英

How to detect and handle API HTTP Response Codes

I am using a C# Console App to interact with the DotMailer API.

The API returns [this][1] list of API Response Types.

I wonder how could I possibly detect these and then handle them in code?

For example in the block of code below, the function of GetCampaignSummary() may fail due to an Invalid_Campaign_Error, which would cause the program to trip over. How can I handle this without having the program quiting due to an exception?

foreach (var campaign in allCampaigns)
{
    if (campaign.Status == CampaignStatus.Sent)
    {
        if (campaign.Id == 111117)
        {
            var campaignSummaryData = api.GetCampaignSummary(UserName, Password, campaign.Id).DateSent;

            // Insert campaign data
        }

    }
}

Useful Link: https://developer.dotmailer.com/docs/error-response-types

Based on the documentation, it clearly at some stage will respond with a HttpWebResponse . This would allow you to simply do the following HttpWebResponse.StatusCode .

Sample:

var request =  WebRequest.Create("...") as HttpWebRequest;
using(var response = request.GetResponse())
{
     if(response.StatusCode != 200)
     {
          // An Error...
     }
}

I'm fairly certain it implements, IDisposable . But would be the general gist of access, without knowing for certain the type of those objects and variables, it will be quite difficult to assist you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM