【发布时间】:2012-06-27 18:55:45
【问题描述】:
我正在尝试使用以下代码进行身份验证
NSString *urlAsString =[NSString stringWithFormat:@"http://www.myurl.com/abc/authenticate.php"];
NSURL *url = [NSURL URLWithString:urlAsString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setTimeoutInterval:30.0f];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest addValue:@"test" forHTTPHeaderField:@"m_username" ];
[urlRequest addValue:@"123" forHTTPHeaderField:@"m_password" ];
[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response,NSData *data, NSError *error) {
if ([data length] >0 && error == nil){
html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"HTML = %@", html);
receivedData = [NSMutableData data];
}
else if ([data length] == 0 && error == nil){
NSLog(@"Nothing was downloaded.");
}
else if (error != nil){
NSLog(@"Error happened = %@", error);
}
}];
// Start loading data
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
if (theConnection)
{
// Create the NSMutableData to hold the received data
receivedData = [NSMutableData data];
}
else {
// Inform the user the connection failed.
}
我的用户名密码是正确的,我认为我没有正确调用这就是为什么我没有得到想要的结果并且网络服务正在接收空参数。
可能是什么问题?
任何帮助表示赞赏。
【问题讨论】:
-
您确定您的 authenticate.php 页面期望用户名/密码在 HTTP 标头中传递,而不是在 url 查询字符串中传递? (或作为表单 POST 值?)
-
如何在 url 查询字符串和表单 post 值中进行调用?你能帮我解决这个问题吗
-
接受了答案但没有投票?哇。
标签: iphone ios xcode json web-services