只需使用 NSMutableURLRequest 和 sendAsynchronousRequest 进行正常的 HTTP 发布。但是我传入的 NSHTTPURLResponse 对象在调用后的 statusCode 为零。我收到此错误:
sendAsynchronousRequest 错误=错误域=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 panic[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代码:
索引.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();
?>
发送响应函数
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;
}