zoukankan      html  css  js  c++  java
  • Submit a POST form and download the result web page

    Submit a POST form and download the result web page - IronPython Cookbook

    Submit a POST form and download the result web page

    From IronPython Cookbook

    • prepare a request object, that is created for a given URI
    • write the PARAMETERS string to the request stream.
    • retrieve the response and read from its stream.
    URI = 'http://www.example.com'
    PARAMETERS="lang=en&field1=1"
    
    from System.Net import WebRequest
    request = WebRequest.Create(URI)
    request.ContentType = "application/x-www-form-urlencoded"
    request.Method = "POST"
    
    from System.Text import Encoding
    bytes = Encoding.ASCII.GetBytes(PARAMETERS)
    request.ContentLength = bytes.Length
    reqStream = request.GetRequestStream()
    reqStream.Write(bytes, 0, bytes.Length)
    reqStream.Close()
    
    response = request.GetResponse()
    from System.IO import StreamReader
    result = StreamReader(response.GetResponseStream()).ReadToEnd()
    print result
    

    This uses the System.Net.WebRequest class.


    Here is a simple function, which works whether you are making a 'POST' or a 'GET':

    from System.Net import WebRequest
    from System.IO import StreamReader
    from System.Text import Encoding
    
    def UrlOpen(uri, parameters=None):
        request = WebRequest.Create(uri)
        if parameters is not None:
            request.ContentType = "application/x-www-form-urlencoded"
            request.Method = "POST"
            bytes = Encoding.ASCII.GetBytes(parameters)
            request.ContentLength = bytes.Length
            reqStream = request.GetRequestStream()
            reqStream.Write(bytes, 0, bytes.Length)
            reqStream.Close()
    
        response = request.GetResponse()
        result = StreamReader(response.GetResponseStream()).ReadToEnd()
        return result
    


    Back to Contents.

  • 相关阅读:
    使用UOS微信桌面版协议登录,wechaty免费版web协议又可以用了
    angular之$watch方法详解
    webpack配置这一篇就够
    select设置disable后ie修改默认字体颜色暂时解决
    201901251946
    new year
    test
    mysql密码忘记解决方法
    bianmayujianmatest
    jinzhizhuanhuan
  • 原文地址:https://www.cnblogs.com/lexus/p/2816478.html
Copyright © 2011-2022 走看看