Get HTTP Status using swift -



Get HTTP Status using swift -

i sorry, haven't found reply question(( please, don't harsh, not professional programmer, maintain learning , hope 1 time able reply someone's question))

i trying http status of link (i sort of generating links depending on 1 database entries code, abcdef, maintain them in array , generate link sec database, www.blablabla.abcdef.net), can see whether page exists in database or not.

i have written code, wrong. maybe it's question like: "what's wrong code?" on stack say, have show attempts of problem solving...

i wish maintain swift, without additional modules or something, think nshttpurlresponse must enough, using somehow wrong.

looking forwards help , replies))

var err: nserror! nsurlconnection.sendasynchronousrequest(request, queue: queue, completionhandler:{ (response: nsurlresponse?, data: nsdata!, error: err) -> void in if (err != nil) { allow httpstatus: nshttpurlresponse = response nshhtpurlresponse mylink in alllinks { println("here current status code" + httpstatus.statuscode + "of link:" + mylink) if httpstatus.statuscode == 200 {println("success!!!!!")} } }

the fundamental issue here appear looking @ statuscode if err not nil. if have error, don't have status code. error parameter closure indicates fundamental network issue prevented getting page in question. statuscode meaningful if error was nil (i.e. succeeded in connecting server), in case statuscode how server informs of ability service http request.

a couple of minor things:

you don't need var err: nserror! line (because error object passed parameter closure). variable you've declared not used here

i don't see how error: err 3rd parameter closure have worked. syntax "variablename: variabletype", there no type of err.

likewise, code referring non-existent class, nshhtpurlresponse.

it's prudent if let retrieval of nshttpurlresponse in order statuscode.

i'm unclear intent in iterating through alllinks, because connection given request, not bunch of links. @ statuscode in lite of particular request. if need test multiple urls, separate request each.

thus:

nsurlconnection.sendasynchronousrequest(request, queue: queue) { response, data, error in if allow httpresponse: nshttpurlresponse = response as? nshttpurlresponse { allow statuscode = httpresponse.statuscode if statuscode == 200 { println("success!!!!!") } else { println("status code \(statuscode), expected 200") } } if (error != nil) { println("error = \(error)") } }

i made few cosmetic changes (i think error fine selection variable name, prefer trailing closure syntax, tend utilize inferred types closure parameters create declaration little more concise, etc.), of immaterial question @ hand: illustrates how 1 checks status code.

http swift httprequest

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -