简体   繁体   English

WebClient.DownloadFile-无效的URI:无法解析主机名

[英]WebClient.DownloadFile - Invalid URI: The hostname could not be parsed

So I tried this in several different formats and produced different results. 因此,我尝试了几种不同的格式并产生了不同的结果。 I will include all relevant information below. 我将在下面提供所有相关信息。

My company uses a web-based application to schedule the generation of reports. 我的公司使用基于Web的应用程序来计划报告的生成。 The service emails a URL that can be clicked on and will immediately begin the "Open Save As Cancel" dialogue box. 该服务会通过电子邮件发送一个可以单击的URL,并将立即开始“打开另存为取消”对话框。 I am trying to automate the process of downloading these reports with a C# script as part of a Visual Studio project (the end goal is to import these reports in SQL Server). 我正在尝试自动化使用C#脚本将这些报告下载为Visual Studio项目的一部分的过程(最终目标是将这些报告导入SQL Server)。

I am encountering terrible difficulty initiating the download of this file using WebClient Here is the closest I have gotten with any of the methods I have tried: 我在使用WebClient初始化此文件的下载时遇到了巨大的困难,这是我尝试过的任何方法中最接近的一个:

*NOTE: I removed all identifying information from the URL, but left all special characters and the basic architecture intact. *注意:我从网址中删除了所有识别信息,但保留了所有特殊字符和基本体系结构。 Hopefully this will be a happy medium between protecting confidential info and giving you enough to understand my dilemma. 希望这将是保护机密信息与给您足够的理解我的两难选择之间的一种愉快的媒介。 The URL does work when manually copied and pasted into the address bar of internet explorer. 手动复制并粘贴到Internet Explorer的地址栏中时,URL 确实起作用。

Error Message: 错误信息:

"Invalid URI: The hostname could not be parsed." “无效的URI:无法解析主机名。”

public void Main()
{
using (var wc = new System.Net.WebClient())
{
wc.DownloadFile(
new Uri(@"http:\\webapp.locality.company.com\scripts\rds\cgigetf.exe?job_id=3058352&file_id=1&format=TAB\report.tab"), 
@"\\server\directory\folder1\folder2\folder3\...\...\...\rawfile.tab");
        }
   }

Note also that I have tried to set: 另请注意,我尝试设置:

        string sourceUri = @"http:\\webapp.locality.company.com\scripts\rds\cgigetf.exe?job_id=3058352&file_id=1&format=TAB\report.tab\abc123_3058352.tab";
        Uri uriPath;
        Uri.TryCreate(sourceUri, UriKind.Absolute, out uriPath);

But uriPath remains null - TryCreate fails. 但是uriPath仍然为空-TryCreate失败。

I have attempted doing a webrequest / webresponse / WebStream, but it still cannot find the host. 我尝试做一个webrequest / webresponse / WebStream,但是它仍然找不到主机。

I have tried including the download URL (as in my first code example) and the download URL + the file name (as in my second code example). 我尝试包括下载URL(如我的第一个代码示例)和下载URL +文件名(如我的第二个代码示例)。 I do not need the file name in the URL to initiate the download if I do it manually. 如果我手动进行下载,则不需要URL中的文件名即可启动下载。 I have also tried replacing the "report.tab" portion of the URL with the file name, but to no avail. 我也尝试用文件名替换URL的“ report.tab”部分,但无济于事。

Help is greatly appreciated as I have simply run out of thoughts on this one. 非常感谢您的帮助,因为我对此一无所获。 The only idea I have left is that perhaps one of the special characters in my URL is getting in the way, but I don't know which one that would be or how to handle it properly. 我留下的唯一想法是,URL中的一个特殊字符可能正在妨碍您的前进,但是我不知道这是哪个字符或如何正确处理它。

Thanks in advance! 提前致谢!

My first thought would be that your URI backslashes are being interpreted as escape characters, leading to a nonsense result after evaluation. 我首先想到的是,您的URI反斜杠被解释为转义字符,导致评估后出现废话。 I would try a quick test where each backslash is escaped as itself (ie "\\" instead of "\\" in each instance). 我会尝试进行一次快速测试,其中每个反斜杠均以自身形式转义(即,在每个实例中使用“ \\”而不是“ \\”)。 I'm also a little puzzled as to why your URI is not using forward slashes...? 对于您的URI为什么不使用正斜杠...,我也有些困惑。

// Create an absolute Uri from a string.
Uri absoluteUri = new Uri("http://www.contoso.com/");

Ref: Uri Constructor on MSDN 参考: MSDN上的Uri构造函数

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

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