MonoTouch: HTTPS POST

1

Category : MonoTouch, iPhone

Here’s some code binding to do a HTTPS POST in MonoTouch with the unbound NSMutableURLRequest, since the HTTPS in the MonoTouch stack is not yet working.  You should call the method like: “Http.HttpsPost(“http://url.com”, “var1=something&var2=anotherthing”); ” where the second argument is a querystring format of the post variables you want to send.  Here’s the code:

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);
}

}
}
SEO Powered by Platinum SEO from Techblissonline