如何在iOS中以url的形式发布数据? [英] How to post data in the form of url in iOS?

查看:114
本文介绍了如何在iOS中以url的形式发布数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的UITextfields数据发送到服务器。

I want to send my UITextfields data to a server.

我想发布数据,但服务器向我显示错误消息。

I want to post data but the server showing error message to me.

请检查我的代码:

  ...

  NSURL *url=[NSURL URLWithString:@"http://projectsatseoxperts.net.au/fishing/api/postRegister.php"];

  NSString *post =[[NSString alloc] initWithFormat:@"FirstName=%@&LastName=%@userName=%@&Email=%@Phone=%@&Address=%@Password=%@&ConfirmPassword=%@",
    txt_firstname.text,txt_lastname.text,txt_username.text,txt_email.text,txt_phone.text,txt_address.text,txt_password.text,txt_confirmpassword.text];
  NSLog(@"Post is: %@",post);

  NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
  NSLog(@"postData is: %@",postData); 

  NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
  NSLog(@"postLength is: %@",postLength);

  NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  [request setURL:url];
  [request setHTTPMethod:@"POST"];
  [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
  //[request setValue:@"http://projectsatseoxperts.net.au/fishing/api/postRegister.php" forHTTPHeaderField:@"Content-Type"];
  [request setHTTPBody:postData];

  NSLog(@"request is: %@", [request allHTTPHeaderFields]);
  NSError *error;
  NSURLResponse *response;
  NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
  NSLog(@"urlData is: %@",urlData);

  NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
  NSLog(@"%@",data);
}

发布详细信息后,价值将来到这里 - http://projectsatseoxperts.net.au/fishing/api/register.php

After post the details the values will come here - http://projectsatseoxperts.net.au/fishing/api/register.php

非常欢迎任何想法或建议。

Any idea or suggestions would be highly welcome.

推荐答案

您的要求是正确的..只是检查您的帖子数据的内容类型和编码。另外,请咨询服务器团队,了解他们期望的确切数据格式..

Your Request is proper.. just check the content Type and Encoding of your post data.. Also do consult with the server team for exact data format they expect from you..

这篇关于如何在iOS中以url的形式发布数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆