我想在這個網站發一個帖子請求: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;
}
不是你的問題的答案。 1.你知道你甚至沒有使用StreamWriter對象嗎? 2.使用'using'語句處理一次性對象,並[優雅地執行](http://msdn.microsoft.com/en-us/library/yh598w02.aspx)。 – 2012-03-24 13:56:07
它應該工作。你收到什麼樣的錯誤? – 2012-03-24 14:08:52
如果響應不符合您的期望,那麼您必須研究網站是否向瀏覽器發送了其他內容以及對您的不同響應。使用wireshark來查看通常會有什麼迴應。你只會得到網站發送的內容,而不是你認爲應該得到的內容 – ata 2012-03-24 14:20:01