{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":22252,"path":"integrate-applications/t_CallURLExportProgrammatically.md","title":"Call URL export programmatically","bundle":null,"doc_type":null,"release":"australia","body":"\n# Call URL export programmatically\n\nDynamically export data from a script or web service by calling a URL export from any programming language.\n\n## Before you begin\n\nRole required: none\n\n## About this task\n\nThe following procedure includes code samples that demonstrate calling a URL export in C\\# for a .Net framework call:\n\n## Procedure\n\n1.  Add the following Imports:\n\n    ```\n    using System.Net;\n    using System.IO;\n    ```\n\n2.  Call the Download method:\n\n    ```\n    static void Main(string[] args)\n    \n         {\n            // Call to DownloadFile method supplying the URL and location to save CSV file locally\n            int read = DownloadFile(\"https://<instance>.service-now.com/incident_list.do?CSV&sysparm_query=priority=1&sysparm_orderby=assigned_to\",\n                         \"c:\\\\test\\\\incident.csv\");\n        }\n    ```\n\n3.  Create a Download method as follows:\n\n    ```\n    public static int DownloadFile(String url,\n                                           String localFilename)\n        {\n            // Function will return the number of bytes processed\n            // to the caller. Initialize to 0 here.\n            int bytesProcessed = 0;\n            // Assign values to these objects here so that they can\n            // be referenced in the finally block\n            Stream remoteStream = null;\n            Stream localStream = null;\n            WebResponse response = null;\n            // Use a try/catch/finally block as both the WebRequest and Stream\n            // classes throw exceptions upon error\n            try\n            {\n                // Create a request for the specified remote file name\n                WebRequest request = WebRequest.Create(url);\n                // Create the credentials required for Basic Authentication\n                System.Net.ICredentials cred = new System.Net.NetworkCredential(\"user_name\", \"password\");\n                // Add the credentials to the request\n                request.Credentials = cred;\n                if (request != null)\n                {\n                    // Send the request to the server and retrieve the\n                    // WebResponse object \n                    response = request.GetResponse();\n                    if (response != null)\n                    {\n                        // Once the WebResponse object has been retrieved,\n                        // get the stream object associated with the response's data\n                        remoteStream = response.GetResponseStream();\n                        // Create the local file\n                        localStream = File.Create(localFilename);\n                        // Allocate a 1k buffer\n                        byte[] buffer = new byte[1024];\n                        int bytesRead;\n                        // Simple do/while loop to read from stream until\n                        // no bytes are returned\n                        do\n                        {\n                            // Read data (up to 1k) from the stream\n                            bytesRead = remoteStream.Read(buffer, 0, buffer.Length);\n                            // Write the data to the local file\n                            localStream.Write(buffer, 0, bytesRead);\n                            // Increment total bytes processed\n                            bytesProcessed += bytesRead;\n                        } while (bytesRead > 0);\n                    }\n                }\n            }\n            catch (Exception e)\n            {\n                Console.WriteLine(e.Message);\n            }\n            finally\n            {\n                // Close the response and streams objects here \n                // to make sure they're closed even if an exception\n                // is thrown at some point\n                if (response != null) response.Close();\n                if (remoteStream != null) remoteStream.Close();\n                if (localStream != null) localStream.Close();\n            }\n            // Return total bytes processed to caller.\n            return bytesProcessed;\n        }\n    ```\n\n\n"}}