首页 > 解决方案 > Android的这个post-UrlRequest如何转换成IOS?

问题描述

下面的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()

标签: androidiosswiftnsurlrequest

解决方案


您正在使用适用于 Android 的 Html Parser,它不适用于 Swift(IOS)您必须使用不同的插件我不是 Swift 专业人士,但我发现这个插件希望对您有所帮助。

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


推荐阅读