2

I want to get a csv file from mint.com and manipulate it in C#. I have to log in to do this. I have followed the suggestions of another post on Stack Overflow and have come up with the following code. Unfortunately, the result I get is not the intended CSV but instead an error page. Any insight into what I am doing wrong or how I could do this another way?

    string cookieHeader = CookieHeaderFromMintLogin("myusername","mypassword");
    string csvURL = "https://wwws.mint.com/transactionDownload.event?queryNew=&offset=0&filterType=cash&comparableType=8";
    string csv = getResponseWithCookies(csvURL, cookieHeader);

    string CookieHeaderFromMintLogin(string username, string password)
    {

        string formUrl = "https://wwws.mint.com/loginJumper.event"; // NOTE: This is the URL the form POSTs to, not the URL of the form (you can find this in the "action" attribute of the HTML's form tag
        string formBuilId = HttpUtility.UrlEncode("form-NAjKdtCT8Z5SuyDHTsmRREioaCMXVEJZpt8-9oaSMEY");
        string formId = HttpUtility.UrlEncode("mint_auth_mint_com_login_form");
        username = HttpUtility.UrlEncode(username);
        password = HttpUtility.UrlEncode(password);
        string formParams = string.Format("username={0}&password={1}&form_build_id={2}&form_id={3}", username, password, formBuilId,formId);
        WebRequest req = WebRequest.Create(formUrl);
        req.ContentType = "application/x-www-form-urlencoded";
        req.Method = "POST";
        byte[] bytes = Encoding.ASCII.GetBytes(formParams);
        req.ContentLength = bytes.Length;
        using (Stream os = req.GetRequestStream())
        {
            os.Write(bytes, 0, bytes.Length);
        }
        WebResponse resp = req.GetResponse();

        return resp.Headers["Set-cookie"];
    }

    string getResponseWithCookies(string getUrl, string cookieHeader)
    {
        string pageSource;
        WebRequest getRequest = WebRequest.Create(getUrl);
        getRequest.Headers.Add("Cookie", cookieHeader);
        WebResponse getResponse = getRequest.GetResponse();
        using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
        {
            pageSource = sr.ReadToEnd();
        }

        return pageSource;
    }

0 Answers0