简体   繁体   English

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

[英]Automatically download an xml file from URI with download popup

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

I can't provide the URI due to its confidential nature, but here is the code I've been toying with. 由于其机密性质,我无法提供URI,但这里是我一直在玩的代码。 (Forgive my ignorance on this matter, it's the first time I've tried anything like this) (原谅我对此事的无知,这是我第一次尝试这样的事情)

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. XML文件似乎包含他们从中下载的网页中的代码,如果我尝试像图像一样,图像就会被破坏。 So, again, any assistance would be appreciated. 所以,再次,任何援助将不胜感激。

Thanks in advance! 提前致谢!

The URI that you are using is probably wrong. 您使用的URI可能是错误的。 You are using the URI that opens the popup page. 您正在使用打开弹出页面的URI。 The popup page should be doing another GET to the dynamically generated file. 弹出页面应该对动态生成的文件执行另一个GET。

To automate this process, you should use a WebRequest to get the contents of the popup page. 要自动执行此过程,您应该使用WebRequest来获取弹出页面的内容。 Scrape the contents of the page to get the actual URL to download the file. 刮取页面内容以获取下载文件的实际URL。 Then use the code you have written to download the file. 然后使用您编写的代码下载文件。

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

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

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