请查看我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace NGH_V1
{
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("Hello. Welcome to NGH's 1st Console Application. To Continue, Please Press Enter.");
string option = Console.ReadLine();
Console.WriteLine("Press 1 to download the VIP Trainer.Press 2 for Coming Soon!.", option);
int result;
if (int.TryParse(Console.ReadLine(), out result))
{
if (result == 1)
{
Console.WriteLine("Downloading The Trainer!", option);
string remoteUri = "https://nukleus.XXX.com/dashboard/download/Trainer/XXXVIP.CETRAINER";
string fileName = XXXVIP.CETRAINER", myStringWebResource = null;
WebClient myWebClient = new WebClient();
myStringWebResource = remoteUri + fileName;
Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
myWebClient.DownloadFile(myStringWebResource, fileName);
Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
}
else if (result == 2)
Console.WriteLine("Info to be released later, ", option);
else
Console.WriteLine("No Such Option!");
}
else
Console.WriteLine("No Such Option!");
Console.ReadLine();
}
}
}
当我运行代码并按1时;我收到以下错误:System.Net.WebException:'远程服务器返回了一个错误:(500)Internal Server Error。但是,当我通过浏览器浏览完全相同的URL时,它下载的文件就很好。我在做什么错了?
答案 0 :(得分:0)
我看到代码有2个问题: 字符串文件名的值应以双引号开头, 我的意思是 字符串文件名=“ XXXVIP.CETRAINER”
假设您已在实际代码中完成此操作,则变量myStringWebResource的值将为https://nukleus.XXX.com/dashboard/download/Trainer/XXXVIP.CETRAINERXXXVIP.CETRAINER,您将传递该变量以下载文件。
如果您的文件名是XXXVIP.CETRAINER,则该网址应为 https://nukleus.XXX.com/dashboard/download/Trainer/XXXVIP.CETRAINER
因此,基本上,代码应如下所示:
if (result == 1)
{
Console.WriteLine("Downloading The Trainer!", option);
string remoteUri = "https://nukleus.XXX.com/dashboard/download/Trainer/XXXVIP.CETRAINER";
string fileName = "XXXVIP.CETRAINER";
WebClient myWebClient = new WebClient(); Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, remoteUri);
myWebClient.DownloadFile(remoteUri, fileName);
Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, remoteUri);
}