NSString urlstring = new NSString("http://211.155.229.230:8585/api/users/postregister");
NSMutableUrlRequest request = new NSMutableUrlRequest(new NSUrl(urlstring));
request.Url = new NSUrl(urlstring);
request.HttpMethod = "POST";
NSUrlConnection connection = new NSUrlConnection(request, new ConnectionDelegate((response) =>
{
//解析数据
JsonValue jsonObj = JsonValue.Parse(response);
}));
////ConnectionDelegate
using System;
using Foundation;
using System.Text;
namespace XinPeiOA
{
public class ConnectionDelegate : NSUrlConnectionDataDelegate
{
public ConnectionDelegate()
{
}
private Action<string> finishedCallback;
private StringBuilder responseData;
//构造方法
public ConnectionDelegate(Action<string> callback)
{
this.finishedCallback = callback;
this.responseData = new StringBuilder();
}
//结束数据后的相关操作
public override void ReceivedData(NSUrlConnection connection, NSData data)
{
//判断数据是否存储
if (null != data)
{
this.responseData.Append(data.ToString());
}
}
///当请求结束后的相关操作
public override void FinishedLoading(NSUrlConnection connection)
{
if (null != this.finishedCallback)
{
this.finishedCallback(this.responseData.ToString());
}
this.responseData.Clear();
}
//当请求失败时的相关操作
public override void FailedWithError(NSUrlConnection connection, NSError error)
{
//判断是否有错误
if (null != error)
{
Console.WriteLine("Error in connection! { 0}", error.LocalizedDescription);
}
}
}
}