[MonoTouch] HttpWebRequest behavior differ from "pure" .NET
Nic Wise
nicw at fastchicken.co.nz
Tue Oct 18 05:51:03 EDT 2011
What happens if you run it in mono on your mac?
Could isolate it between a windows vrs mono vrs monotouch issue.
On Fri, Oct 14, 2011 at 22:20, thecoachfr <alexandre.fau at gmail.com> wrote:
> 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.
> _______________________________________________
> MonoTouch mailing list
> MonoTouch at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/monotouch
>
--
Nic Wise
t. +44 7788 592 806 | @fastchicken | http://www.linkedin.com/in/nicwise
b. http://www.fastchicken.co.nz/
Nearest Bus: find when the next bus is coming to your stop. http://goo.gl/Vcz1p
mobileAgent (for FreeAgent): get your accounts in your pocket.
http://goo.gl/IuBU
Trip Wallet: Keep track of your budget on the go: http://goo.gl/ePhKa
London Bike App: Find the nearest Boris Bike, and get riding! http://goo.gl/Icp2
More information about the MonoTouch
mailing list