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;