【问题标题】:Why this POST request doesn't work?为什么这个 POST 请求不起作用?
【发布时间】:2012-03-24 13:43:53
【问题描述】:

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

谁能告诉我我的请求有什么问题?

这里是代码:date 是 post 请求 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. 你知道你甚至没有使用那个 StreamWriter 对象吗? 2. 处理一次性物品,使用using语句do it elegantly
  • 它应该可以工作。您收到什么样的错误?
  • 如果响应不是您所期望的,那么您必须研究该网站是否正在向浏览器发送其他内容以及向您发送不同的响应。使用wireshark查看通常会出现什么响应。你只会得到网站发送的东西,而不是你认为应该得到的东西
  • 你是对的,但事实上,我认为 "&ok.x=17&ok.y=16" 有问题。我不知道它到底是什么。我完全错了。谢谢你的提示

标签: c# http-post


【解决方案1】:

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

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

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

它会起作用的。

【讨论】:

    猜你喜欢
    • 2018-06-05
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    相关资源
    最近更新 更多