Submit a POST form and download the result web page
2012-12-13 15:37
411 查看
Submit a POST form and download the result web page - IronPython Cookbook
write the PARAMETERS string to the request stream.
retrieve the response and read from its stream.
Here is a simple function, which works whether you are making a 'POST' or a 'GET':
Back to Contents.
Submit a POST form and download the result web page
From IronPython Cookbook
prepare a request object, that is created for a given URIwrite 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 resultThis 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.
相关文章推荐
- Craw the data of the web page and parse to pdf
- 如何打印SharePOint 一部分,而不是整个页面Printing just the web part and not the entire SharePoint page
- IIS Error:404.2 The page you are requesting cannot be served because of the ISAPI and CGI Restriction list settings on the Web server
- post the hexo blog through web page
- HTTP Error 404.2 - Not Found The page you are requesting cannot be served because of the ISAPI and CGI Restriction list settings on the Web server(转)
- After upgrading the ebPart Assembly "Web Part Error: A Web Part or Web Form Control on this Page cannot be displayed .."
- NSURLRequest : Post data and read the posted page
- [Drupal] Submit and redirect back to the modify page
- cannot use the same dataset for report.dataset and page.dataset
- How can selenium web driver get to know when the new window has opened and then resume its execution
- [Angular2 Form] Understand the Angular 2 States of Inputs: Pristine and Untouched
- Sharepoint 2013 安装中 Config Application Server and Web Server role for the Server 失败 (iisext.vbs)
- Disabling Mobile Web Page Access in SharePoint Server 2010 and SharePoint Foundation 2010
- Methods GET and POST in HTML forms - what's the difference?
- The Difference Between “Add Web Reference” and “Add Service Reference”
- WebClient post data to .aspx page
- calculate all controls size and position in the form when the form resize
- 【转帖】标准化设计解决方案 - 标记语言和样式手册 (Web Standards Solutions The Markup and Style Handbook)
- 【转】theform.submit(); 对像不支持些属性!
- Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The s