使用下载弹出窗口自动从URI下载xml文件

时间:2023-02-06 07:37:54

ASP.NET MVC 4 Razor:

ASP.NET MVC 4 Razor:

I've been working at this for a bit, so I apologize if I'm missing something obvious, but I will truly appreciate any assistance that could be offered.

我已经在这方面工作了一段时间,所以如果我遗漏了一些明显的东西,我会道歉,但我会非常感谢能提供的任何帮助。

In a nutshell, what I'm looking to do is download an XML file from a URI using C#. It ought to be pretty straightforward, but the URI leads to a blank page with a download prompt popup populated with a dynamically created filename.

简而言之,我要做的是使用C#从URI下载XML文件。它应该非常简单,但URI会导致一个空白页面,其下载提示弹出窗口中填充了动态创建的文件名。

I can't provide the URI due to its confidential nature, but here is the code I've been toying with. (Forgive my ignorance on this matter, it's the first time I've tried anything like this)

由于其机密性质,我无法提供URI,但这里是我一直在玩的代码。 (原谅我对这件事的无知,这是我第一次尝试这样的事情)

byte[] data;
using (WebClient Client = new WebClient())
{
    data = Client.DownloadData(uriString + fileString);
}
File.WriteAllBytes(dirString + fileString, data);

I've also tried:

我也尝试过:

using (WebClient Client = new WebClient())
{
    Client.DownloadFile(uriString + fileString, dirString + fileString);
}

To be honest, this code doesn't really work for me. The downloaded files aren't correct. The XML files appear to contain the code from the webpage they've been downloaded from, and if I try something like an image, the image is broken. So, again, any assistance would be appreciated.

说实话,这段代码并不适合我。下载的文件不正确。 XML文件似乎包含他们从中下载的网页中的代码,如果我尝试像图像一样,图像就会被破坏。所以,再次,任何援助将不胜感激。

Thanks in advance!

提前致谢!

1 个解决方案

#1


0  

The URI that you are using is probably wrong. You are using the URI that opens the popup page. The popup page should be doing another GET to the dynamically generated file.

您使用的URI可能是错误的。您正在使用打开弹出页面的URI。弹出页面应该对动态生成的文件执行另一个GET。

To automate this process, you should use a WebRequest to get the contents of the popup page. Scrape the contents of the page to get the actual URL to download the file. Then use the code you have written to download the file.

要自动执行此过程,您应使用WebRequest获取弹出页面的内容。抓取页面内容以获取下载文件的实际URL。然后使用您编写的代码下载文件。

var request = WebRequest.Create("PopupUrl");
var response = request.GetResponse();
string url = GetUrlFromResponseByRegExOrXMLParsing();
var client = new WebClient();
webClient.DownloadFile(url, filePath);

#1


0  

The URI that you are using is probably wrong. You are using the URI that opens the popup page. The popup page should be doing another GET to the dynamically generated file.

您使用的URI可能是错误的。您正在使用打开弹出页面的URI。弹出页面应该对动态生成的文件执行另一个GET。

To automate this process, you should use a WebRequest to get the contents of the popup page. Scrape the contents of the page to get the actual URL to download the file. Then use the code you have written to download the file.

要自动执行此过程,您应使用WebRequest获取弹出页面的内容。抓取页面内容以获取下载文件的实际URL。然后使用您编写的代码下载文件。

var request = WebRequest.Create("PopupUrl");
var response = request.GetResponse();
string url = GetUrlFromResponseByRegExOrXMLParsing();
var client = new WebClient();
webClient.DownloadFile(url, filePath);