Reputation: 175
I have a problem where I am trying to download two files in succession with WebClient
and the second file fails. The file is at 127.0.0.2/files/ which is running through IIS. Directory browsing is on and I can see the files in that location and download them through my browser.
FileDownload()
in WebClass
runs when the form loads, and successfully downloads state.xml. I can edit this file then check in the download directory (application path) that the file has changed. Then the form load runs ReadXml()
and if <action>
is true
in this file, the download is run with <file>
and this is where it fails. I check the directory and the file is not there. If you pause the program at the download line, you will see the second time it runs there is an unknown WebException.
I am not sure how to make this question simpler, but I think I have removed all the code not related to the problem. I have a few message boxes throughout to check the progress, and you will see from these that the file is found on the web address, but not found locally. I have ruled out folder permissions because the xml file is downloaded fine, and I was initially using a batch file, plain and zipped but switched to a text file in case it was a security problem.
Also of note: when I pause the program at the line to download the file, all the variables have the correct reading and copying the link in the line client.DownloadFile(strDownUrl, Application.StartupPath + @"\" + strSaveFile);
into my browser takes me to that file.
Edit: Web Exception: ex {"An exception occurred during a WebClient request."} System.Net.WebException
InnerException {"The given path's format is not supported."} System.Exception {System.NotSupportedException}
Below is my code:
Form1.cs
namespace Test
{
private void button1_Click(object sender, EventArgs e)
{
WebClass.FileDownload(Globals.urlFiles + Globals.xmlPath, Globals.xmlPath);
Main.ReadXml();
}
}
WebClass.cs
namespace Test
{
class WebClass
{
public static void FileDownload(string strDownUrl, string strSaveFile)
{
//var strDownUrl = Globals.url + Globals.xmlPath;
MessageBox.Show(strDownUrl);
var flag = 0;
System.Net.HttpWebRequest request = null;
System.Net.HttpWebResponse response = null;
request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(strDownUrl);
request.Timeout = 30000;
try
{
response = (System.Net.HttpWebResponse)request.GetResponse();
flag = 1;
}
catch
{
flag = -1;
}
if (flag == 1)
{
MessageBox.Show("File Found!");
}
else
{
MessageBox.Show("File Not Found!");
}
using (WebClient client = new WebClient())
{
try
{
client.DownloadFile(strDownUrl, Application.StartupPath + @"\" + strSaveFile);
}
catch (WebException ex)
{
var n = ex.Status;
MessageBox.Show("Cannot find " + strDownUrl);
}
}
}
}
}
Main.cs
namespace Test
{
public static class Globals
{
private static string prUrl = "http://127.0.0.2";
public static string url{ get { return prUrl; } set { prUrl = value; } }
private static string prUrlFiles = "http://127.0.0.2/files/";
public static string urlFiles { get { return prUrlFiles; } set { prUrlFiles = value; } }
private static string prXmlPath = "state.xml";
public static string xmlPath { get { return prXmlPath; } set { prXmlPath = value; } }
}
class Main
{
public static void ReadXml()
{
double xVersion;
bool xAction, xArchive;
string xFile;
XmlDocument doc = new XmlDocument();
doc.Load(Application.StartupPath + @"\" + Globals.xmlPath);
//GET XML VALUES
xVersion = Convert.ToDouble(doc.SelectSingleNode("XML/Program/Version").InnerText);
xAction = Convert.ToBoolean(doc.SelectSingleNode("XML/Program/Action").InnerText);
xArchive = Convert.ToBoolean(doc.SelectSingleNode("XML/Program/Action").InnerText);
xFile = Convert.ToString(doc.SelectSingleNode("XML/Request/Command/File").InnerText);
if (xAction == true)
{
MessageBox.Show("Action");
if (xFile != "")
{
WebClass.FileDownload(Globals.urlFiles + xFile, Application.StartupPath + @"\" + xFile);
}
}
}
}
}
state.xml
<?xml version="1.0" ?>
<XML>
<Program>
<Version>0.1</Version>
<Action>True</Action>
</Program>
<Request>
<Number>1</Number>
<Serial_Number></Serial_Number>
<User></User>
<Command>
<File>test.txt</File>
<Archive>False</Archive>
</Command>
</Request>
</XML>
Upvotes: 0
Views: 2792
Reputation: 1796
I believe the issue is with local file path, not the issue with the file on the server. So there is the problem with file path here:
Application.StartupPath + @"\" + xFile
I would strongly recommend to use Path.Combine
method, and work with building pathes using static Path
class. It knows how to work with the slashes automatically.
So your code should be like this:
Path.Combine(Application.StartupPath,xFile)
For building urls there are couple of classes
Also, in case there are some issues with Webclient
, or any System.Net
namespace classes, you can turn on additional logging described here:
https://msdn.microsoft.com/en-us/library/bb203855.aspx
Also, I would rewrite your downloading method to look like this:
public static void FileDownload(string strDownUrl, string strSaveFile)
{
MessageBox.Show(strDownUrl);
System.Net.HttpWebRequest request = null;
System.Net.HttpWebResponse response = null;
request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(strDownUrl);
request.Timeout = 30000;
try
{
response = (System.Net.HttpWebResponse)request.GetResponse();
Debug.WriteLine(string.Format("Content length is {0}", response.ContentLength));
Debug.WriteLine(string.Format("Content type is {0}", response.ContentType));
using (var file = File.OpenWrite(strSaveFile))
using (Stream stream = response.GetResponseStream())
{
if (stream == null)
{
Debug.WriteLine("Response is null, no file found on server");
}
else
stream.CopyTo(file);
}
}
catch(Exception e)
{
Debug.WriteLine("Error during copying:"+e);
}
}
Now you can see where exactly issue happens and what is the response from server, because you need to know if it is during the request or during the writing file to the local file system.
Upvotes: 2