zoukankan      html  css  js  c++  java
  • python+requests 请求响应文本出错返回“登录超时”

    Python+requests请求响应:"msg":"登录过时"

    1、出错原代码:

     1 import requests
     2 import json
    #页面按条件搜索返回相应数据的接口测试
    3 s = requests.session() 4 url = "http://47.106.203.20:8000/ssposs2/api/sspquery/query" 5 par = { 6 "order":"", 7 "orderField":"", 8 "pageIndex":1, 9 "pageSize":20, 10 "filename":"query-export", 11 "action":"download", 12 "service":"wf", 13 "sh":"n", 14 "provId":"", 15 "cityId":"", 16 "brockId":"", 17 "imei":"", 18 "pliceNo":"", 19 "zfHphm":"粤A61933", 20 "gtDate":"", 21 "shStatus":"", 22 "jgDataStatus":"", 23 "wfType":"", 24 "dataType":"0", 25 "gtStartDate":"2019-07-17 15:34:01", 26 "gtEndDate":"2019-08-16 15:34:01" 27 } 28 header = { 29 'connection':'keep-alive', 30 'Content-Type':'application/json' 31 } 32 r =s.post(url,data=json.dumps(par),headers=header) 33 print(r.status_code) 34 print(r.text)

    2、运行结果:

    1 200
    2 {"result":false,"msg":"登录过时","code":"012"}#接口响应成功的,但是请求内容登录过时。

    因为该接口测试的前提是在登录页面后进行条件查询,所以是要有登录的cookie值,但是这里没有获取前一个页面登录的cookie值所以导致请求出错

    二修改后:

    import requests
    import json
    s = requests.session()
    url = "http://47.106.203.20:8000/ssposs2/api/sspquery/query"
    par = {
        "order":"",
        "orderField":"",
        "pageIndex":1,
        "pageSize":20,
        "filename":"query-export",
        "action":"download",
        "service":"wf",
        "sh":"n",
        "provId":"",
        "cityId":"",
        "brockId":"",
        "imei":"",
        "pliceNo":"",
        "zfHphm":"粤A61933",
        "gtDate":"",
        "shStatus":"",
        "jgDataStatus":"",
        "wfType":"",
        "dataType":"0",
        "gtStartDate":"2019-07-17 15:34:01",
        "gtEndDate":"2019-08-16 15:34:01"
    }
    header = {
        'connection':'keep-alive',
        'Content-Type':'application/json'
    }
    #*******************************************
    #加上登录页面的cookie cookie
    = { 'JSESSIONID':'d192ae01-8b58-4033-85c1-d7e44a06bd00', 'root-Token':'ceshi' }
    #******************************************* r
    =s.post(url,data=json.dumps(par),headers=header,cookies=cookie) print(r.status_code) print(r.text)

    2、运行结果:

    200
    {"result":true,"msg":null,"code":null,"data":{"content":[],"pageable":{"sort":{"unsorted":true,"sorted":false,"empty":true},"pageNumber":0,"pageSize":20,"offset":0,"unpaged":false,"paged":true},"last":true,"totalPages":0,"totalElements":0,"first":true,"sort":{"unsorted":true,"sorted":false,"empty":true},"numberOfElements":0,"size":20,"number":0,"empty":true}}
  • 相关阅读:
    【转】UML中类与类之间的5种关系表示
    OSGI框架—HelloWorld小实例
    解决:“Workbench has not been created yet” error in eclipse plugin programming”,OSGI启动控制台报错问题
    Restful风格到底是什么?怎么应用到我们的项目中?
    Java程序员面试题集(1-50
    【转】Spring中@Component的作用
    【转】Spring AOP 实现原理与 CGLIB 应用
    【转】spring和springMVC的面试问题总结
    Java算法之“兔子问题”
    DDD创始人Eric Vans:要实现DDD原始意图,必须CQRS+Event Sourcing架构
  • 原文地址:https://www.cnblogs.com/xswt/p/11365678.html
Copyright © 2011-2022 走看看