如何从SSIS发出HTTP请求? [英] How to make an HTTP request from SSIS?
问题描述
我很想知道如何从SSIS进行HTTP调用。例如,我希望能够从 http://www.domain.com/resource.zip
下载文件,并记录下载的日期时间和目的地驱动器上的文件。我还想捕获文件大小等属性并捕获日期和时间。下载完成的时间。
I'm interested in knowing how I can make an HTTP call from SSIS. For example, I would like to be able to download a file from http://www.domain.com/resource.zip
and record the datetime of the download and the destination of the file on the drive. I would also like to capture such attributes as file size and capture the date & time when the download completed.
推荐答案
您可以使用命名空间 System.Net.WebClient
在SSIS中借助脚本任务
进行Http请求。以下示例显示了如何实现这一目标。该示例是在 SSIS 2008 R2
中创建的。
You can make use of the namespace System.Net.WebClient
to make the Http request with the help of Script Task
in SSIS. Following example shows how this can be achieved. The example was created in SSIS 2008 R2
.
分步流程:
-
创建一个新的SSIS包并创建两个变量,即 RemoteUri 和 LocalFolder 即可。设置变量
RemoteUri
,其值为http://www.google.com/intl/en_com/images/srpr/logo1w.png
。这是Google主页上徽标的图片网址。使用值C:\temp \
设置变量LocalFolder
。这是我们要保存内容的路径。请参阅屏幕截图# 1 。
Create a new SSIS package and create two variables namely RemoteUri and LocalFolder. Set the variable
RemoteUri
with the valuehttp://www.google.com/intl/en_com/images/srpr/logo1w.png
. this is the image url of the logo on the Google's home page. Set the variableLocalFolder
with the valueC:\temp\
. this is the path where we are going to save the content. Refer screenshot #1.
在SSIS包上,放置脚本任务
。使用脚本任务代码部分下提供的代码替换脚本任务中的Main()方法。请参阅屏幕截图# 2 。
On the SSIS package, place a Script Task
. Replace the Main() method within the script task with the code provided under the Script Task Code section. Refer screenshot #2.
屏幕截图# 3 显示路径 C:\ temp \
为空。
Screenshot #3 shows that the path C:\temp\
is empty.
屏幕截图# 4 显示成功执行了包。
Screenshot #4 shows successful execution of the package.
屏幕截图# 5 显示内容(在本例中为徽标图片)已下载到本地文件夹路径。
Screenshot #5 shows that the content (in this case the logo image) has been downloaded to the local folder path.
屏幕截图# 6 显示代码已经过测试,可以下载.zip文件。为此,使用需要下载的内容网址更改了变量 RemoteUri 的值。
Screenshot #6 shows that the code was tested to download a .zip file. To achieve this, the value of the variable RemoteUri was changed with the content url that needs to be downloaded.
脚本任务代码:
C#代码只能在中使用 SSIS 2008及以上
。
C# code that can be used only in SSIS 2008 and above
.
public void Main()
{
Variables varCollection = null;
Dts.VariableDispenser.LockForRead("User::RemoteUri");
Dts.VariableDispenser.LockForRead("User::LocalFolder");
Dts.VariableDispenser.GetVariables(ref varCollection);
System.Net.WebClient myWebClient = new System.Net.WebClient();
string webResource = varCollection["User::RemoteUri"].Value.ToString();
string fileName = varCollection["User::LocalFolder"].Value.ToString() + webResource.Substring(webResource.LastIndexOf('/') + 1);
myWebClient.DownloadFile(webResource, fileName);
Dts.TaskResult = (int)ScriptResults.Success;
}
屏幕截图#1:
屏幕截图#2:
屏幕截图#3:
屏幕截图#4:
屏幕截图#5:
屏幕截图#6:
这篇关于如何从SSIS发出HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!