2012-03-24 68 views
-1

我想在这个网站发一个帖子请求:http://www.prezup.info/index.php?page=films为了做一个研究。但是,当我发布帖子请求时,结果不是我所期望的(研究结果),而是主页面。为什么这个POST请求不起作用?

有人能告诉我我的要求有什么问题吗?

下面是代码:日期是POST请求ARG和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; 
    } 
+0

不是你的问题的答案。 1.你知道你甚至没有使用StreamWriter对象吗? 2.使用'using'语句处理一次性对象,并[优雅地执行](http://msdn.microsoft.com/en-us/library/yh598w02.aspx)。 – 2012-03-24 13:56:07

+0

它应该工作。你收到什么样的错误? – 2012-03-24 14:08:52

+0

如果响应不符合您的期望,那么您必须研究网站是否向浏览器发送了其他内容以及对您的不同响应。使用wireshark来查看通常会有什么回应。你只会得到网站发送的内容,而不是你认为应该得到的内容 – ata 2012-03-24 14:20:01

回答

0

这表POST请求http://www.prezup.info/index.php?page=films&action=list,让您的网址不正确。更改:

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

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

,它会工作。

+0

那是错的。万分感谢! – 2012-03-24 14:20:22

+0

@JérémieDjidjiL'Amoroso,yw。 – 2012-03-24 14:35:54