使用ASP.NET和C#HttpWebRequest登录到不工作的站点

使用ASP.NET和C#HttpWebRequest登录到不工作的站点,c#,asp.net,httpwebrequest,C#,Asp.net,Httpwebrequest,在过去的几天里,我一直在尝试以编程方式登录到一个站点,但运气不佳。我一直在论坛上寻找解决方案,虽然很多人都有相同的问题和解决方案,但我似乎无法在自己的网站上找到解决方案。 问题是,如果我想显示登录的响应,我只会得到相同的登录页面,而不会像在浏览器中键入用户名和密码那样登录 我一直在使用fiddler获取原始标题,如下所示: POST/Login.aspx HTTP/1.1 主持人:basystest.basket.dk 连接:保持活力 内容长度:558 接受:text/html、applicat

在过去的几天里,我一直在尝试以编程方式登录到一个站点,但运气不佳。我一直在论坛上寻找解决方案,虽然很多人都有相同的问题和解决方案,但我似乎无法在自己的网站上找到解决方案。 问题是,如果我想显示登录的响应,我只会得到相同的登录页面,而不会像在浏览器中键入用户名和密码那样登录

我一直在使用fiddler获取原始标题,如下所示:

POST/Login.aspx HTTP/1.1 主持人:basystest.basket.dk 连接:保持活力 内容长度:558 接受:text/html、application/xhtml+xml、application/xml;q=0.9,图像/webp,/;q=0.8 来源: 用户代理:Mozilla/5.0(Windows NT 6.2;WOW64)AppleWebKit/537.36(KHTML,如Gecko)Chrome/30.0.1599.69 Safari/537.36 内容类型:application/x-www-form-urlencoded 推荐人: 接受编码:gzip、deflate、sdch 接受语言:en-US,en;q=0.8 Cookie:ASP.NET_SessionId=5vdxpjkgldncbgugzs4wpf44

我正在尝试使用以下代码登录到以下站点:basystest.basket.dk:

public void main()
{
    //We need a container to store the cookies in.
    CookieContainer cookies = new CookieContainer();

    //Request login page to get a session cookie
    GETHtml(basysURL, cookies);

    //Now we can do login
    textbox.Text = Login(username, password, cookies).ToString();


}

public bool Login(string Username, string Password, CookieContainer cookies)
{
    string poststring = string.Format("txtUserName={0}&txtPassword={1}&butLogin=submit",
                                Username, Password);
    byte[] postdata = Encoding.UTF8.GetBytes(poststring);

    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(basysURL);
    webRequest.CookieContainer = cookies;
    webRequest.Host = "basystest.basket.dk";
    webRequest.KeepAlive = true;
    webRequest.Method = "POST";
    webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
    webRequest.Referer = "http://basystest.basket.dk/Login.aspx";
    webRequest.Headers.Add("origin", "http://basystest.basket.dk");
    webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36";
    webRequest.ContentType = "application/x-www-form-urlencoded";
    webRequest.ContentLength = postdata.Length;
    using (Stream writer = webRequest.GetRequestStream())
        writer.Write(postdata, 0, postdata.Length);

    using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
    {
        //We need to add any response cookies to our cookie container
        cookies.Add(webResponse.Cookies);

        //Only for debug
        using (var stream = new StreamReader(webResponse.GetResponseStream()))
        {
            String result = stream.ReadToEnd();
            System.Diagnostics.Debug.WriteLine(result);
            label.Text = result;
        }
        return (webResponse.StatusCode == HttpStatusCode.OK);
    }

}

public string GETHtml(string url, CookieContainer cookies)
{
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
    webRequest.CookieContainer = cookies;
    webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.5.21022;";
    webRequest.Referer = "http://basystest.basket.dk/Login.aspx";

    using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
    {
        //We need to add any response cookies to our cookie container           
        cookies.Add(webResponse.Cookies);

        using (var stream = new StreamReader(webResponse.GetResponseStream()))
            return stream.ReadToEnd();
    }
}
我希望有人能帮忙——这可能是一个简单的解决方案,但我不知道我做错了什么

提前谢谢


/Peter

编辑您的问题,以包括任何异常的详细信息以及调试期间收集的任何信息。会发生什么?您会遇到什么错误?butLogin不应该是“登录”而不是“提交”吗?或者它可能是无关紧要的…我没有得到任何错误,我只是得到了与我登录的页面相同的返回页面。我希望在使用浏览器时接收重定向到的页面。代码可能没有错误,但我需要知道如何导航到新页面?@Colandus,我只是尝试更改它,没有区别