草庐IT

sendSynchronousRequest

全部标签

ios - 如何在 Swift 2.0 中获取错误消息?

我在Swift1.2中经常使用这种方法:NSURLConnection.sendSynchronousRequest(:_:_:_)但在iOS9中显然已弃用。但是它仍然有效,但现在它使用新的Swift2.0错误处理,我不知道如果失败我将如何获得错误消息,例如。如果时间用完。我知道我必须将它放入do-catch中,然后在方法之前说尝试,但我不知道如何捕获错误消息。do{letdata=tryNSURLConnection.sendSynchronousRequest(request,returningResponse:nil)returndata}catch_{returnnil}之前我

ios - NSURLConnection sendSynchronousRequest 响应在无效凭证时为零

我遇到一个奇怪的行为,即使用带有无效凭据的sendSynchronousRequest或sendAsynchronousRequest会使nsurlresponse为零。但是使用[[NSURLConnectionalloc]initWithRequest:requestdelegate:self];的旧方法我得到了401响应代码。使用sendSynchronousRequest或sendAsynchronousRequest的错误值为ErrorDomain=NSURLErrorDomainCode=-1012"Theoperationcouldn’tbecompleted.(NSURL

objective-c - 如何检测 NSURLConnection 的 sendSynchronousRequest 是否为 :returningResponse:error: ended up being timed out or other error

我使用NSURLConnection的sendSynchronousRequest:returningResponse:error:方法(在单独的NSOperation线程中)连接到外部服务器以检索数据。我如何知道操作结束是超时还是其他网络错误? 最佳答案 如果出现错误,当sendSynchronousRequest:returningResponse:error:返回时,error参数将为非nil。您可以通过检查[NSErrorcode]返回的值来检索错误代码。超时的错误代码是NSURLErrorTimedOut。例如:NSErr

ios - dispatch_async 和 [NSURLConnection sendSynchronousRequest]

围绕这个话题有各种各样的问题,很多建议说不要在dispatch_async中使用sendSynchronousRequest,因为它会阻塞线程,并且GCD会产生很多新的工作线程来服务所有同步URL请求。关于iOS5[NSURLConnectionsendAsynchronousRequest:queue:completionHandler:]在幕后做了什么,似乎没有人给出明确的答案。我读过的一篇文章指出它“可能”进行优化,并且“可能”使用运行循环——但肯定不会为每个请求创建一个新线程。当我在使用sendAsynchronousRequest:queue:completionHandle

ios - NSURLConnection sendSynchronousRequest 似乎挂起

此HTTP调用似乎挂起,我不明白为什么。调用后的日志行永远不会运行。我几乎肯定在做一些非常愚蠢的事情,但如果有人能发现它是什么,我将不胜感激。据我所知,我的代码等同于文档。我使用什么URL似乎并不重要,并且HTTP请求永远不会被服务器记录(如果指向一个)。NSURLRequest*req=[NSURLRequestrequestWithURL:[NSURLURLWithString:@"http://www.google.com/"]];NSURLResponse*res=nil;NSError*err=nil;NSLog(@"Abouttosendreq%@",req.URL);NS

ios - 如何在 Swift 上检查 sendSynchronousRequest 中的 Response.statusCode

如何在Swift中检查SendSynchronousRequest中的response.statusCode代码如下:leturlPath:String="URL_IS_HERE"varurl:NSURL=NSURL(string:urlPath)varrequest:NSURLRequest=NSURLRequest(URL:url)varresponse:AutoreleasingUnsafeMutablePointer=nilvarerror:NSErrorPointer?=nilvardata=NSURLConnection.sendSynchronousRequest(req
12