简体   繁体   English

SSIS 脚本:部分 Uri 上的 HTTPWebRequest 和 Webclient.Downloadfile 失败

[英]SSIS Script: HTTPWebRequest and Webclient.Downloadfile failing on part of Uri

I am trying to download a file from an API, which works perfectly well in chrome using the following Url:我正在尝试从 API 下载文件,该文件在 chrome 中使用以下 URL 运行良好:

https://ec.europa.eu/eurostat/api/dissemination/sdmx/2.1/data/namq_10_pc/.CP_EUR_HAB.NSA.B1GQ.?startPeriod=2017&format=SDMX_2.1_STRUCTURED https://ec.europa.eu/eurostat/api/dissemination/sdmx/2.1/data/namq_10_pc/.CP_EUR_HAB.NSA.B1GQ.?startPeriod=2017&format=SDMX_2.1_STRUCTURED

However, when I try to download this in ac# script in SSIS using either HTTPWebRequest or Webclient.Downloadfile it fails (404 response error).但是,当我尝试使用HTTPWebRequestWebclient.Downloadfile在 SSIS 中的 ac# 脚本中下载它时,它失败(404 响应错误)。

After many hours scouring Stackoverflow, trying numerous approaches, I have found that both approaches do work if I remove part of the Url:经过数小时的 Stackoverflow 搜索,尝试了多种方法,我发现如果删除部分 Url,两种方法都可以工作:

https://ec.europa.eu/eurostat/api/dissemination/sdmx/2.1/data/namq_10_pc/?startPeriod=2017&format=SDMX_2.1_STRUCTURED https://ec.europa.eu/eurostat/api/dissemination/sdmx/2.1/data/namq_10_pc/?startPeriod=2017&format=SDMX_2.1_STRUCTURED

Why does the inclusion of ".CP_EUR_HAB.NSA.B1GQ."为什么包含“.CP_EUR_HAB.NSA.B1GQ”。 make it fail in c# but not in chrome?使它在 c# 中失败但在 chrome 中失败?

The code I am using:我正在使用的代码:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(sourceFilePath));
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (Stream file = File.Create(savePathAndName))
{
     CopyStream(stream, file);
}

public static void CopyStream(Stream input, Stream output)
{
    byte[] buffer = new byte[8 * 1024];
    int len;
    while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
    {
         output.Write(buffer, 0, len);
    }
}

UPDATE Originally, SSIS was omitted, as I thought it would be irrelevant, however, after the comment saying that they could get it working, I tested what the Uri is returning, and it is removing the last dot before the question mark:更新最初,SSIS 被省略了,因为我认为它无关紧要,但是,在评论说他们可以让它工作之后,我测试了 Uri 返回的内容,并且它删除了问号前的最后一个点:

https://ec.europa.eu/eurostat/api/dissemination/sdmx/2.1/data/namq_10_pc/.CP_EUR_HAB.NSA.B1GQ?startPeriod=2017&format=SDMX_2.1_STRUCTURED https://ec.europa.eu/eurostat/api/dissemination/sdmx/2.1/data/namq_10_pc/.CP_EUR_HAB.NSA.B1GQ?startPeriod=2017&format=SDMX_2.1_STRUCTURED

There are many documented difficulties in getting these dot segments included: just a couple include:包含这些点段有许多记录在案的困难:只有几个包括:

Is there a way to keep dot segments in url using Uri class? 有没有办法使用 Uri 类在 url 中保留点段? and A url resource that is a dot (%2E)一个 url 资源,它是一个点 (%2E)

The API returns a failure if that dot isn't there.如果该点不存在,API 将返回失败。 And microsoft confirms this:微软证实了这一点:

https://docs.microsoft.com/en-us/dotnet/api/system.uri?view=net-5.0 https://docs.microsoft.com/en-us/dotnet/api/system.uri?view=net-5.0

After some investigation, it seems that this might be to do with the framework, and updating it might be the answer, which might suggest why others could get this working, but not me in SSIS.经过一番调查,似乎这可能与框架有关,更新它可能是答案,这可能表明为什么其他人可以使这个工作,而不是我在 SSIS 中。 The framework that SSIS 2017 is using is bound to 4.5. SSIS 2017 使用的框架绑定到 4.5。 I'm thinking that I might be at a dead-end with this.我想我可能会陷入困境。

It was the trailing dot in the section ".CP_EUR_HAB.NSA.B1GQ."它是“.CP_EUR_HAB.NSA.B1GQ”部分的尾随点。 that was causing the issue.这导致了这个问题。 the Uri removes dot segments automatically, and it appears there is no way round this. Uri 会自动删除点段,似乎没有办法解决这个问题。

With it being used in SSIS 207, there is no way for me to change the target framework, which Microsoft now ties to the version of SSIS.由于它在 SSIS 207 中使用,我无法更改目标框架,Microsoft 现在将其与 SSIS 版本相关联。

The solution was to instead of having the trailing "."解决方案是,而不是使用尾随的“.”。 which the API requires to specify the country wild card, I had to instead list each country code in the URL: API 需要指定国家/地区通配符,我不得不在 URL 中列出每个国家/地区代码:

https://ec.europa.eu/eurostat/api/dissemination/sdmx/2.1/data/namq_10_pc/.CP_EUR_HAB.NSA.B1GQ.UK+BE+BG+CZ+DK+DE+EE+IE+EL+ES+FR+HR+IT+CY+LV+LT+LU+HU+MT+NL+AT+PL+PT+RO+SI+SK+FI+SE+IS+NO+CH+ME+MK+AL+RS+TR+BA+XK?startPeriod=2017&format=SDMX_2.1_STRUCTURED https://ec.europa.eu/eurostat/api/dissemination/sdmx/2.1/data/namq_10_pc/.CP_EUR_HAB.NSA.B1GQ.UK+BE+BG+CZ+DK+DE+EE+IE+EL+ES +FR+HR+IT+CY+LV+LT+LU+HU+MT+NL+AT+PL+PT+RO+SI+SK+FI+SE+IS+NO+CH+ME+MK+AL+RS +TR+BA+XK?startPeriod=2017&format=SDMX_2.1_STRUCTURED

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM