在下面的示例中,代码给出了不成功的输出。第一个是GET请求并接受cookie,然后发送post请求并使用许多标头和参数来设置cookie,如下所示。所以,请帮我从请求的成功响应此请求在android中工作,但同样不能在IOS(swift)中工作。请给我任何建议,示例或解决方案,或者告诉我是否正确。
let url = URL(string: "https://parivahan.gov.in/rcdlstatus/?pur_cd=102")
var req = URLRequest(url: url!)
req.httpMethod = "GET"
let datatask = URLSession.shared.dataTask(with: req) {(data, resp, err) in
if data == nil
{
print("something wrong")
return
}
let htttp = resp as! HTTPURLResponse?
print(htttp!.statusCode)
if err == nil && (htttp?.statusCode)! <= 500
{
self.cookie = HTTPCookieStorage.shared.cookies(for: (resp?.url!)!)
do
{
let str = String(data: data!, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))as String?
self.swift = try SwiftSoup.parse(str!)
var first = try self.swift.getElementsByAttributeValue("name", "javax.faces.ViewState").first()
if first == nil
{
first = try self.swift.getElementById("j_id1:javax.faces.ViewState:0")
}
self.vhtype = (try first?.attr("value"))!
let s = (Element.self, try SwiftSoup.parse(str!).body()?.getElementsByAttributeValueStarting("id","form_rcdl:j_idt").select("button").get(0).attr("id"))//.trimmingCharacters(in: .whitespaces))
self.str2 = s.1!
}
catch
{}
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.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
postreq.setValue("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.ascii.rawValue))as String?
// let con = String(data: data!, encoding: String.Encoding.utf8)
print(con!,"post urlrequest content")
}
})
posttask.resume()
}
else
{
print("status code > 500 bad response")
if err != nil
{
print(err!)
}
}
} // main datatask
datatask.resume()
我使用SwiftSoup库快速获取HTML元素。在上面的GET-Request中使用。
请求后的响应:
<?xml version="1.0" encoding="UTF-8"?>
<partial-response id="j_id1">
<changes>
<eval><![CDATA[PrimeFaces.ajax.Utils.loadStylesheets(['/rcdlstatus/vahan/javax.faces.resource/grid-css.css?ln=css','/rcdlstatus/vahan/javax.faces.resource/layout.css?ln=css','/rcdlstatus/vahan/javax.faces.resource/css/bootstrap.min.css?ln=bootstrap-3.3.7','/rcdlstatus/vahan/javax.faces.resource/components.css?ln=primefaces&v=6.0']);PrimeFaces.ajax.Utils.loadScripts(['/rcdlstatus/vahan/javax.faces.resource/jquery/jquery.js?ln=primefaces&v=6.0','/rcdlstatus/vahan/javax.faces.resource/jquery/jquery-plugins.js?ln=primefaces&v=6.0','/rcdlstatus/vahan/javax.faces.resource/core.js?ln=primefaces&v=6.0','/rcdlstatus/vahan/javax.faces.resource/components.js?ln=primefaces&v=6.0','/rcdlstatus/vahan/javax.faces.resource/keyfilter/keyfilter.js?ln=primefaces&v=6.0','/rcdlstatus/vahan/javax.faces.resource/jsf.js?ln=javax.faces']);]]></eval>
<update id="j_id1:javax.faces.ViewState:0"><![CDATA[tAHz4tEdkp3UuCksHevEmiJr3Ba888MqQIF6aJrubsyc8zjlt857mRKhGx6fEiqHHMDD4jaSaeAFhBu0PP/phICFySUlkGtAG4RGUBKsu5/a1LRD/qinwrwXFAwQu+7/FQPMCPIY3m6D8vERt5MpwDZT+TsRR0qpTqIo8Tlh6PyC3p/o01/Ukgw2DjhB3p3OnPpCkJsaGnpaIQKJyt08MISK+/yddilWaq9PQQ/7bYIPOUYUzNMU1M1MXtk/NxnQ3n6J17oy4DDFscd8MHmcna6BoOItPZ/vRaKRRNgXkxw66hgd9nBCP+JlXDg5V4kjCWWYa4BCHoNgtz/OdOI6KiDRrSFVTn0qWXnWwq2oMM7VGxllZSSNYA/nucYdKVrQ+4bLSpUz72/i4b+1kN12PWXIUaJmElPSCdyX0MRPf051oM2uAD7DdhgAkwG4AA6+2c3RHu4MgVcpSBM/9BWXawlArJGEfvPWiCTHSjjTAslhVFNOFuwffKpvwjku0BQsMh6DioVrXDq63C2alWmrGo6+1cD8In+iWWgfvgV5rKoVmCNpRfVQRkiEtjj5j/YQDCZsejFFEKEzf8a/nKBCQlM4hv0O0bkVYy+bq7OA/fjP3Pz8Ycyo/5tFW07qmWJBDblFZGVxEgb4VvBaX4NtWbCYrRWjTAttGGcQeyKEj9K1e3+6XNpvUDXKOkk1mXqzSdszGMwzcVOHGLhx9AkaXF4jWP6gG7OccRsOrI2i2qFSi2kl7hIem+qNdBQrvocM0CzHpQ53MeLxRoVxhWDUSXQCboPMH7lLX2E/CV82coP3Yy0I8y2gIp4byam+WP8plM6BcyV5jmfmk6q37gz4sWCYKR0VWIW5HZXKBwpPutQfvetrRmyWXDqmUI0Wf14XRpRS9KiCyiLO23C5g2UQJEC4BDc4K0JpxeBfnEA2FHCATE+i4y9QnJmOqXiN1UFigKst4HNjBdUA32NZdvIgTKGy4aROB9U1RuhLtJCR/En2xViWKo0g/4obOE79vWWnA1ndK22xlZvSaytfrZeDlnnffBoyCpEXCmAM3Gif28HJzMmYHC9pmK4/F6SB5a9BdQUlmVBvrAzARUhrMilr/RtI13peLWmVfaSMp/lskYHYeIyUjAVHAFRDKnw6v6443fXkJBCI9VM0KV0i9TUutdn/qYJYAnWMbGSW5wljsYO3qRKoegQthby3QrHraxrypIiCxmiodiTVXV0hNsJKan05EDsHyzI1Ih4tnhq7cTOCt4f1LY8r6H6ZvHapu716h7LYulTmewLC0i58Y2MmMm1PzkXeoXp4uN3LMAEW3HDfJri3NweoINkMWVIB4vzt/qEeZxkCerWcXFu2JHW30mowwM9+Rh0jc86CozF41PK0pYDaQZfsrVGuZPQezXv/0lJBfDUjbCwylXgCSwyRAr+YrZ22XrhHZ+QMDeIlJlkBILobSHGd+QelgFVABctb+APyJQAR5kmoSkd9yjOKr3Cz+pP4r80TSE80w/+95H7u1QMEkOHgTvqY0kBpK/1+bXgAYZHy2jb5HN7heKGE+pr/NpEEh5oLh4dyM0VHY2TK8TF2LZJvSnLVfjrIncjSWmEVB2pgy7Rslf8LtE0K+BJ8AQfOkXDX9gtLW88SrlYQjx4gcPha8mH1cAwqW3RG]]></update>
</changes>
</partial-response>
扩展响应类似于:
<?xml version='1.0' encoding='UTF-8'?><partial-response id="j_id1"><changes><update id="form_rcdl:rcdl_pnl"><![CDATA[<div id="form_rcdl:rcdl_pnl" class="ui-panel ui-widget ui-widget-content ui-corner-all datatable-panel" data-widget="widget_form_rcdl_rcdl_pnl"><div id="form_rcdl:rcdl_pnl_header" class="ui-panel-titlebar ui-widget-header ui-helper-clearfix ui-corner-all"><span class="ui-panel-title">Know your RC Status</span></div><div id="form_rcdl:rcdl_pnl_content" class="ui-panel-content ui-widget-content"><div id="form_rcdl:j_idt14" class="ui-messages ui-widget" aria-live="polite"></div><div id="form_rcdl:j_idt27" class="ui-outputpanel ui-widget">
<div class="ui-grid-row bottom-space">
<div class="ui-grid-col-4 resp-blank-height"></div>
<div class="ui-grid-col-2">
<label class="field-label resp-label-section right-position "><label id="form_rcdl:j_idt29" class="ui-outputlabel ui-widget top-space field-label-mandate" for="form_rcdl:tf_reg_no1">Registration No.<span class="ui-outputlabel-rfi">*</span></label>
</label>
</div>
<div class="ui-grid-col-3"><input id="form_rcdl:tf_reg_no1" name="form_rcdl:tf_reg_no1" type="text" value="gj05fl" autocomplete="off" maxlength="6" placeholder="DL10ABC" size="20" onchange="PrimeFaces.ab({s:"form_rcdl:tf_reg_no1",e:"valueChange",p:"form_rcdl:tf_reg_no1",u:"form_rcdl:tf_reg_no1"});" disabled="disabled" style="width: 48% !important" aria-required="true" class="ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all ui-state-disabled input top-space inline-section" /><script id="form_rcdl:tf_reg_no1_s" type="text/javascript">PrimeFaces.cw("InputText","widget_form_rcdl_tf_reg_no1",{id:"form_rcdl:tf_reg_no1"});</script><input id="form_rcdl:tf_reg_no2" name="form_rcdl:tf_reg_no2" type="text" value="9124" autocomplete="off" maxlength="4" placeholder="1234" size="15" onchange="PrimeFaces.ab({s:"form_rcdl:tf_reg_no2",e:"valueChange",p:"form_rcdl:tf_reg_no2",u:"form_rcdl:tf_reg_no2"});" disabled="disabled" style="width: 42% !important" aria-required="true" class="ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all ui-state-disabled input top-space inline-section" /><script id="form_rcdl:tf_reg_no2_s" type="text/javascript">PrimeFaces.cw("InputText","widget_form_rcdl_tf_reg_no2",{id:"form_rcdl:tf_reg_no2"});</script>
</div>
</div></div>
<div class="ui-grid-row bottom-space center-position">
<div class="ui-grid-col-12"><button id="form_rcdl:j_idt43" name="form_rcdl:j_idt43" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" onclick="PrimeFaces.ab({s:"form_rcdl:j_idt43",u:"form_rcdl:pnl_show form_rcdl:pg_show form_rcdl:rcdl_pnl",onst:function(cfg){PF('block_ui').show();;},onsu:function(data,status,xhr){PF('block_ui').hide();;}});return false;" type="submit"><span class="ui-button-text ui-c">Check Status</span></button><script id="form_rcdl:j_idt43_s" type="text/javascript">PrimeFaces.cw("CommandButton","widget_form_rcdl_j_idt43",{id:"form_rcdl:j_idt43"});</script><button id="form_rcdl:rest_bt" name="form_rcdl:rest_bt" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" onclick="" type="submit"><span class="ui-button-text ui-c">Reset</span></button><script id="form_rcdl:rest_bt_s" type="text/javascript">PrimeFaces.cw("CommandButton","widget_form_rcdl_rest_bt",{id:"form_rcdl:rest_bt"});</script>
</div>
</div></div></div><script id="form_rcdl:rcdl_pnl_s" type="text/javascript">PrimeFaces.cw("Panel","widget_form_rcdl_rcdl_pnl",{id:"form_rcdl:rcdl_pnl"});</script>]]></update><update id="form_rcdl:pnl_show"><![CDATA[<div id="form_rcdl:pnl_show" class="ui-outputpanel ui-widget datatable-panel"><span id="form_rcdl:pg_show">
<div class="ui-grid ui-grid-responsive">
<div class="ui-grid-col-12">
<div class="ui-grid ui-grid-responsive"><div id="form_rcdl:rcPanel" class="ui-outputpanel ui-widget"><div id="form_rcdl:j_idt67" class="ui-outputpanel ui-widget">
<div class="center-position bottom-space">
<span class="font-bold text-underline">Vehicle Details Showing in Registering Authority</span>
</div>
<div class="font-bold top-space bottom-space text-capitalize">
Registering Authority: SURAT, GUJARAT
</div>
<table class="table table-responsive table-striped table-condensed table-bordered">
<tr>
<td style="width: 15%"><span class="font-bold">Registration No:</span></td>
<td style="width: 45%"><span class="">GJ05FL9124</span></td>
<td style="width: 15%"><span class="font-bold">Registration Date:</span></td>
<td>06-Oct-2008</td>
</tr>
<tr>
<td><span class="font-bold">Chassis No:</span></td>
<td>MBLHA10EL8GGXXXXX</td>
<td><span class="font-bold">Engine No:</span></td>
<td>HA10EB8GGXXXXX</td>
</tr>
<tr>
<td><span class="font-bold">Owner Name:</span> </td>
<td colspan="4">BHUPATBHAI</td>
</tr>
<tr>
<td><span class="font-bold">Vehicle Class:</span> </td>
<td>M-CYCLE/SCOOTER (2)</td>
<td><span class="font-bold">Fuel Type:</span></td>
<td>PETROL</td>
</tr>
<tr>
<td><span class="font-bold">Maker / Model:</span></td>
<td colspan="3">HERO HONDA MOTORS LTD / PASSION PLUS MAC</td>
</tr>
<tr>
<td><span class="font-bold">Fitness Upto:</span></td>
<td>05-Oct-2023</td>
<td><span class="font-bold">Insurance Upto:</span></td>
<td>29-Oct-2017</td>
</tr>
<tr>
<td><span class="font-bold">Fuel Norms:</span> </td>
<td>NOT AVAILABLE</td>
<td><span class="font-bold">Tax Paid Upto:</span></td>
<td></td>
</tr>
</table></div></div>
</div>
</div>
</div></span></div>]]></update><update id="j_id1:javax.faces.ViewState:0"><![CDATA[rJD1lmfq15GxvFXr7QVaQ07XM5ypeP+pbjgAg0ekiPxhoxedOlSIZ4075nxHt8AivXApJ7vSsY2/6qyy2cGwZqCnoThQkju+nZvdbB0RVwxvAHo3UFzq4EWHfDXMdHY+3Hb5EpiCUBm8TeVN//k8s1NqU1Wf8Koy6GBLoK2rQ9ARSfQPenYNBPAs7WXv8CUOYrYRWwzesxDJh/oZ3/71F3dG/GnN8aZZu/mEShZ0qS/OX4AyBiu4IyytEjMovNqh/5h4KYRdULULLf6IQ3xFfmIo0eb+wF2VfOk5x4ulhN0WgNkwJtexeM2gv/DZ82iLx//L7uUUScDFgHcgEOpcvXLt7qiVHWOA+p4r4TKYCg6PVwFgOWd15VDHJ/wLooKY+VxF2IqEYqJOvWAV0L5lgKDozX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