Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 刮薄荷糖会导致“会话已过期”_C#_C# 4.0_Webclient - Fatal编程技术网

C# 刮薄荷糖会导致“会话已过期”

C# 刮薄荷糖会导致“会话已过期”,c#,c#-4.0,webclient,C#,C# 4.0,Webclient,我尝试使用WebClient类在C中执行两个http web请求。第一个是登录mint.com的帖子,第二个是从mint获取登录用户的交易。我相信我是在登录后检索设置的cookie,并将其传递到请求中以获取事务,但是它给了我响应: 媫\b\0\0\0\0\0ÿ\0\0\0ÿÿf\0™ÿ1会话已过期。™ØÒf\0\0\0 这是代码,你知道我做错了什么会导致会话设置不正确吗 public static class MintService { private static st

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

媫\b\0\0\0\0\0ÿ\0\0\0ÿÿf\0™ÿ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");
            }
        }
    }