mardi 5 janvier 2021

c# proxy request to webpage returns error?

I am trying to code an application which allows me to login into my Netflix account and I am trying to manage some data from there. Now my request works when using it without a proxy. I successfully get redirected to the netflix/browse page where I can select a user profile. But when using a http proxy it returns a webpage which shows that the password is invalid. Now my question is how to fix this? I have googled a bit but did not really find anything so I hope someone can help me here. I can only assume its something with the proxy but I really dont know.

Picture of request with proxy even tho the password is correct: https://imgur.com/a/GtmfI76

With proxy --> does not work

Without proxy --> works

Here is my code that I use

        string urlGet = "https://www.netflix.com";
        string urlPost = "https://www.netflix.com/login";
        string username = "myemail";
        string password = "mypassword";
        string proxy = "219.147.112.150:1080"; // this is an example proxy but the normal ones work for sure

        var cookieContainer = new CookieContainer();
        var httpclientHandler = new HttpClientHandler();
        httpclientHandler.CookieContainer = cookieContainer;
        httpclientHandler.Proxy = new WebProxy(proxy);
        httpclientHandler.UseProxy = true;
        httpclientHandler.UseDefaultCredentials = true;

        var httpClient = new HttpClient(httpclientHandler);
        var result = await httpClient.GetAsync(urlGet);
      
        string resultString = "";
        Stream resultStream = await result.Content.ReadAsStreamAsync();
        using (StreamReader reader = new StreamReader(resultStream))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                resultString += line;
            }
        }

        List<Cookie> cookies = cookieContainer.GetCookies(new Uri(urlGet)).Cast<Cookie>().ToList();

        string authURL = Regex.Match(resultString, @"(?<=""authURL"":"")(.*?)(?="")").Value.Replace(@"\x3D", "=").Replace(@"\x2F", "/").Replace(@"\x2B", "+");

        var content = new System.Net.Http.FormUrlEncodedContent(new[]
        {
            new KeyValuePair<string, string>("userLoginId", username),
            new KeyValuePair<string, string>("password", password),
            new KeyValuePair<string, string>("rememberMe", "false"),
            new KeyValuePair<string, string>("flow", "websiteSignUp"),
            new KeyValuePair<string, string>("mode", "login"),
            new KeyValuePair<string, string>("action", "loginAction"),
            new KeyValuePair<string, string>("withFields", "rememberMe,nextPage,userLoginId,password,countryCode,countryIsoCode,recaptchaResponseToken,recaptchaError,recaptchaResponseTime"),
            new KeyValuePair<string, string>("authURL", authURL),
        });
        // settings for httpclient

        httpClient.DefaultRequestHeaders.Host = "www.netflix.com";
        httpClient.DefaultRequestHeaders.AcceptLanguage.ParseAdd("en-US;q=0.8,en;q=0.7");
        httpClient.DefaultRequestHeaders.Accept.ParseAdd("text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
        httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(userAgent);

        var resultPOST = await httpClient.PostAsync(new Uri(urlPost), content);
        string resultPOSTString = "";
        Stream resultPOSTStream = await resultPOST.Content.ReadAsStreamAsync();
        using (StreamReader reader = new StreamReader(resultPOSTStream))
        {
            resultPOSTString = reader.ReadToEnd();
        }
       
        if (resultPOSTString.Contains("/browse") || resultPOSTString.Contains("/signup"))
        {
            File.WriteAllText(@"C:\Users\PC3221\Desktop\postrq.html", resultPOSTString); // THIS WORKS WITHOUT PROXY AND RETURNS GOOD RESULT
            Console.WriteLine("Done! Logged in");

        }
        else
        {
            Console.WriteLine("Tried password but failed!");
            File.WriteAllText(@"C:\Users\PC3221\Desktop\postrqNotWorking.html", resultPOSTString); // THIS IS THE RESULT WITH PROXY AND RETURNS THE IMAGE I POSTED AT THE TOP
        }



Aucun commentaire:

Enregistrer un commentaire