gpt4 book ai didi

iphone - 如何向服务器写入数据?

转载 作者:行者123 更新时间:2023-11-29 04:56:39 25 4
gpt4 key购买 nike

我正在制作一个注册页面,用户可以注册吗?这是我的代码..............

-(IBAction)btn_log:(id)sender
{

//NSString* username = nameInput.text;
//NSString* pass = passInput.text;

NSString* firstname = nameInput.text;
NSString* lastname = passInput.text;
NSString* bname = lastInput.text;


NSString *post =
[[NSString alloc] initWithFormat:@"fname=%@&lname=%@&email=%@",firstname,lastname,bname];

//NSString *post = [[NSString alloc]initWithFormat:@"fname=firstname&lname=lastname&email=bname];

NSData * postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString * postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest * request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.yoursite.com/file.php"]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if (conn) NSLog(@"Connection Successful");
}

但是它不起作用,所以请有人建议我哪里出错了。

最佳答案

试试这个方法也许会对你有帮助

Nsurl *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.yoursite.com/file.php"]]];nsstring *body = [nsstring stringwithformat:@"%@,%@",nameinput,paasinput];nsmutableurlrequest *request = [nsmutablerequest alloc]initwithurl:url];

[请求setHTTPMethod:@"POST"];[请求 setValue:@"application/x-www-form-urlencoded"forHTTPHeaderField:@"Content-Type"];[请求sethttpbody:[body dataysubgebcidubg:asciiiiencodingallowlossyconversion: true]];[请求 setValue:postLength forHTTPHeaderField:@"Content-Length"];[请求setHTTPBody:postData];NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if (conn) NSLog(@"连接成功");

关于iphone - 如何向服务器写入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7836369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com