1

我试图在第一个请求中执行 2 个 HTTP 请求,我将获得一个将在第二个请求中使用的 var。我的问题是当我使用下面的代码执行此操作时,它只运行第二个请求而不运行第一个请求。

    let url = NSURL(string:"https://loginurl")
    let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
    let request = NSMutableURLRequest(URL: url!, cachePolicy: cachePolicy, timeoutInterval: 2.0)
    request.HTTPMethod = "POST"

    let contentType = " application/x-www-form-urlencoded"
    NSURLProtocol.setProperty(contentType, forKey: "Content-Type", inRequest: request)

    var dataString = "user details"
    var requestBodyData = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
    request.HTTPBody = requestBodyData

    NSURLProtocol.setProperty(requestBodyData!.length, forKey: "Content-Length", inRequest: request)

    var response: NSURLResponse?
    let session = NSURLSession.sharedSession()


    var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in

 //code for getting the session id to be used in the second request

    })
    task.resume()

    let url2 = NSURL(string:"http://url2?sessionid")
    let request2 = NSMutableURLRequest(URL: url2!, cachePolicy: cachePolicy, timeoutInterval: 2.0)
    request2.HTTPMethod = "GET"
     dataString=""
     requestBodyData = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
    request2.HTTPBody = requestBodyData
    NSURLProtocol.setProperty(contentType, forKey: "Content-Type", inRequest: request2)

      task = session.dataTaskWithRequest(request2, completionHandler: {data, response, error -> Void in


     })

    task.resume()

谁能解释这段代码有什么问题

问题2:

 let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in

        if let httpRes = response as? NSHTTPURLResponse {
            let html = NSString(data:data!, encoding:NSUTF8StringEncoding)


            if let match = html?.rangeOfString("(?<=sessionid=')[^\';]+", options: .RegularExpressionSearch)  {
                portalid = (html?.substringWithRange(match))!


            }

        }

当我在模拟器中运行时,它可以正常工作,但是当我在 iPhone 上调试它时,它崩溃并显示此错误“由于未捕获的异常 'NSRangeException',正在终止应用程序,原因:'-[__NSCFString substringWithRange:]: Range {9223372036854775807, 0} out of界限;字符串长度 52427'" 谢谢

4

1 回答 1

1

这是发出两个请求的代码示例:

//: Playground - noun: a place where people can play

import UIKit
import XCPlayground

XCPlaygroundPage.currentPage.needsIndefiniteExecution = true

let session = NSURLSession.sharedSession()

session.dataTaskWithURL(NSURL(string: "http://www.google.com")!, completionHandler: { (data :NSData?, response :NSURLResponse?, error :NSError?) -> Void in

        let res = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print(res!)

        // make a new request here
        let innerSession = NSURLSession.sharedSession()

    innerSession.dataTaskWithURL(NSURL(string: "http://www.google.com")!, completionHandler: { (data :NSData?, response :NSURLResponse?, error :NSError?) -> Void in

        let res = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print(res!)


    }).resume()


    }).resume()
于 2015-11-24T22:37:06.423 回答