.NET code in the iPhone - Asynch Http Web Request - More Wally - Wallace B. McClure
in

MoreWally.com

Giving people what they want, More Wally. This is the technical and personal blog site of
Wallace B. (Wally) McClure.

This Blog

Syndication

News

Please goy buy 3-4 copies of my book on MonoTouch titled "Professional Android Programming with Mono for Android for .NET/C# Developers." They make great gifts all year round. Plus, I get about $.25 when you buy a copy.

Technical Sites

More Wally - Wallace B. McClure

This blog will have all kinds of posts about Wally McClure. In it, there will be tons of .NET and computer programming posts as well as Wally's views on life in general. As you might guess, this site and blog help you get More Wally in your life. What more could anyone want? iPhone, Android, MonoTouch, MonoDroid, Mobile, HTML5, .NET, ADO.NET, ASP.NET, AJAX, jQuery, jQuery Mobile, ASP.NET AJAX, and Windows Azure............follow me on twitter at Wally

.NET code in the iPhone - Asynch Http Web Request

 I just wrote the following code and ran it in the iPhone simulator on my mac using MonoDevelop and MonoTouch.  With this code, I wrote it in .NET and then opened it in MonoDevelop, massaged appropiately, and then built it.  It ran!  Very cool.  Here's a copy of the code from VS.NET.  It ran perfect in the MonoDevelop IDE and the iPhone simulator.  A word of warning, running in the simulator is no guarantee it will run in a physical iPhone or iPod Touch.

Code:

            string Url = "http://somewhere";
            string Body = "{\"ld\":{\"UserName\":\"blah\",\"PassWord\":\"blah\",\"AppKey\":\"blah\"}}";
            byte[] byteData = UTF8Encoding.UTF8.GetBytes(Body);
            try
            {
                // Create the web request
                HttpWebRequest request = WebRequest.Create(Url) as HttpWebRequest;
                request.ContentLength = Body.Length;

                // Set type to POST
                request.Method = "POST";
                request.ContentType = "text/json";
               
                // Write the parameters
                StreamWriter stOut = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
                stOut.Write(Body);
                stOut.Close();
              
                request.BeginGetResponse(new AsyncCallback(ProcessHttpResponse), request);
            }
            catch (WebException we)
            {
                Console.Error.WriteLine("Exception: " + we.Message);
            }
            catch (System.Exception sysExc)
            {
                Console.Error.WriteLine("Exception: " + sysExc.Message);
            }
 

Here is the callback:

        private void ProcessHttpResponse(IAsyncResult iar)
        {
            HttpWebRequest request = (HttpWebRequest)iar.AsyncState;
            HttpWebResponse response;
            response = (HttpWebResponse)request.EndGetResponse(iar);
            Console.Error.WriteLine("get response.");
            System.IO.StreamReader strm = new System.IO.StreamReader(
                response.GetResponseStream());
            string responseString = strm.ReadToEnd();
            response.Close();
            Console.Error.WriteLine("response: " + responseString);
        }
 

Using statements from my MonoTouch app:

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Linq;
using System.Text;
using System.Xml;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

Comments

 

MonoTouch.Info said:

Thank you for submitting this entry - Trackback from MonoTouch.Info

September 1, 2009 6:27 PM
 

Dew Drop – September 2, 2009 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop – September 2, 2009 | Alvin Ashcraft's Morning Dew

September 2, 2009 8:59 AM
 

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

September 3, 2009 9:21 AM
2006 - Wallace B. McClure
Powered by Community Server (Non-Commercial Edition), by Telligent Systems