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.

  • 相关阅读:
    ASP.NET常用的三十三种代码
    asp.net获取IP地址
    Inside Microsoft Sql Server 2005 TSQL Programming 学习笔记
    动态SQL与SQL注入(一)动态SQL
    (二)SQL 注入
    WCF 安全
    C# 运算符重载和 implicit关键字
    分页那回事
    thinking
    Moss css
  • 原文地址:https://www.cnblogs.com/lexus/p/2816478.html
Copyright © 2011-2022 走看看