NSHTTPURLRequest无法读取statusCode

时间:2013-09-04 20:55:24

标签: php iphone objective-c http

使用NSMutableURLRequest和sendAsynchronousRequest执行正常的HTTP发布。但是我传入的NSHTTPURLResponse对象在调用后的statusCode为零。我收到这个错误:

  

sendAsynchronousRequest error =错误域= NSURLErrorDomain   代码= -1005“网络连接丢失。”的UserInfo = 0xb374a00   {NSErrorFailingURLStringKey = http://54.221.224.251,   NSErrorFailingURLKey = http://54.221.224.251,NSLocalizedDescription =   网络连接丢失。,NSUnderlyingError = 0xb587990“网络   连接丢失。“} 2013-09-04 16:46:19.146恐慌[2032:5907]   statusCode:0

但没有状态代码。为什么?服务器发送的状态码是150。

当我将不同的数据发送到服务器,并且不要求它返回statusCode时,连接顺利进行并且符合预期。

应用代码:

[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
            if (error)
                NSLog(@"sendAsynchronousRequest error = %@", error);
                NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
                int code = [httpResponse statusCode];
                NSString *coder = [NSString stringWithFormat:@"%d",code];
                NSLog(@"%@",coder);


            if (data) {
                NSLog(@"This is Data: %@",data);
                NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
                int code = [httpResponse statusCode];
                NSString *coder = [NSString stringWithFormat:@"%d",code];
                NSLog(@"%@",coder);
            }
        }];

PHP代码:

的index.php

<?php
require_once 'includes/main.php';
class dumb {
function dumber(){
/*--------------------------------------------------
    Handle visits with a login token. If it is
    valid, log the person in.
---------------------------------------------------*/


if(isset($_GET['tkn'])){

    // Is this a valid login token?
    $user = User::findByToken($_GET['tkn']);

    if($user){

        // Yes! Login the user and redirect to the protected page.

        $user->login();
        redirect('panic://success');
    }

    // Invalid token. Redirect back to the login form.
    redirect('panic://fail');
}



/*--------------------------------------------------
    Handle logging out of the system. The logout
    link in protected.php leads here.
---------------------------------------------------*/


if(isset($_GET['logout'])){

    $user = new User();

    if($user->loggedIn()){
        $user->logout();
    }

    redirect('index.php');
}


/*--------------------------------------------------
    Don't show the login page to already 
    logged-in users.
---------------------------------------------------*/


$user = new User();

if($user->loggedIn()){
    redirect('protected.php');
}



/*--------------------------------------------------
    Handle submitting the login form via AJAX
---------------------------------------------------*/

        if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["phash"])){
            rate_limit($_SERVER['REMOTE_ADDR']);

            rate_limit_tick($_SERVER['REMOTE_ADDR'], $_POST['email']);

            $message = '';
            $name = $_POST["name"];
            $email = $_POST["email"];
            $phash = $_POST["phash"];
            $subject = 'Your Login Link';

            if(!User::exists($email)){
                $subject = "Thank You for Registering!";
                $message = "Thank you for registering at our site!\n\n";
                // Attempt to login or register the person
            $user = User::loginOrRegister($email, $name, $phash);


            $message.= "You can login from this URL:\n";
            $message.= get_page_url()."?tkn=".$user->generateToken()."\n\n";

            $message.= "The link is going expire automatically after 10 minutes.";

            $result = send_email($fromEmail, $_POST['email'], $subject, $message);

            if(!$result){
            sendResponse(403, 'Error Sending Email');
            return false;
            }
            }
        else{
                sendResponse(150, 'Account already created.');
                return false;
        }
        }
        else if(isset($_POST["email"]) && isset($_POST["phash"])){
            rate_limit($_SERVER['REMOTE_ADDR']);

            rate_limit_tick($_SERVER['REMOTE_ADDR'], $_POST['email']);

            $message = '';
            $name = '';
            $email = $_POST["email"];
            $phash = $_POST["phash"];
            $subject = 'Your Login Link';

            if(!User::exists($email)){
                sendResponse(155, 'Account not yet created.');
                return false;
            }
            else{
            // Attempt to login or register the person
            $user = User::loginOrRegister($email, $name, $phash);


            $message.= "You can login from this URL:\n";
            $message.= get_page_url()."?tkn=".$user->generateToken()."\n\n";

            $message.= "The link is going expire automatically after 10 minutes.";

            $result = send_email($fromEmail, $_POST['email'], $subject, $message);

            if(!$result){
            sendResponse(403, 'Error Sending Email');
            return false;
            }
        }
        }

/*--------------------------------------------------
    Output the login form
---------------------------------------------------*/
}
}
$api = new dumb;
$api->dumber();
?>

sendResponse功能

function sendResponse($status, $body = '', $content_type = 'text/html')
{
    $status_header = 'HTTP/1.1 ' . $status . ' ' . 'ERROR';
    header($status_header);
    header('Content-type: ' . $content_type);
    echo $body;
}

1 个答案:

答案 0 :(得分:0)

1xx状态代码是特殊的(参见http://greenbytes.de/tech/webdav/draft-ietf-httpbis-p2-semantics-23.html#status.1xx);此外,仅仅编制新的状态代码并不是一个好主意。