zoukankan      html  css  js  c++  java
  • post 请求报400错误

    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    import sys
    import pycontrol.pycontrol as pc
    import time
    import csv
    import ssl
    from datetime import date,datetime
    import urllib2
    import urllib
    import cookielib
    import json
    import httplib
    import re
    import requests
    import StringIO
    import time
    import sys
    import json
    import re
    import hashlib
    import xlrd
    def read_excel(a):
      wb=xlrd.open_workbook(filename=a) #打开文件
      print wb.sheet_names() ##获取所有sheet页名字
      print type(wb.sheet_names())
      for x in wb.sheet_names():
           print type(x)
           print  x.encode('utf-8')
      sheet1=wb.sheet_by_name(u'批量提交')
      print sheet1.name,sheet1.ncols,sheet1.nrows
      b=sheet1.nrows
      list = [[] for i in range(b)]
      for i in range(sheet1.nrows):
           if i==0:
               pass
           else:
               print sheet1.row(i)
               print type(sheet1.row(i))
               for j in  xrange(len(sheet1.row(i))):
                   if j==7:
                    # print  sheet1.row(i)[j].value
                    # print  sheet1.row(i)[j].value
                    print xlrd.xldate_as_datetime(sheet1.row(i)[j].value,0)
                    list[i].append(xlrd.xldate_as_datetime(sheet1.row(i)[j].value,0))
                    #print  sheet1.row(i)[j].value.ctype
                    #print date(sheet1.row(i)[j].value).strftime("%Y-%m-%d %H:%M:%S")
                   else:
                    print  sheet1.row(i)[j].value
                    list[i].append(sheet1.row(i)[j].value)
      return list
    ##删除空数组
    # newlist=read_excel('test.xlsx')
    # while [] in newlist:
    #     newlist.remove([])
    # print newlist
    def  batch_upload(a,b,c):
        s = requests.session()
        cookies= requests.utils.dict_from_cookiejar(s.cookies)
        print cookies
        ##比如要批量关闭CNAPS的所有监控
        username=a
        password=b
        login_url = 'http://10.4.17.164:8080/balantflow/j_security_check'
        print login_url
        #password= urllib.quote(password)
        data = {'j_username': username, 'j_password':password}
        #data = urllib.urlencode(data)
        print data
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0',
            'Referer':'http://10.4.17.164:8080/balantflow/',
            'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
            #'Content-Type': 'application/json;charset=utf-8',
            'X-Requested-With': 'XMLHttpRequest'
    
        }
        responce=s.post(login_url,data=data, headers=headers,timeout=10)
        print responce.content
        print responce.status_code
        myurl1='http://10.4.17.164:8080/balantflow/'
        #print s.get(myurl1).content
    batch_upload('015208','123456','aaa')
    
    C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled/ittle/t1.py
    {}
    http://10.4.17.164:8080/balantflow/j_security_check
    {'j_username': '015208', 'j_password': '123456'}
    <html><head><title>Apache Tomcat/7.0.78 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 400 - Invalid direct reference to form login page</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>Invalid direct reference to form login page</u></p><p><b>description</b> <u>The request sent by the client was syntactically incorrect.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.78</h3></body></html>
    400
    
    Process finished with exit code 0
    
    
    解决办法:
    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    import sys
    import pycontrol.pycontrol as pc
    import time
    import csv
    import ssl
    from datetime import date,datetime
    import urllib2
    import urllib
    import cookielib
    import json
    import httplib
    import re
    import requests
    import StringIO
    import time
    import sys
    import json
    import re
    import hashlib
    import xlrd
    def read_excel(a):
      wb=xlrd.open_workbook(filename=a) #打开文件
      print wb.sheet_names() ##获取所有sheet页名字
      print type(wb.sheet_names())
      for x in wb.sheet_names():
           print type(x)
           print  x.encode('utf-8')
      sheet1=wb.sheet_by_name(u'批量提交')
      print sheet1.name,sheet1.ncols,sheet1.nrows
      b=sheet1.nrows
      list = [[] for i in range(b)]
      for i in range(sheet1.nrows):
           if i==0:
               pass
           else:
               print sheet1.row(i)
               print type(sheet1.row(i))
               for j in  xrange(len(sheet1.row(i))):
                   if j==7:
                    # print  sheet1.row(i)[j].value
                    # print  sheet1.row(i)[j].value
                    print xlrd.xldate_as_datetime(sheet1.row(i)[j].value,0)
                    list[i].append(xlrd.xldate_as_datetime(sheet1.row(i)[j].value,0))
                    #print  sheet1.row(i)[j].value.ctype
                    #print date(sheet1.row(i)[j].value).strftime("%Y-%m-%d %H:%M:%S")
                   else:
                    print  sheet1.row(i)[j].value
                    list[i].append(sheet1.row(i)[j].value)
      return list
    ##删除空数组
    newlist=read_excel('test.xlsx')
    while [] in newlist:
        newlist.remove([])
    print newlist
    def  batch_upload(a,b,c):
        s = requests.session()
        cookies= requests.utils.dict_from_cookiejar(s.cookies)
        print cookies
        ##比如要批量关闭CNAPS的所有监控
        username=a
        password=b
        myurl1='http://20.5.101.171:8080/balantflow'
        print s.get(myurl1)
        login_url = 'http://20.5.101.171:8080/balantflow/j_security_check'
        print login_url
        #password= urllib.quote(password)
        data = {'j_username': username, 'j_password':password}
        #data = urllib.urlencode(data)
        print data
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0',
            'Referer':'http://20.5.101.171:8080/balantflow/',
            'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
            'X-Requested-With': 'XMLHttpRequest'
    
        }
        responce=s.post(login_url,data=data, headers=headers,timeout=10)
        print responce.content
        print responce.status_code
        myurl2='http://20.5.101.171:8080/balantflow/'
        #print s.get(myurl2).content
    batch_upload('015208','gggggg','aaa')
    
    this is happening because if you use FORM Authentication, 
    and if you go directly to login page to login (assuming valid login userid and password), 
    the servlet engine will redirect back to where ever the last request to access restricted page (or resource) and in your case it was the login page.  
    That's why you get that "Invalid direct reference to form login page" message.  The idea is that you cannot login directly from your login page.  
    You first have to goto a page other than the login page such as index.jsp or mydata.jsp (which are restricted resource), 
    and then the web-server will automatically redirect you to the login page and once you have valid login, 
    the web-server will automatically redirect you back to the restricted page such as index.jsp or mydata.jsp, etc.  
    I hope you get the idea.  
    
  • 相关阅读:
    js参数自定义
    分页插件--记录
    .net mvc接收参数为null的解决方案
    c#枚举转字典或表格
    openlayers添加弹出框
    openlayers按坐标点播放
    openlayers轨迹匀速播放
    MyEclipse配置进行Hibernate逆映射
    BIO,NIO,AIO
    Git遇到的一点错误
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13348788.html
Copyright © 2011-2022 走看看