In this post you will learn how to use SSIS Script Task (C# Code) to detect specific URL found or not (Detect HTTP 404 Error)
If you using SSIS JSON Source or our REST API Task then if URL/Document you trying to read not found then server may return 404 (Not Found) Exception. In order to handle this error gracefully you can use following Script to store status in a variable and then you can use conditional Control Flow to Skip data loading from Web Service.
Step1: Drag Script Task from toolbox
Step2: Select Language as C#
Step3: Select Readonly variable (In this case “User::url” – String Type)
Step4: Select ReadWrite variable (In this case “User::found” – Boolean Type)
Step5: Edit code and paste following script in your script task
C# Code: Check URL Exists (Detect 404 Error)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public void Main() { // TODO: Add your code here Dts.Variables["found"].Value = UrlFound(Dts.Variables["url"].Value.ToString()); Dts.TaskResult = (int)ScriptResults.Success; } private bool UrlFound(string url) { try { var request = WebRequest.Create(url); var response = request.GetResponse(); return true; } catch (WebException we) { HttpWebResponse errorResponse = we.Response as HttpWebResponse; if (errorResponse != null) if (errorResponse.StatusCode == HttpStatusCode.NotFound) { return false; } else throw; //we only look for 404 Error } return false; } |