Gemfile里添加
gem 'savon', '~> 2.12.0'
# 国政通参数加密
def encrypt(str, key = 'XXXX')
cipher = OpenSSL::Cipher.new("DES")
cipher.encrypt
cipher.key = key
cipher.iv = key
result = cipher.update(str.encode('gb18030','utf-8'))
result << cipher.final
Base64.encode64(result).strip
end
def decrypt(str, key: 'XXXX')
cipher = OpenSSL::Cipher.new("DES")
cipher.decrypt
cipher.key = key
cipher.iv = key
result = cipher.update(Base64.decode64(str)) + cipher.final
result
end
调用国政通接口
client = Savon.client(wsdl: 'http://xxxxxxx?wsdl')
response = client.call(:query_single, message: { username_: encrypt("xxx"),
password_: encrypt("xxx"),
type_: encrypt("xxx"),
param_: encrypt("姓名,身份证号").encode('utf-8','gb18030') })
result = response.body[:query_single_response][:query_single_return]
doc = Nokogiri.XML(decrypt(result))
data_status = doc.xpath('/data/message/status[1]').text
return false if data_status != "0"
policeCheckInfo_status = doc.xpath('/data/policeCheckInfos/policeCheckInfo/message/status[1]').text
# policeCheckInfo_value = doc.xpath('/data/policeCheckInfos/policeCheckInfo/message/value[1]').text.encode('utf-8','gb18030')
return false if policeCheckInfo_status != "0"
compStatus = doc.xpath('/data/policeCheckInfos/policeCheckInfo/compStatus[1]').text
# compResult = doc.xpath('/data/policeCheckInfos/policeCheckInfo/compResult[1]').text.encode('utf-8','gb18030')
name = doc.xpath('/data/policeCheckInfos/policeCheckInfo/name[1]').text.encode('utf-8','gb18030')
if compStatus == "3"
认证通过
end
国政通编码为gb18030,在加密参数的时候记得转换编码。
网友评论