Need a way to upload a generated file to a website using SSIS? The code below uses HttpWebRequest to make page requests, HttpWebResponse to get server responses and CookieContainer to hold the session data.
The website required a session cookie along with login credentials. To complicate things even more the cookie was created on one page, login was on another and file upload was on a third page.
The final result is a way to upload a file to a website using SSIS without any specials tools. This is an alternative to legacy WGET scripts.
using System; using System.Data; using Microsoft.SqlServer.Dts.Runtime; using System.IO; using System.Text; using System.Net; namespace ST_{GUID}.csproj { [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")] public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase { #region VSTA generated code enum ScriptResults { Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success, Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure }; #endregion public void Main() { HttpWebRequest wPost; HttpWebResponse wResponse; CookieContainer cookieCont; DateTime fileDate = DateTime.Now; //suppress self signed cert error if necessary //ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; string hostURL = @"https://website.com"; //string testURL = @"https://test.website.com"; string cookieURL = "/index.html"; string loginURL = "/loginpage"; string uploadURL = "/uploadpage"; string fileToLoad = @"\\server\path\file_" + String.Format("{0:yyyy-MM-dd}", fileDate) + ".txt"; string userName = "yourLogin"; string passWord = "yourPassword"; string loginData = "username=" + userName + "&password=" + passWord; byte[] byteArrayLogin = Encoding.UTF8.GetBytes(loginData); //get cookie from the first page wPost = (HttpWebRequest)WebRequest.Create(hostURL + cookieURL); wPost.CookieContainer = new CookieContainer(); cookieCont = wPost.CookieContainer; //get response wResponse = (HttpWebResponse)wPost.GetResponse(); wResponse.Close(); //post login creds to login page wPost = (HttpWebRequest)WebRequest.Create(hostURL + loginURL); wPost.Method = "POST"; wPost.CookieContainer = cookieCont; wPost.ContentType = "application/x-www-form-urlencoded"; wPost.ContentLength = byteArrayLogin.Length; //get request stream and write login data to it using (Stream s = wPost.GetRequestStream()) { s.Write(byteArrayLogin, 0, byteArrayLogin.Length); s.Close(); } //get response wResponse = (HttpWebResponse)wPost.GetResponse(); wResponse.Close(); //upload file wPost = (HttpWebRequest)WebRequest.Create(hostURL + uploadURL); wPost.Method = "POST"; //set timeout threshold to 5 mins to allow script to run against slow servers wPost.Timeout = 600000; wPost.CookieContainer = cookieCont; wPost.ContentType = "application/x-www-form-urlencoded"; Stream uploadStream = wPost.GetRequestStream(); StreamWriter sourceWriter = new StreamWriter(uploadStream); //open file StreamReader sourceFile = new StreamReader(fileToLoad); //loop through file and read each line and write to request stream buffer string inLine = sourceFile.ReadLine(); while (inLine != null) { sourceWriter.WriteLine(inLine); inLine = sourceFile.ReadLine(); } sourceFile.Close(); sourceWriter.Close(); //get response wResponse = (HttpWebResponse)wPost.GetResponse(); Stream receiveStream = wResponse.GetResponseStream(); StreamReader rdrResp = new StreamReader(receiveStream, Encoding.UTF8); //output response here if needed rdrResp.Close(); wResponse.Close(); Dts.TaskResult = (int)ScriptResults.Success; } } }
Could you please share more details? Its very useful.