使用ASP.NET与C#HttpWebRequest登录到无法正常工作的站点

时间:2013-10-15 08:53:37

标签: c# asp.net httpwebrequest

我一直在尝试以编程方式登录过去几天的网站,没有运气。我一直在寻找解决方案的论坛,虽然许多人有同样的问题和解决方案,我似乎无法让它在我自己的网站上工作。 问题是,如果我想显示登录的响应,我只是获得相同的登录页面,如果我在浏览器中输入我的用户名和密码,我就不会登录

我一直在使用fiddler来获取看起来像这样的原始标题:

POST /Login.aspx HTTP / 1.1 主持人:basystest.basket.dk 连接:保持活力 内容长度:558 接受:text / html,application / xhtml + xml,application / xml; q = 0.9,image / webp, / ; q = 0.8 来源:http://basystest.basket.dk User-Agent: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 推荐人:http://basystest.basket.dk/Login.aspx Accept-Encoding:gzip,deflate,sdch Accept-Language: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();
    }
}

我希望有人可以提供帮助 - 这可能是一个简单的解决方案,但我无法弄清楚我做错了什么

提前致谢

/彼得

0 个答案:

没有答案