Faccio una richiesta "post" per scaricare una pagina web:
Mi restituisce la seguente eccezione:"ProtocolViolationException"codice:public string getPage() { String sourceFile; HttpWebRequest r = (HttpWebRequest)HttpWebRequest.Create("http://xxx.it/Login.aspx"); ASCIIEncoding encoding = new ASCIIEncoding(); string postData = "username=ilario"; postData += "&password=maiolo"; byte[] data = encoding.GetBytes(postData); r.Method = "POST"; r.ContentType = "application/x-www-form-urlencoded"; r.ContentLength = data.Length; HttpWebResponse r2 = (HttpWebResponse)r.GetResponse(); StreamReader s = new StreamReader(r2.GetResponseStream()); sourceFile = s.ReadToEnd(); s.Close(); r2.Close(); return sourceFile; }
Come deve essere gestita per evitare questo problema?codice:È necessario specificare il corpo di una richiesta se si imposta ContentLength>0 o SendChunked==true. A tale scopo, chiamare [Begin]GetRequestStream prima di [Begin]GetResponse.

Rispondi quotando