C# 使用c中的凭据登录到第三方网站#

C# 使用c中的凭据登录到第三方网站#,c#,asp.net,C#,Asp.net,我想使用凭据登录到第三方网站。 下面是我通过stackoverflow使用的代码,但是它重定向到登录页面,我认为会话并没有存储如何存储会话 //The URL of the Login Form of the website String urlSignin = "https://www.grouprevmax.com/WebMARDSV2/RevenueCaterSuper/RevenueManager.aspx"; //The action URL of the L

我想使用凭据登录到第三方网站。 下面是我通过stackoverflow使用的代码,但是它重定向到登录页面,我认为会话并没有存储如何存储会话

    //The URL of the Login Form of the website
    String urlSignin = "https://www.grouprevmax.com/WebMARDSV2/RevenueCaterSuper/RevenueManager.aspx";

    //The action URL of the Login Form of the website on Submit
    String urlLogin = "https://www.grouprevmax.com/WebMARDSV2/RevenueCaterSuper/RevenueManager.aspx";

    //Initializes the Uri object of the URLs
    Uri uriSignin = new Uri(urlSignin);
    Uri uriLogin = new Uri(urlLogin);

    //Hashtable to store the form details
    Hashtable formData = new Hashtable();
    formData.Add("UTC", new Hashtable());
    formData.Add("txtUserName", new Hashtable());
    formData.Add("txtPassword", new Hashtable());

    ((Hashtable)formData["UTC"])["value"] = -330;
    ((Hashtable)formData["txtUserName"])["value"] = username;
    ((Hashtable)formData["txtPassword"])["value"] = password;

    //Initializing the data for the post action
    String postData = "";

    foreach (string name in formData.Keys)
    {
        postData += "&" + name + "=" + ((Hashtable)formData[name])["value"];
    }
    postData = postData.Substring(1);

    System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
    byte[] data = encoding.GetBytes(postData);

    HttpWebRequest webReq;
    HttpWebResponse webResp;

    //To store the cookies of the response objects to be used for the next request
    CookieContainer cookies = new CookieContainer();

    String responseString = "";

    try
    {
        //Getting response for the Signin page  
        webReq = (HttpWebRequest)WebRequest.Create(urlSignin);
        webResp = (HttpWebResponse)webReq.GetResponse();

        //Storing response cookies to be used in the next request
        cookies.Add(webResp.Cookies);

        //Storing ASPSESSION cookie that appears in the Response header Set-Cookie to be used in the next request
        string sessionCookie = webResp.Headers["Set-Cookie"];
        responseString = new StreamReader(webResp.GetResponseStream()).ReadToEnd();

        string respCookie = sessionCookie.Substring(0, sessionCookie.IndexOf(';'));
        char[] separator = { '=' };
        string[] cookieValues = respCookie.Split(separator);
        cookies.Add(new Cookie(cookieValues[0], cookieValues[1], "/", "www.grouprevmax.com"));

        //Initializing the request object for log in
        webReq = (HttpWebRequest)WebRequest.Create(urlLogin);
        webReq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        webReq.Referer = urlSignin;
        webReq.KeepAlive = true;
        webReq.Method = "POST";
        webReq.ContentType = "application/x-www-form-urlencoded";
        webReq.ContentLength = data.Length;
        webReq.AllowAutoRedirect = false;
        webReq.CookieContainer = cookies;
        webReq.Timeout = 30000;
        webReq.ReadWriteTimeout = 60000;



        //Get the response for the request to log in
        //PROBLEM OCCURS HERE - THE CODE DOES NOT EXECUTE FURTHER
        webReq.GetRequestStream().Write(data, 0, data.Length);
        webReq.GetRequestStream().Close();
        webResp = (HttpWebResponse)webReq.GetResponse();
        responseString = new StreamReader(webResp.GetResponseStream()).ReadToEnd();

        webResp.Close();
        return responseString;

您应该能够将
CookieContainer
与从
webResp
收集的cookie一起使用

cookies.Add(webResp.Cookies);
您不应该处理cookies,因为您低于此值。只需在新请求上设置
CookieContainer
就足够了

webReq.CookieContainer = cookies;

在第三方网站中,他们可能会通过我们加载页面时生成的一些动态参数传递凭据。因此,在此之前,请使用
firebug
fiddler
分析该网站,确定他们在登录时传递的参数,并尝试传递这些参数。这种方式可以帮助您尝试使用Fiddler查看与web服务器的交换。这有助于诊断确切的问题;webReq.AllowAutoRedirect=false;cookies.Add(webResp.cookies);//webReq.CookieContainer=cookies;webReq.Timeout=30000;webReq.ReadWriteTimeout=60000;但它仍然不起作用