简体   繁体   English

c# 对网页的代理请求返回错误?

[英]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.我正在尝试编写一个允许我登录到我的 Netflix 帐户的应用程序,并且我正在尝试从那里管理一些数据。 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.我成功地被重定向到 netflix/browse 页面,我可以在其中 select 用户配置文件。 But when using a http proxy it returns a webpage which shows that the password is invalid.但是当使用 http 代理时,它会返回一个显示密码无效的网页。 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即使密码正确,使用代理的请求图片: 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
        }

I won't suspect that VPN/Proxy in blacklist IP-based are allowed to connect to Netflix, according to Netflix FAQ https://help.netflix.com/en/node/277根据 Netflix 常见问题解答https://help.netflix.com/en/node/277 ,我不会怀疑基于 IP 的黑名单中的 VPN/Proxy 可以连接到 Netflix

Do you use a VPN or proxy for work or for privacy?您是否将 VPN 或代理用于工作或隐私?

A VPN or proxy may prevent you from streaming content that isn't available globally. VPN 或代理可能会阻止您流式传输在全球范围内不可用的内容。 Disable any VPN or proxy and try Netflix again.禁用任何 VPN 或代理,然后重试 Netflix。

The best test method in this case is using that VPN/Proxy to connect via Browser to make sure it does.在这种情况下,最好的测试方法是使用该 VPN/代理通过浏览器进行连接,以确保它可以连接。 Then we could affirm that VPN/Proxy has been blocked and the message is just wrong context然后我们可以确认 VPN/Proxy 已被阻止并且消息只是错误的上下文

Although they have several workaround ways to detect VPN/Proxy, but IP-based list is not always up-to-date, therefore you might try some other VPN IPs in your country to test via Browser first, perhaps successfully rate could grow up虽然他们有几种解决方法来检测 VPN/代理,但基于 IP 的列表并不总是最新的,因此您可以尝试您所在国家/地区的一些其他 VPN IP 先通过浏览器进行测试,也许成功率会提高

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

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