从sis包中发布http请求的最佳方法是什么

时间:2019-02-26 13:50:43

标签: ssis

我想通过我的sis程序包调用http发布请求。但是不确定什么是调用rest api post方法的最佳方法。请帮忙。

2 个答案:

答案 0 :(得分:0)

您可以在脚本任务中使用此方法使用httpclient进行api调用:

    public void Post(Produto produto)
    {
        var client = new HttpClient;
        client.BaseAddress = new Uri("https://localhost:5001/api/"); 
        client.DefaultRequestHeaders.Clear();
        //client.DefaultRequestHeaders.Accept.Clear();
        //client.DefaultRequestHeaders.Add("ApiKey", "");
        var jsonBody = JsonConvert.SerializeObject(produto);
        client.PostAsync("createproduto", new StringContent(jsonBody, Encoding.UTF8, "application/json"));            
}

答案 1 :(得分:0)

借助SSIS中的脚本任务,可以利用名称空间System.Net.WebClient发出Http请求。以下示例显示了如何实现。该示例是在SSIS 2008 R2中创建的。

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);

 byte[] data;
 using (WebClient client = new WebClient())
 {
     data = client.DownloadData(webResource);
 }
 FileInfo file = new System.IO.FileInfo(fileName);
 file.Directory.Create(); // If the directory already exists, this method does nothing.
 File.WriteAllBytes(file.FullName, data);

 Dts.TaskResult = (int)ScriptResults.Success;
}