0

NSMutableURLRequest と sendAsynchronousRequest を使用して通常の HTTP 投稿を行うだけです。しかし、私が渡す NSHTTPURLResponse オブジェクトは、呼び出し後にゼロの statusCode を持っています。次のエラーが表示されます。

sendAsynchronousRequest エラー = エラー Domain=NSURLErrorDomain Code=-1005 「ネットワーク接続が失われました。」UserInfo=0xb374a00 {NSErrorFailingURLStringKey= http://54.221.224.251、NSErrorFailingURLKey= http://54.221.224.251、NSLocalizedDescription=ネットワーク接続が失われました。

しかし、ステータスコードはありません。なんで?サーバーが送信しているステータス コードは 150 です。

サーバーにさまざまなデータを POST し、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;
}
4

1 に答える 1

0

1xx ステータス コードは特別です ( http://greenbytes.de/tech/webdav/draft-ietf-httpbis-p2-semantics-23.html#status.1xxを参照)。また、単に新しいステータス コードを作成するのは得策ではありません。

于 2013-09-05T05:55:34.470 に答える