我正在制作一个Lo-gin应用程序,我想将用户名和密码发送到服务器以进行验证,我已经通过很多方式做到了这一点,但是我无法发布。
我正在发布用户名和密码,但如果我直接将用户名和密码提供给php则无法正常工作,因此如何在iphone中通过邮寄方式发送

NSString *post = [[NSString alloc] initWithFormat:@"UserName=%@&Password=%@",username,pass];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

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

NSURL *url = [NSURL URLWithString:@"http://www.celeritas-solutions.com/emrapp/connect.php?"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody:postData];

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if( theConnection )
{
    webData = [[NSMutableData data] retain];
}
else
{
    NSLog(@"Inside the else condition");
}

[nameInput resignFirstResponder];
[passInput resignFirstResponder];
nameInput.text = nil;
passInput.text = nil;

最佳答案

//编辑代码,尝试这样做可能会有所帮助。

 NSString *post = [[NSString alloc] initWithFormat:@"UserName=%@&Password=%@",username,pass];

 NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

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

 NSURL *url = [NSURL URLWithString:@"http://www.celeritas-solutions.com/emrapp/connect.php?"];
 NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
 [theRequest setHTTPMethod:@"POST"];
 [theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
 theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
 [theRequest setHTTPBody:postData];

10-04 13:42