使用 webRequest 对网站的身份验证会导致错误 403

本文关键字:错误 身份验证 webRequest 网站 使用 | 更新日期: 2023-09-27 18:35:54

在互联网上与同事一起搜索了近 2 天后,我没有找到可以帮助我的解决方案......这就是为什么我需要你的帮助:)

我在这里的问题是webRequest可以连接到银行网站,使用ID和密码连接,去获取一些信息并将它们还给我。

到目前为止,我

为一家银行制作它,它运行得很好,但我需要与另一家银行和新银行一起做,我以前做的事情不想工作。

最大的问题是在我尝试连接的wbesite给出的请求的响应中,我有一个:"远程服务器返回错误:(403)禁止。我看到互联网上的很多人都遇到了webRequest和这个403错误的问题,并尝试了一切可能的方法。无论我尝试什么,设置默认标题,我自己做的标题或其他任何事情,我都会收到此错误。即使我的ID和密码是假的,我也会收到此错误,所以我甚至无法连接到网站。

目前的理论是,我正在尝试连接到我无法访问的剧目页面,并且我的 HttpHeader 在连接 URI 之后是不可能的,并且不适用于这家银行。

我还尝试更改线程时间(看看他们是否因为连接不是人类而拒绝访问)我试图pu用户代理,它没有任何改变。可以帮助我的东西:我可以像以前一样在 uri 中传递连接信息吗?还是有另一种可行的方法?

PS :如果您需要其他或更精确的信息,请不要犹豫。

这是代码。

谢谢你的帮助。

public static List<Compte> requeteBank(PopUpWebRequest popup)
    {
        List<Compte> listeCompte = new List<Compte>();
        ASCIIEncoding encoding = new ASCIIEncoding();
        string contentHTTPHeader = "_cm_user" + id + "&_cm_pwd" + password;
        byte[] data = encoding.GetBytes(contentHTTPHeader);

        string urlConnexion = "https://www.bank.fr/identification/default.cgi"; // This URI doesn't exist, this is for the example
        HttpWebRequest requeteConnexion = (HttpWebRequest)WebRequest.Create(urlConnexion);
        //I even tried with this method 
        // I left this here to show you how I made this 
        //string authInfo = popup.identifiant + ":" + popup.password; 
        //requeteConnexion.Headers["Authorization"] = "Basic " + authInfo;
        //var response = requeteConnexion.GetResponse();
        //authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
        //The form for the Id & Password is a Method POST
        requeteConnexion.Method = "POST";
        Stream connexionStream = requeteConnexion.GetRequestStream();
        // Sending connection data...
        connexionStream.Write(data, 0, data.Length);
        connexionStream.Close();
        requeteConnexion.CookieContainer = new CookieContainer();
        requeteConnexion.AllowAutoRedirect = false;
        HttpWebResponse reponseConnexion = (HttpWebResponse)requeteConnexion.GetResponse(); // Error 403
        //Id from the cookies
        Cookie idSession = reponseConnexion.Cookies["IdSes"];
        reponseConnexion.Close();
        [ The rest of the code.... ]

使用 webRequest 对网站的身份验证会导致错误 403

好消息,我发现了我的问题,所以我给出了完整的答案,也许在未来帮助某人:)感谢所有帮助过我的人!

我的程序中缺少两件事才能正常工作。

首先,即使所有银行来自同一子公司,它们也不相同,它们也不使用相同的HTTP标头。所以我在Mozilla Firefox中使用了一个插件,当我手动连接时,它 https://addons.mozilla.org/fr/firefox/addon/live-http-headers/显示网站的标题和响应。

有了这个,我在创建请求的银行网站标题中添加了很多不同的信息:

requeteConnexion.Method = "POST";
requeteConnexion.ContentType = "application/x-www-form-urlencoded";
requeteConnexion.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0";
requeteConnexion.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9*/*;q=0.8";
requeteConnexion.KeepAlive = true;

我的第二个错误是string contentHTTPHeader = "_cm_user" + id + "&_cm_pwd" + password;
我删除了名称和值之间的空格,忘记在名称后加上"="。好的路线是 string contentHTTPHeader = "_cm_user="+id+"&_cm_pwd="+password; .

对于想知道这里的人,"_cm_user="等来自,当我手动连接时,我可以看到这就是 id 和密码在标头中发送的方式,我只需要在我的代码中添加完全相同的东西。

希望这能有所帮助。