如何使用MonoTouch在iOS中的post请求中发送表单数据?

时间:2013-11-26 05:08:08

标签: ios iphone objective-c xamarin.ios

我正在尝试在MonoTouch中发布一个帖子请求以及我的表单数据,我在Objective-C中得到了答案但是在MonoTouch中无法做到这一点。
这是objC代码:

  NSData* postData= [<yourJSON> dataUsingEncoding:NSUTF8StringEncoding];
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  [request setHTTPMethod:@"POST"];
  [request setValue:[NSString stringWithFormat:@"%d", postData.length] forHTTPHeaderField:@"Content-Length"];
  [request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
  [request setHTTPBody:postData];

  NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
  [connection start];

任何正文都可以帮助我将此代码转换为单声道触控代码。

1 个答案:

答案 0 :(得分:1)

由于Xamarin.iOS(MonoTouch)允许您使用.NET Framework,因此您可以像使用桌面C#应用程序一样执行此操作。有几种不同的方法,但WebClient有一种方法:

   using (WebClient client = new WebClient())
   {

       byte[] response = client.UploadValues("http://test.com/endpoint", new NameValueCollection()
       {
           { "name", "value" },
           { "name", "value" }
       });
   }

搜索“C#帖子表格数据”应该会有很多其他例子。

相关问题