Click here to Skip to main content
14,580,263 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello,

How to Get Files from Http Server using c# .net?


I want to get selected files from web server using c# .net.

suppose my https server url: "https://abc.com/images/Upload/"

Now its getting error.
Error:
The remote server returned an error: (404) Not Found.

Please help me.

Thanks in Advance.

Ankit Agarwal
Software Engineer

What I have tried:

public void ExportImages(List<FileDetails> fd)
        {

            foreach (var f in fd)
            {
                HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("https://abc.com/images/Upload/" + f.XMarksSheetName + "," + f.XCertificateName + "," + f.XIIMarksSheetName + "," + "," + f.XIICertificateName + "," + f.GMarksSheetName + "," + f.GCertificateName + "," + f.FileName + "," + f.FileNameNationalId + "," + f.FileNamePassport);
                httpRequest.Method = WebRequestMethods.Http.Get;
                HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
                //Stream httpResponseStream = httpResponse.GetResponseStream();  //Error: The remote server returned an error: (404) Not Found.
                StreamReader sr = new StreamReader(httpResponse.GetResponseStream());
                var results = sr.ReadToEnd();
                sr.Close();
            }
                

        }
Posted
Updated 29-Jun-20 23:00pm
v4
Comments
OriginalGriff 30-Jun-20 4:01am
   
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?
Where are you stuck?
What help do you need?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
Agarwal1984 30-Jun-20 4:06am
   
f.XMarksSheetName etc. are my file names and file already stored in "https://abc.com/images/Upload/".
I want to try get files according to my file names.
My simple question is "How to get selected files from https server path"?
Richard MacCutchan 30-Jun-20 5:04am
   
The 404 error tells you that your URL is not valid. So start by printing the generated URL so you can actually see what is wrong with it.
Agarwal1984 30-Jun-20 5:15am
   
Suppose my url: https://abc.com/images/Upload
and files: XMarksheet.pdf,XCertificate.pdf etc. in my https folder.
my simple question i want to get files from path so we can use files further.
Richard MacCutchan 30-Jun-20 5:36am
   
It is no good saying "Suppose ...", you need to see exactly what your code is doing in order to find out why it does not work. As a simple test you could post that URL and filename into your browser to see whether abc.com accepts it.
Agarwal1984 30-Jun-20 5:42am
   
I want to ask one question:-
is this correct way for find file name in path:-
https://abc.com/images/Upload/" + f.XMarksSheetName + "," + f.XCertificateName + "," + f.XIIMarksSheetName + "," + "," + f.XIICertificateName + "," + f.GMarksSheetName + "," + f.GCertificateName + "," + f.FileName + "," + f.FileNameNationalId + "," + f.FileNamePassport.

or can you suggest me right way and what i use get file list from this path.
Richard MacCutchan 30-Jun-20 5:52am
   
I have no idea. Ask the people who own the website.
Agarwal1984 30-Jun-20 5:19am
   
if we use local directory, so we can easily pick the files using:-

var directory = new DirectoryInfo(filePath);
var myFile = (from f in directory.GetFiles()
select f).First();

but my issue with https we server.

Directory info not working with https web server.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

The first thing I notice is that even if that did work, it wouldn't return "results" - it would return a single result, the last file read - because inside your loop you "throw away" the previous results when you call ReadToEnd to fetch the next.

But that doesn;t matter, because results itself goes out of scope at the end of the loop, and can't be returned anyway ... As a result, your code won't even compile!

Plus, the method is declared as returning a void - no value - so it can't return anything to the outside world anyway.
   
v2
Comments
Agarwal1984 30-Jun-20 4:11am
   
Please give me a solution, how can we get files from https server path?
OriginalGriff 30-Jun-20 4:32am
   
If I was you, I would start with code that compiles ...
You are, after all, a "Software Developer (Senior)", and even a junior will know that throwing crap together and hoping it compiles isn't going to work!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100