【问题标题】:How this post-UrlRequest of Android convert into IOS?Android的这个post-UrlRequest如何转换成IOS?
【发布时间】:2018-11-20 05:20:35
【问题描述】:

下面的 Jsoup 发布请求 我正在尝试转换为 IOS 但无法正常工作,并且还为此设置了 cookie,因此请提供任何解决方案。我尝试了 4-5 天但没有成功。所以,请帮我下面的android代码:

str2 = Jsoup.connect("https://parivahan.gov.in/rcdlstatus/vahan/rcDlHome.xhtml").validateTLSCertificates(false).followRedirects(true)
                    .method(Method.POST).cookies(cookies).
                            referrer("https://parivahan.gov.in/rcdlstatus/?pur_cd=102").
                            header("Content-Type",
                                    "application/x-www-form-urlencoded")
                    .header("Host", "parivahan.gov.in")
                    .header("HEADER_ACCEPT", "application/xml, text/xml, */*; q=0.01")
                    .header("Accept-Language", "en-US,en;q=0.5")
                    .header("Accept-Encoding", "gzip, deflate, br")
                    .header("X-Requested-With", "XMLHttpRequest")
                    .header("Faces-Request", "partial/ajax")
                    .header("Origin", "https://parivahan.gov.in")
                    .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64) " +
                            "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 " +
                            "Safari/537.36").data("javax.faces.partial.ajax", "true")
                    .data("javax.faces.source", str2).data("javax.faces.partial.execute",
                            "@all").data("javax.faces.partial.render",
                            "form_rcdl:pnl_show form_rcdl:pg_show form_rcdl:rcdl_pnl")
                    .data(str2, str2).data("form_rcdl", "form_rcdl")
                    .data("form_rcdl:tf_reg_no1", str3).data("form_rcdl:tf_reg_no2", vhcasino)
                    .data("javax.faces.ViewState", vhtype).execute().body();

在这个请求中传递了许多标头和参数,所以它在 ios 中的表现如何正确请给它任何建议。

我的 IOS 代码:

      let posturl = URL(string: "https://parivahan.gov.in/rcdlstatus/vahan/rcDlHome.xhtml")
            var postreq = URLRequest(url: posturl!)
            postreq.httpMethod = "POST"
            HTTPCookieStorage.shared.setCookies(self.cookie, for: posturl!, mainDocumentURL: nil)

            postreq.setValue("https://parivahan.gov.in/rcdlstatus/?pur_cd=102", forHTTPHeaderField: "Referer")
            postreq.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
            postreq.addValue("application/xml, text/xml, */*; q=0.01", forHTTPHeaderField: "Accept")
            postreq.setValue("parivahan.gov.in", forHTTPHeaderField: "Host")
            postreq.setValue("en-US,en;q=0.5", forHTTPHeaderField: "Accept-Language")
            postreq.setValue("gzip, deflate, br", forHTTPHeaderField: "accept-encoding")
            postreq.setValue("XMLHttpRequest", forHTTPHeaderField: "X-Requested-With")
            postreq.setValue("partial/ajax", forHTTPHeaderField: "Faces-Request")
            postreq.setValue("https://parivahan.gov.in/", forHTTPHeaderField: "Origin")
            let userAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) " + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140" + "Safari/537.36"
            postreq.setValue(userAgent, forHTTPHeaderField: "User-Agent")

            let postparam = ["javax.faces.partial.ajax" : "true" ,
                             "javax.faces.source" : self.str2,
                             "javax.faces.partial.execute" : "@all",
                             "javax.faces.partial.render" : "form_rcdl:pnl_show form_rcdl:pg_show  form_rcdl:rcdl_pnl" ,
                             self.str2 : self.str2 ,
                             "form_rcdl" : "form_rcdl" ,
                             "form_rcdl:tf_reg_no1" : self.regno1,
                             "form_rcdl:tf_reg_no2" : self.regno2,
                             "javax.faces.ViewState": self.vhtype]


            do
            {
                postreq.httpBody = try JSONSerialization.data(withJSONObject: postparam, options: [])
            } catch {
                print(error,"error in postreq body")
            }

            print(self.str2)
            print(self.regno1,"reg no1")
            print(self.regno2,"reg no2")


            let posttask = URLSession.shared.dataTask(with: postreq, completionHandler: {(data, response, err) in

                if let http = response as? HTTPURLResponse
                {
                    print(http.statusCode)
                }
                if err == nil
                {
                    let con = String(data: data!, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))as String?
                    print(con!,"post urlrequest content")
                }
            })

            posttask.resume()

【问题讨论】:

  • 您可以使用 postman 为您的 api 生成 swift 代码
  • 如何使用邮递员来指导我?
  • 像往常一样在邮递员中执行您的 api,然后单击右侧的代码选项。从他们那里,它将向您显示一个下拉列表,您希望使用哪种语言生成代码
  • 但这不是 api 它是一个 URL 并在输出中获取 html 部分响应所以我该怎么走?
  • @jinalrakholiya 你能分享你的IOS代码吗?参数,标题和url。

标签: android ios swift nsurlrequest


【解决方案1】:

您正在使用适用于 Android 的 Html Parser,它无法在 Swift(IOS) 上运行,您必须使用不同的插件

还有一点建议 Swift 是基于目标 c 的,它与使用 Kotlin 和 Java 的 Android 不同的语言我们不能使用相同的插件

【讨论】:

  • 那么,我该如何完成这个任务呢?
  • @jinalrakholiya 关注这些链接 1.) stackoverflow.com/questions/31080818/… 2.) github.com/scinfu/SwiftSoup 并研究它们
  • 这是 html 解析器,但我不想从带有标题和参数的 post 请求中获取响应,如上。 html解析器在完全得到响应后在下一步使用。
猜你喜欢
  • 2017-10-29
  • 2018-03-28
  • 2021-10-11
  • 1970-01-01
  • 2021-01-03
  • 2021-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多