为什么这个POST请求不起作用?

时间:2012-03-24 13:43:53

标签: c# http-post

我想在此网站发布帖子请求:http://www.prezup.info/index.php?page=films以便进行研究。但是,当我发布请求时,结果不是我所期望的(研究结果),而是主页。

有人可以告诉我我的请求有什么问题吗?

以下是代码:date是post request arg,url是url

public string POST()
  {
       string data = "motcle=terminator&ok.x=17&ok.y=16";
        string Reponse = String.Empty;
        string contenttype = "application/x-www-form-urlencoded";
        string useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 ( .NET CLR 3.5.30729; .NET4.0E)";
        string host = "www.prezup.info";
        string url = "http://www.prezup.info/index.php?page=films";
        string method = "POST";

        StreamWriter Sw = null; // Pour écrire les données
        StreamReader Sr = null; // Pour lire les données
        try
        {
            HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(url);
            Req.Method = method; // POST ou GET
            Req.Host = host;
            Req.KeepAlive = true;
            Req.UserAgent = useragent;
            Req.CookieContainer = cookieJar;

            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] byte1 = encoding.GetBytes(data);
            Req.ContentType = contenttype;
            Req.ContentLength = byte1.Length; // La longueur des données
            Stream newStream = Req.GetRequestStream();
            newStream.Write(byte1, 0, byte1.Length);
            newStream.Close();

            Sr = new StreamReader(((HttpWebResponse)Req.GetResponse()).GetResponseStream());
            Reponse = Sr.ReadToEnd(); // On choppe la réponse
            int cookieCount = cookieJar.Count;
            Sr.Close(); // Et on ferme
            Sw = null;
        }
        catch (Exception e) // En cas d'exception
        {
            if (Sw != null) // Si le flux est ouvert, on le ferme
                Sw.Close();
            if (Sr != null)
                Sr.Close();

            Reponse = e.Message;
        }
        return Reponse;
    }

1 个答案:

答案 0 :(得分:0)

该表单向http://www.prezup.info/index.php?page=films&action=list发出POST请求,因此您的网址不正确。变化:

string url = "http://www.prezup.info/index.php?page=films";

string url = "http://www.prezup.info/index.php?page=films&action=list";

它会起作用。