QTP 通过URL地址下载文件到本地(转)

While automation, you may come to situations where you need to need to download a file on clicking a link. This generally involves a lot of User Interface (GUI)overhead like syncing the download box, clicking the buttons, managing the Save As box, etc. This many a time causes sync issues. Moreover, we end up automating something that is not at all needed to be automated or tested. In this situation, all you need is a code snippet in Visual Basic for Quick Test Professional that automatically downloads the file in the background without the need of any GUI appearances. You just need to provide the download link and the file path in your system. The function "funcDownloadFile" below takes the file path and the download link as parameters in String and performs the download in the background. You just need to capture the download URL from the potential download link and call this function and you will find the download completed at the specified path.

The function supports authenticated downloads and proxy settings. The function is based onADODB STREAM object and WinHTTP API from Microsoft.

Sub utilDownloadFile(strFilePath, strURL)  

   Set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")  

   ‘WinHttpReq.SetProxy HTTPREQUEST_PROXYSETTING_PROXY, "xxx.xxx.xxx.xxx:zzzz"
   ‘Required only if your internet routes through a proxy. Not required in 90% cases.
   ‘You can ignore this line for first attempt but add it if your download is hindered, X is IP and Z is Port  

   temp = WinHttpReq.Open("POST", strURL, false)  

   ‘WinHttpReq.SetCredentials "Username", "Password", HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
   ‘Required only if the file download server required authentication. Not required in 90% cases. Change Username and Password wuth actuals.  

   WinHttpReq.Send()
   WinHttpReq.WaitForResponse  

   strResult = WinHttpReq.ResponseBody  

   Set oStream = createobject("Adodb.Stream")
   Const adTypeBinary = 1
   Const adSaveCreateOverWrite = 2  

   oStream.type = adTypeBinary
   oStream.open
   oStream.write strResult
   oStream.savetofile strFilePath, adSaveCreateOverWrite  

End Sub  
时间: 2024-11-05 13:16:22

QTP 通过URL地址下载文件到本地(转)的相关文章

在MFC中通过访问IP地址下载文件到本地

void CDownLoad::OnBnClickedOk() { // TODO: 在此添加控件通知处理程序代码 CDialogEx::OnOK(); UpdateData(TRUE); CString sPath = m_savePath;//下载文件的保存地址 CString m_theUrl = m_ipPath;//下载的网址 CString filename = sPath + (_T("11.mdb"));//下载文件的保存名 CInternetSession sessi

根据图片的url地址下载图片到本地保存代码片段

public void download(String urlString, String filename,String savePath) throws Exception { // 构造URL URL url = new URL(urlString); // 打开连接 URLConnection con = url.openConnection(); //设置请求超时为5s con.setConnectTimeout(5*1000); // 输入流 InputStream is = con

PHP CURL实现远程下载文件到本地

<?php //$result=httpcopy('http://www.phpernote.com/image/logo.gif'); echo '<pre>';print_r($result); function httpcopy($url,$file='',$timeout=60){ $file=empty($file)?pathinfo($url,PATHINFO_BASENAME):$file; $dir=pathinfo($file,PATHINFO_DIRNAME); !i

c#.net从ftp下载文件到本地

c#.net从ftp下载文件到本地    /*首先从配置文件读取ftp的登录信息*/ string TempFolderPath = System.Configuration.ConfigurationManager.AppSettings["TempFolderPath"].ToString(); string FtpUserName = System.Configuration.ConfigurationManager.AppSettings["FtpUserName&q

java 从网络Url中下载文件

转自:http://blog.csdn.net/xb12369/article/details/40543649 /** * 从网络Url中下载文件 * @param urlStr * @param fileName * @param savePath * @throws IOException */ public static void downLoadFromUrl(String urlStr,String fileName,String savePath) throws IOExcepti

C# Http方式下载文件到本地

下文代码是从网络(http://www.cnblogs.com/hayden/archive/2012/04/26/2472815.html)得来,亲测好用.我中修改了下格式和注释,版权属于原作者. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Net; namespace ConsoleTest { class

Android从网络某个地址下载文件、写入SD卡

首先创建一个HttpDownloader类,获取下载文件的网络地址,将文件下载下来以String流的方式返回: public String download(String urlStr){ //urlStr为文件的网络地址,如http://192.168.1.105:8080/mp3/resources.xml StringBuffer sb = new StringBuffer(); String line = null; BufferedReader buffer = null; try {

本地上传文件到服务器,从服务器下载文件到本地

最近在做项目的时候涉及到了文件的上传.下载,以前学习IO时也没有搞得多清楚,在网上找了些上传下载的例子,然后修改了部分.经测试,上传下载文件暂时能用,下面是上传和下载的方法: 1.本地上传文件到服务器 html代码: <form id="uploadDatumInfo" name="uploadDatumInfo" method="post" enctype="multipart/form-data" target=&q

通过pscp从linux服务器下载文件到本地windows操作系统记载

从linux 服务器下载数据到本地windows操作系统 1) 下载 putty.exe 完整安装包. 2) 将pscp.exe程序 放在C:/windows/system32文件下(或者配置pscp.e xe的系统变量). 3)   打开windows cmd.exe命令行窗口 输入pscp命令看是否能够运行. 4)   在命令行运行pscp [email protected]:/usr/tideway/java/integrations/mappings/extended-rdb/Oracl