1

我正在尝试使用 WebClient 类在 C# 中执行两个 http Web 请求。第一个是登录 mint.com 的 POST,第二个是 GET,然后从 mint 获取登录用户的交易。我相信我在登录后检索设置的 cookie 并将它们传递到请求中以获取事务,但是它给了我响应:

‹\b\0\0\0\0\0ÿ\0\0\0ÿÿf\0™ÿ<code>1会话已过期。™ØÒf\0\0\0

这是代码,任何想法我做错了什么会导致它无法正确设置会话?

public static class MintService
    {
        private static string Domain = "https://wwws.mint.com";

        public static void GetTransactions(string userName, string password) {
            var cookies = Login(userName, password);
            var transactionsHtml = DownloadTransactions(userName, cookies);
        }

        private static string Login(string userName, string password) {
            var url = Domain + "/login.event";
            var requestBody = String.Format("validation=%7B%22validators%22%3A%5B%7B%22name%22%3A%22username%22%2C%22func%22%3A%22isNotEmpty%22%2C%22type%22%3A%22inline%22%2C%22validationProperty%22%3A%22isNotBlank%22%2C%22copy%22%3A%22Please+enter+your+email+address.%22%7D%2C%7B%22name%22%3A%22username%22%2C%22func%22%3A%22isEmail%22%2C%22type%22%3A%22inline%22%2C%22validationProperty%22%3A%22isEmail%22%2C%22copy%22%3A%22Please+use+a+valid+email+address.%22%7D%2C%7B%22name%22%3A%22password%22%2C%22func%22%3A%22isNotEmpty%22%2C%22type%22%3A%22inline%22%2C%22validationProperty%22%3A%22isNotBlank%22%2C%22copy%22%3A%22Please+enter+your+password.%22%7D%5D%2C%22validatorn%22%3A2%7D&username={0}&password={1}&remember=T&task=L&timezone=-5&nextPage=&browser=Chrome&browserVersion=26&os=v&clientType=Mint",
                userName.Replace("@", "%40"), password);
            return WebRequestHelper.PostWithCookies(url, requestBody, "application/x-www-form-urlencoded");
        }

        private static string DownloadTransactions(string userName, string cookies) {
            var url = Domain + String.Format("/listTransaction.xevent?queryNew={0}&offset={1}&filterType={2}&comparableType={3}&rnd={4}", 
                "", 0, "cash", 3, "1325292983068");
            var response = WebRequestHelper.Get(url, "text/html", cookies);
            return response;
        }
    }

public static class WebRequestHelper {
        public static string Get(string url, string contentType = "application/json", string cookies = null) {
            using (var client = new WebClient()) {
                AddRequestHeaders(url, client, contentType);
                if (!String.IsNullOrEmpty(cookies))
                    client.Headers.Add(HttpRequestHeader.Cookie, cookies);
                return client.DownloadString(url);
            }
        }

        public static string PostWithCookies(string url, string requestBody, string contentType = "application/json") {
            using (var client = new WebClient()) {
                AddRequestHeaders(url, client, contentType);
                var response = client.UploadString(url, "POST", requestBody);
                return client.ResponseHeaders[HttpResponseHeader.SetCookie];
            }
        }

        private static void AddRequestHeaders(string url, WebClient client, string contentType) {
            client.Headers.Add(HttpRequestHeader.Accept, "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            client.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
            client.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate,sdch");
            client.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.8");
            client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31");
            client.Headers.Add(HttpRequestHeader.ContentType, contentType);
            if (url.IndexOf("mint.com") > -1) {
                client.Headers.Add(HttpRequestHeader.Host, "wwws.mint.com");
                client.Headers.Add(HttpRequestHeader.Referer, "https://wwws.mint.com/login.event?task=L");
            }
        }
    }
4

0 回答 0