繁体   English   中英

HttpClient Post仅在提琴手运行时有效

[英]HttpClient Post only works when fiddler is running

public async Task<LoginResult> Login(string username, string password)
    {
        cookies = new CookieContainer();
        handler = new HttpClientHandler()
        {
            CookieContainer = cookies,
            UseCookies = true,
            AllowAutoRedirect = true,
            UseProxy = true,
            Proxy = null
        };
        ThreadActivity.Account = username;
        ThreadActivity.Status = "Logging in...";
        LoginResult result = new LoginResult();
        try
        {
            cookies = new CookieContainer();
            client = new HttpClient(handler);
            client.DefaultRequestHeaders.Connection.Add("keep-alive");
            client.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue() { MaxAge = TimeSpan.Zero };
            client.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip,deflate,sdch");
            client.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.8");
            client.DefaultRequestHeaders.Add("Accept-Charset", "ISO-8859-1");
            client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36");

            HttpResponseMessage hr = await client.GetAsync("https://instagram.com/accounts/login/#");
            if (!hr.IsSuccessStatusCode)
                throw new Exception("Couldn't load instagram page; " + hr.ReasonPhrase);
            string source = await hr.Content.ReadAsStringAsync();
            //Get login token
            string token = ParseFormNameText(source, "csrfmiddlewaretoken");
            //Login
            HttpContent content = new FormUrlEncodedContent(new[]
            {
                    new KeyValuePair<string,string>("csrfmiddlewaretoken", token),
                    new KeyValuePair<string, string>("username", username),
                    new KeyValuePair<string, string>("password", password)
            });
            client.DefaultRequestHeaders.Referrer = new Uri("https://instagram.com/accounts/login/");
            hr = await client.PostAsync("https://instagram.com/accounts/login/", content);
            if (!hr.IsSuccessStatusCode)
                throw new Exception("Couldn't submit login; " + hr.ReasonPhrase);
            source = await hr.Content.ReadAsStringAsync();
            if (source.Contains("Please enter a correct username and password"))
                throw new Exception("Couldn't login; invalid username/password.");
            //Logged in, login to webstagram now
            hr = await client.GetAsync("https://instagram.com/oauth/authorize/?client_id=9d836570317f4c18bca0db6d2ac38e29&redirect_uri=http://web.stagram.com/&response_type=code&scope=likes+comments+relationships");
            if (!hr.IsSuccessStatusCode)
                throw new Exception("Couldn't load webstagram login; " + hr.ReasonPhrase);
            source = await hr.Content.ReadAsStringAsync();
            if (!source.Contains(">LOG OUT</a>"))
                throw new Exception("Couldn't load webstagram; failed to login.");
            RaiseEvent("Logged in!", this);
        }
        catch (Exception ex)
        {
            RaiseEvent(ex.Message, this);
            result.ErrorMessage = ex.Message;
        }
        finally
        {
            result.Success = string.IsNullOrEmpty(result.ErrorMessage);
        }
        return result;
    }

这是我的登录方法,但是当它实际上尝试提交登录时,出现403禁止错误。 但是,当我尝试在Fiddler运行时执行此操作时,它会起作用。

我不太确定为什么要这么做,也许有人可以帮忙吗?

我曾经有过类似的问题。 问题在于,提琴手-在拦截流量时-更改了请求。 我认为在我们的情况下,代理服务器配置为阻止此类请求(ASP.net请求失败)“ CONNECT www.20min.ch”,Fiddler将请求更改为“ CONNECT http://www.20min.ch ”这被允许通过代理(工作)。 也许您需要使用Wireshark比较Fiddler和ASP.net的请求,并查找它们之间的区别。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM