更改后接收URL

时间:2009-12-24 22:36:59

标签: c# url uri

在www.vkontakte.ru上授权通过ie8 me跨越页面:www.vkontakte.ru/MyPage。但我无法通过代码

收到www.vkontakte.ru/MyPage
        HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(new Uri("http://vkontakte.ru/login.php", UriKind.Absolute));
        authRequest.CookieContainer = new CookieContainer();
        authRequest.AllowAutoRedirect = false;
        string param = string.Format("email={0}&pass={1}&expire=1", HttpUtility.UrlEncode("---"), HttpUtility.UrlEncode("---"));
        authRequest.Method = "POST";
        authRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 3.5.30729)";
        authRequest.ContentType = "application/x-www-form-urlencoded";
        authRequest.ContentLength = param.Length;
        authRequest.GetRequestStream().Write(Encoding.GetEncoding(1251).GetBytes(param), 0, param.Length);

HttpWebResponse authResponse = (HttpWebResponse)authRequest.GetResponse(); 
listBox1.Items.Add(authRequest.Address); 

返回http://vkontakte.ru/而不是www.vkontakte.ru/MyPage =( HttpContext.Current.Request.Url.AbsoluteUri - 可以帮忙吗? 帮助我!

1 个答案:

答案 0 :(得分:2)

您忘记关闭请求流。

您应该写下以下内容:

using (Stream requestStream = authRequest.GetRequestStream())
using (StreamWriter writer = new StreamWriter(requestStream, Encoding.GetEncoding(1251))
    writer.Write(param);

此外,您应该运行Fiddler检查请求和响应的内容。