当我试图在没有(www-authorization)的情况下使用NSURLSession和dataTaskWithRequest(我正在使用自定义委托)发布帖子请求。
效果很好。 (我发送请求,在服务器上我只打印echo $ _POST ['name'],我得到“名称”作为回复。)
当我尝试使用(www-authorization)发布帖子请求时,它看起来效果正常
如果我输入正确的用户名和密码,我会得到带有状态码200和json数据的标题,如果我犯了错误,我会得到状态码为401的标题。 在服务器端我做了
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Realm"');
header('HTTP/1.0 401 Unauthorized');
exit;
}
else
{
$user = new btuser($_SERVER["PHP_AUTH_USER"],$_SERVER['PHP_AUTH_PW']);
if ($user->logToDB())
{
header('content-type:application/json');
header('test-login: '.$user->getname());
$array = array("fff"=>"tttt",
"ddd"=>"qqqq",
"uuuu"=>$_post['name']
);
echo json_encode($array);
}
else
{
header('HTTP/1.0 401 Unauthorized');
unset($_SERVER['PHP_AUTH_USER']);
}
}
,但“post”请求在某处遗失)))...我得到这样的东西 - {“fff”:“tttt”,“ddd”:“qqqq”,“uuuu”:null}。
所有delegete的方法看起来都很好...... 第一种方法是
1
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData: (int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend: (int64_t)totalBytesExpectedToSend{
NSLog(@"did send body data");
}
2
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler{
NSLog(@"challenge");
NSURLCredential* newCredintioal;
newCredintioal = [NSURLCredential credentialWithUser:@"testuser" password:@"testpassword" persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:newCredintioal forAuthenticationChallenge:challenge];
completionHandler(NSURLSessionAuthChallengeUseCredential,newCredintioal);
}
3然后再次发送了BodyBodyData。
4
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
NSLog(@"did receive response %@", response);
completionHandler(NSURLSessionResponseAllow);
NSURLRequest* req = dataTask.currentRequest;
NSData* dt = [req HTTPBody];
NSString* body = [[NSString alloc] initWithData:dt encoding:NSUTF8StringEncoding];
completionHandler(NSURLSessionResponseAllow);
NSLog(@"credintials %d \n %@ \n %@ \n %@ ",dataTask.state, body, dataTask, req);
}
5
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
NSError *jsonParsingError = nil;
NSDictionary *urlResponseDataDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonParsingError];
[_savedData appendData:data];
// NSString* str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"did receive data %@",urlResponseDataDict);
}
6
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
// NSError *jsonParsingError = nil;
// NSDictionary *urlResponseDataDict = [NSJSONSerialization JSONObjectWithData:_savedData options:0 error:&jsonParsingError];
NSString* str = [[NSString alloc] initWithData:_savedData encoding:NSUTF8StringEncoding];
NSLog(@"did complete with error - %@, data - %@, _saveddata - %@", error, str, _savedData ); //, urlResponseDataDict);
}
如何通过授权发出简单的数据发布请求(URLSession:task:task didReceiveChallenge :)? 我做错了什么?
P.S。抱歉我的英文。
答案 0 :(得分:1)
问题可能出在PHP脚本上:$_post
与$_POST
不同(请注意大小写)