Skip to content

Instantly share code, notes, and snippets.

@Redth
Created February 1, 2011 20:11
Show Gist options
  • Select an option

  • Save Redth/806566 to your computer and use it in GitHub Desktop.

Select an option

Save Redth/806566 to your computer and use it in GitHub Desktop.
MonoTouch HTTPS Post
using System;
using MonoTouch.Foundation;
using MonoTouch.CoreFoundation;
using MonoTouch.UIKit;
using MonoTouch.ObjCRuntime;
namespace MyProject
{
public class Http
{
public static string HttpsPost(string url, string postData)
{
//Instantiate a NSMutableURLRequest
IntPtr nsMutableRequestPtr = Messaging.IntPtr_objc_msgSend(new Class("NSMutableURLRequest").Handle,
new Selector("new").Handle);
//Since NSMutableURLRequest subclasses NSUrlRequest, we can use NSURLRequest to work with
NSUrlRequest req = (NSUrlRequest)Runtime.GetNSObject(nsMutableRequestPtr);
//Set the url of the request
Messaging.void_objc_msgSend_IntPtr(req.Handle, new Selector("setURL:").Handle,
new NSUrl(url).Handle);
//Set the HTTP Method (POST)
Messaging.void_objc_msgSend_IntPtr(req.Handle,
new Selector("setHTTPMethod:").Handle,
new NSString("POST").Handle);
//Make a selector to be used twice
Selector selSetValueForHttpHeaderField = new Selector("setValue:forHTTPHeaderField:");
//Set the Content-Length HTTP Header
Messaging.void_objc_msgSend_IntPtr_IntPtr(req.Handle,
selSetValueForHttpHeaderField.Handle,
new NSString(postData.Length.ToString()).Handle,
new NSString("Content-Length").Handle);
//Set the Content-Type HTTP Header
Messaging.void_objc_msgSend_IntPtr_IntPtr(req.Handle,
selSetValueForHttpHeaderField.Handle,
new NSString("application/x-www-form-urlencoded").Handle,
new NSString("Content-Type").Handle);
//Make our c# string into a NSString of our post data
NSString sData = new NSString(postData);
//Now get NSData from that string using ASCII Encoding
NSData pData = new NSData(Messaging.IntPtr_objc_msgSend_int_int(sData.Handle,
new Selector("dataUsingEncoding:allowLossyConversion:").Handle, 1, 1));
//Set the HTTPBody, which is our POST data
Messaging.void_objc_msgSend_IntPtr(req.Handle,
new Selector("setHTTPBody:").Handle,
pData.Handle);
//Need to pass in a reference to the urlResponse object for the next method
IntPtr urlRespHandle = IntPtr.Zero;
//Send our request Synchronously
NSData dataResult = new NSData(Messaging.IntPtr_objc_msgSend_IntPtr_IntPtr_IntPtr(
new Class("NSURLConnection").Handle,
new Selector("sendSynchronousRequest:returningResponse:error:").Handle,
req.Handle, urlRespHandle, IntPtr.Zero));
//Get the urlResponse object
// Get this if you need it
//NSUrlResponse urlResp = (NSUrlResponse)Runtime.GetNSObject(urlRespHandle);
//Get ourselves a new NSString alloc'd, but not init'd
IntPtr resultStrHandle = Messaging.IntPtr_objc_msgSend(new Class("NSString").Handle,
new Selector("alloc").Handle);
//Init the NSString with our response data, and UTF8 encoding
resultStrHandle = Messaging.IntPtr_objc_msgSend_IntPtr_int(resultStrHandle,
new Selector("initWithData:encoding:").Handle, dataResult.Handle, 4);
//Finally, get our string result
return new NSString(resultStrHandle);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment