以下 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()
答案 0 :(得分:0)
您正在使用Android的Html Parser,它将无法在Swift(IOS)上运行,您必须使用其他插件。我不是Swift的专业人士,但我发现此插件希望对您有所帮助。
一点建议Swift是基于目标c的,目标c与使用Kotlin和Java的Android不同,我们不能使用相同的插件