【问题标题】:Getting JSON object from php authentication objective c从 php 身份验证目标 c 获取 JSON 对象
【发布时间】: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


【解决方案1】:

调试此问题的第一步是尝试在浏览器中查看所需的响应或使用 fiddler 之类的工具。查看您正在使用的 url 并查看实际的 POST 值是什么。提交的用户名/密码在哪里?通常身份验证使用服务器端身份验证,但有时它在 url 本身中。它在您的浏览器中是如何工作的?

根据描述和您的代码,我认为您可能包含在

中的字段
 [urlRequest addValue:@"test" forHTTPHeaderField:@"m_username" ];
 [urlRequest addValue:@"123" forHTTPHeaderField:@"m_password" ];

是否应该添加到您要发布的网址中?也许是这样的:

NSString *urlAsString =[NSString stringWithFormat:@"http://www.myurl.com/abc/authenticate.php?username/password"];

【讨论】:

  • 其实用户名和密码不要和url连接。这些将在 post 方法中作为参数发送。
  • 纠正什么,内特·弗林克
  • Omer,只是想更正我的答案以成为这里的最新代码
【解决方案2】:

如果您期待身份验证挑战,您应该使用connection:didReceiveAuthenticationChallenge: NSURLConnection 方法

通过添加该方法并包含断点,您至少可以看到您的挑战是否正在发生,并且实际上是否得到了正确处理。以下是我如何在项目中使用身份验证质询方法的示例

 - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
 {
     NSURLCredential *credential = [NSURLCredential credentialWithUser:@"username" password:@"passwordvalue" persistence:NSURLCredentialPersistenceForSession];
     [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
 }

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/AuthenticationChallenges.html#//apple_ref/doc/uid/TP40009507-SW1

【讨论】:

  • 谢谢,但我不期待身份验证挑战
猜你喜欢
  • 2023-03-23
  • 1970-01-01
  • 2014-11-17
  • 1970-01-01
  • 2016-08-24
  • 1970-01-01
  • 2021-12-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多