gpt4 book ai didi

url - 为 URL 请求设置 Cookie

转载 作者:行者123 更新时间:2023-12-01 15:00:26 29 4
gpt4 key购买 nike

目前我有一个 iOS 应用程序,可以从网站上提取价格和数据。到目前为止,它运行良好,但我想让它更准确。为此,我需要为当前使用 String(contentsOf: _) 的 URL 请求设置 cookie。

当前进程

let requestUrl: URL = URL(string: "http://www.samsclub.com/sams/search/searchResults.jsp?searchTerm=Apple")!

var content: String?

do {
content = try String(contentsOf: requestUrl)
} catch {
print("Error while converting an NSURL to String: \(error)")
}

if content != "" {
// I do things with the content of the requestUrl...
}

可以用?

我想也许我应该使用 Alamofire 来拉取那些网站,然后解析数据。

我需要设置更改商店编号的 cookie 进行搜索,但一直无法找到这样做的方法。 Bellow 是我用于提取网站数据的代码 没有 设置cookie。
let requestUrl: String = "http://www.samsclub.com/sams/search/searchResults.jsp?searchTerm=Apple"

Alamofire.request(requestUrl, method: .post).responseString { response in
if let content: String = response.result.value {
// I do things with the content of the requestUrl...
}
}

其他 claim

我发现了许多不同的通过 Alamofire 设置 cookie 的方法,但这些方法不起作用,但如果 Alamofire 不是这种方法,请通知我。我真的需要它来工作,我愿意接受任何建议。

最佳答案

这一天花了四个星期,但我想通了! URLRequest和 Alamofire 是我光荣的答案!

  • 创建要调用的 URL。
    let requestUrl: String = "http://www.samsclub.com/sams/search/searchResults.jsp?searchTerm=Apple"
  • 接下来制作 URLRequest使用 URL 字符串,并设置其 http 方法。
    var urlRequest = URLRequest(url: requestUrl)
    urlRequest.httpMethod = "POST"
  • 然后为 URLRequest 设置 cookie .
    urlRequest.setValue("myPreferredClub=4969", forHTTPHeaderField: "Cookie")
    urlRequest.httpShouldHandleCookies = true
  • 最后发了URLRequest使用 Alamofire,并以我希望的任何方式使用响应数据。
    Alamofire.request(urlRequest).responseString { response in
    if let content: String = response.result.value {
    // I do things with the content of the urlRequest...
    }
    }
  • 关于url - 为 URL 请求设置 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42987502/

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