首页 新闻 会员 周边 捐助

这个网站的POST登录该如何写

0
悬赏园豆:10 [已解决问题] 解决于 2012-08-31 19:49

http://rookiesite.com/node?destination=node

 

尝试了几个办法 都失败了 不知道错在哪里 求解

 

 public CookieCollection cookie = new CookieCollection();
string post(string url, string postData)
{
    ASCIIEncoding encoding = new ASCIIEncoding();
    byte[] bytesToPost = encoding.GetBytes(postData);
    CookieContainer cookieCon = new CookieContainer();
    if (cookie.Count > 0)
    {
        cookieCon.Add(new Uri(url), cookie);
    }
    HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
    httpRequest.CookieContainer = cookieCon;
    httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
    httpRequest.ContentType = "application/x-www-form-urlencoded";
    httpRequest.Method = "POST";
    httpRequest.ContentLength = bytesToPost.Length;
    Stream requestStream = httpRequest.GetRequestStream();
    requestStream.Write(bytesToPost, 0, bytesToPost.Length);
    requestStream.Close();
    Stream responseStream = httpRequest.GetResponse().GetResponseStream();
    string stringResponse = string.Empty;
    StreamReader responseReader = new StreamReader(responseStream, Encoding.UTF8);
    stringResponse = responseReader.ReadToEnd();
    responseStream.Close();
    cookie = httpRequest.CookieContainer.GetCookies(new Uri(url));
    return stringResponse;
}
public CookieCollection cookie = new CookieCollection();
string post(string url, string postData)
{
    ASCIIEncoding encoding = new ASCIIEncoding();
    byte[] bytesToPost = encoding.GetBytes(postData);
    CookieContainer cookieCon = new CookieContainer();
    if (cookie.Count > 0)
    {
        cookieCon.Add(new Uri(url), cookie);
    }
    HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
    httpRequest.CookieContainer = cookieCon;
    httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
    httpRequest.ContentType = "application/x-www-form-urlencoded";
    httpRequest.Method = "POST";
    httpRequest.ContentLength = bytesToPost.Length;
    Stream requestStream = httpRequest.GetRequestStream();
    requestStream.Write(bytesToPost, 0, bytesToPost.Length);
    requestStream.Close();
    Stream responseStream = httpRequest.GetResponse().GetResponseStream();
    string stringResponse = string.Empty;
    StreamReader responseReader = new StreamReader(responseStream, Encoding.UTF8);
    stringResponse = responseReader.ReadToEnd();
    responseStream.Close();
    cookie = httpRequest.CookieContainer.GetCookies(new Uri(url));
    return stringResponse;
}

这两个办法POST后返回的还是需要登录  求解

shineme的主页 shineme | 初学一级 | 园豆:29
提问于:2012-08-26 12:16
< >
分享
最佳答案
0

post 方法没有问题,问题可能出在header上面,例如缺少referer,cookie等,建议查看下用浏览器登录时的post请求header

收获园豆:10
喵喵喵猫 | 小虾三级 |园豆:1742 | 2012-08-26 14:46

不行  还是失败了 这个之前考虑过的

shineme | 园豆:29 (初学一级) | 2012-08-26 18:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册