gpt4 book ai didi

xcode - 在一个 NSURLSession.sharedSession 上运行 2 个任务

转载 作者:行者123 更新时间:2023-11-28 06:54:56 25 4
gpt4 key购买 nike

我正在尝试执行 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:]:范围{9223372036854775807,0 } 越界;字符串长度 52427'"谢谢

最佳答案

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

//: 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()

关于xcode - 在一个 NSURLSession.sharedSession 上运行 2 个任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33905063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com