使用c#

时间:2018-05-07 16:02:07

标签: c# httpwebrequest httpwebresponse

我正在尝试使用HttpWebRequest登录我们的一个内部网站以使用一个小程序。您可以找到我在This Question中找到的以下代码来登录该网站。请根据我们的网站在下面找到我更改的代码。

class LoginSite
{
    CookieContainer Cookies = new CookieContainer();

   public void Main()
    {
        Login();

        // Now the cookies in "Cookies" are all set.
        // Ensure you set CookieContainer on all subsequent requests
    }

    void Login()
    {
        var wr = (HttpWebRequest)WebRequest.Create("http://portal.test.com/Login.aspx");
        wr.Method = "POST";
        wr.ContentType = "application/x-www-form-urlencoded";
        wr.Referer = "http://portal.test.com/Login.aspx"; // my tests show this is needed
        wr.CookieContainer = Cookies;

        var parameters = new Dictionary<string, string>{
        //{"__LASTFOCUS", ""},
        //{"__EVENTTARGET",""},
        // {"__EVENTARGUMENT",""},
        //{"__VIEWSTATE","INdS1F45fzWYVwKD8b1b5x5Mr09nYrvS3hrSO4EAXFHVwtN76gSoToydwoOlIf1nI+KIxVR5CWiN2c5NOPO7kjTW7DbiNG2ZZ5Mpd0pYvN3tt68I6MvZ1dGHMJFhgJQ1YYpQQ+V6oonOrncJ6lwk69LaPVuSUSAPSubgSBoNYLfEy28kHirrNNc0uburWHQ9DK0zHwwod8gWKS8sz+qSjWOzV0PZyX7HhMQjc4MAD+RkHQMgeyOMqghODYn2ZW5mCBcY34ISL328aPGZmb6XERo+9LArRwASFBNqyFygZ1YJaUwReOdceEpTtg6TEOJb"},
        //{"__VIEWSTATEGENERATOR", "A43BD702"},
        //{"__EVENTVALIDATION","ngIch5+O80dcLktVDBbT/QqkZ8TvAm/HNMQVR54bGv0oQRxd2LqBISWhjdwgvwjnbaGSBAFDFcuvCgmdBvAJ/eBUSCNB9vMGfPzhecbdCFcGVzKy7/d49KNK/f8D+QBrT912wDav1/LFqFn32Op+HVOj2qLwuuvRLF+v4NL/xhpu22QNXXO/B4r/OAYJgtFYlZCg1k4jO7QacNdjaoJ02KVj0xBm9fWbr02spbCUld7+fY9shCyEcthLpEag8OryqKQlYtDgx8UxCc6bj5GKQw=="},
        {"ctl00$cphBody$LogIn1$txtUserName", "testuser"},
        {"ctl00$cphBody$LogIn1$vceUserName_ClientState", ""},
        {"ctl00$cphBody$LogIn1$txtPassword", "testpwd"},
         {"ctl00$cphBody$LogIn1$vecPassword_ClientState", ""},
          {"ctl00$cphBody$LogIn1$btnSubmit", "Submit"}
    };

        using (var requestStream = wr.GetRequestStream())
        using (var writer = new StreamWriter(requestStream, Encoding.UTF8))
            writer.Write(ParamsToFormEncoded(parameters));

        using (var response = (HttpWebResponse)wr.GetResponse())
        {
            // here you need to detect a correct login... this might be one of the cookies.
            // if incorrect throw an exception or something.
            using (StreamReader sr = new StreamReader(response.GetResponseStream()))
            {
              var  result = sr.ReadToEnd();
            }
        }
    }

    string ParamsToFormEncoded(Dictionary<string, string> parameters)
    {
        return string.Join("&", parameters.Select(kvp =>
            Uri.EscapeDataString(kvp.Key).Replace("%20", "+") + "=" + Uri.EscapeDataString(kvp.Value).Replace("%20", "+")
        ).ToArray());
    }
}

我已将确切的表单值添加到参数中,我在获得响应时没有得到任何异常,但仍然获得的Cookie计数为零。任何人都可以帮助我,我已经花了很多时间在这上面。以下是表格数据的屏幕截图。enter image description here

1 个答案:

答案 0 :(得分:0)

我可以在取消注释LASTFocus,__ EVENTTARGET,__ EVENTARGUMENT,__ VIEWSTATE,__ VIEWSTATEGENERATOR,__ EVENTVALIDATION并添加wr.AllowAutoRedirect = false时获取新的cookie。但我没有得到新的重定向页面,即http://portal.test.com/Home.aspx作为回应。相反,我得到像对象移动

的响应

对象已移至here