[MonoTouch] HttpWebRequest behavior differ from "pure" .NET

thecoachfr alexandre.fau at gmail.com
Fri Oct 14 17:20:24 EDT 2011


Hi, 

I'm trying to implement a request to a web site with login in order to
retrieve some html data. 

I have a bunch of code that actually works well in .NET (I means I can login
to the web site and have the expect result). 
When I try the exact same code with monotouch (from the same computer) the
request never works. 

Basically I do this : 
           public static void Main() 
       { 
           CredentialCache cCache = new CredentialCache(); 
           var cookies = new CookieContainer(); 
           Connect("http://myWebSite:90/", cCache, cookies); 
           Connect("http://myWebSite:90/login.php", cCache, cookies); 
           Connect("http://myWebSite:90/settings.php", cCache, cookies); 
           Console.ReadLine(); 
       } 

       private static void Connect(String url, CredentialCache 
cCache, CookieContainer cookies) 
       { 
           HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
           // Set the Method property of the request to POST. 
           request.Method = "POST"; 
           // Create POST data and convert it to a byte array. 
           string postData = "login=login&passwd=passwd"; 
           byte[] byteArray = Encoding.UTF8.GetBytes(postData); 
           // Set the ContentType property of the WebRequest. 
           request.ContentType = "application/x-www-form-urlencoded"; 
           // Set the ContentLength property of the WebRequest. 
           request.ContentLength = byteArray.Length; 
           request.CookieContainer = cookies; 
           request.Accept = 
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; 
           request.UserAgent = "Mozilla/5.0 (Windows NT 6.1;
WOW64)AppleWebKit/535.1(KHTML, like Gecko) Chrome/13.0.782.112Safari/535.1"; 
           request.Credentials = cCache; 
           request.AllowAutoRedirect = false; 
           request.KeepAlive = true; 
           request.MaximumAutomaticRedirections = 50; 
           // Get the request stream. 
           Stream dataStream = request.GetRequestStream(); 
           dataStream.Write(byteArray, 0, byteArray.Length); 
           dataStream.Close(); 
           // Get the response. 
           HttpWebResponse response =
(HttpWebResponse)request.GetResponse(); 
           cookies.Add(response.Cookies); 
           cookies.Add(new Uri("http://myWebSite:90/settings.php"),
response.Cookies); 
           cookies.Add(new Uri("http://myWebSite:90/login.php"),
response.Cookies); 
           cookies.Add(new Uri("http://myWebSite:90/"), response.Cookies); 
           // Get the stream containing content returned by the server. 
           dataStream = response.GetResponseStream(); 
           StreamReader reader = new StreamReader(dataStream); 
           string responseFromServer = reader.ReadToEnd(); 
           // Display the content. 
           Console.WriteLine("resp = " + responseFromServer); 
           // Clean up the streams. 
           reader.Close(); 
           dataStream.Close(); 
           response.Close(); 
       } 
   } 

PS: I had some redirect trouble that why I handle it by my self. 

When it works (with .NET on windows) that code display some html code. 
With mono the login process never succeed and of course I have no answer. 
I have the latest mono version (downloaded yesterday). 

I real don't understand why I can't make mono works with my code ... 

Any ideas ? 

Thanks for you help 

@lex 

--
View this message in context: http://monotouch.2284126.n4.nabble.com/HttpWebRequest-behavior-differ-from-pure-NET-tp3906260p3906260.html
Sent from the MonoTouch mailing list archive at Nabble.com.


More information about the MonoTouch mailing list