1

I'm trying to upload files to a remote server (windows server 2008 R2) from my asp.net 1.1 (C#) Windows application (I know.. It's really old, sadly, that's what we have). When I try to upload, it's giving me an error: "The remote server returned an error: (404) Not Found.". Here's the code I'm using: Any ideas?

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uploadUrl);
req.Credentials = new NetworkCredential(uName,pwd);
req.Method = "PUT";
req.AllowWriteStreamBuffering = true;

// Retrieve request stream 
Stream reqStream = req.GetRequestStream();

// Open the local file
FileStream rdr = new FileStream(txt_filename.Text, FileMode.Open);

// Allocate byte buffer to hold file contents
byte[] inData = new byte[4096];

// loop through the local file reading each data block
//  and writing to the request stream buffer
int bytesRead = rdr.Read(inData, 0, inData.Length);
while (bytesRead > 0)
{
    reqStream.Write(inData, 0, bytesRead);
    bytesRead = rdr.Read(inData, 0, inData.Length);
}

rdr.Close();
reqStream.Close();

req.GetResponse();

The uploadUrl is like this: http://10.x.x.x./FolderName/Filename

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Abhi
  • 141
  • 2
  • 4
  • 17
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Feb 20 '15 at 17:00
  • 1
    If the server is telling you "not found", then the chances are that you're using the wrong URL. Also, your two streams and your WebResponse should be in `using` blocks. – John Saunders Feb 20 '15 at 17:01
  • Thank you John. Apologize about the tags in title. When I open the URL in a browser, it works fine. That's why it has me stumped. – Abhi Feb 20 '15 at 17:04
  • 1
    When you open the URL in a browser, you are using a GET, not a PUT. Maybe you meant "POST"? – John Saunders Feb 20 '15 at 17:06
  • I'm getting the same error when I use "POST". – Abhi Feb 20 '15 at 17:19
  • You need to find out from the "person" who owns the URL what the correct URL is. Also, try `http://10.x.x.x/FolderName/FileName` without the trailing `.` in the host part of the URL. – John Saunders Feb 20 '15 at 19:08
  • @John,That trailing . was a typo on this post. Sorry about that. – Abhi Feb 24 '15 at 16:10

1 Answers1

0

Please use "POST" method instead of "PUT" and I guess it will work.

Edit:

Check the code below, it will help you.

 public void UploadFile()
    {
        string fileUrl = @"enter file url here";
        string parameters = @"image=" + Convert.ToBase64String(File.ReadAllBytes(fileUrl));
        WebRequest req = WebRequest.Create(new Uri("location url here"));

        req.ContentType = "application/x-www-form-urlencoded";
        req.Method = "POST";
        byte[] bytes = Encoding.ASCII.GetBytes(parameters);

        try
        {
            req.ContentLength = bytes.Length;
            Stream s = req.GetRequestStream();
            s.Write(bytes, 0, bytes.Length);
            s.Close();
        }
        catch (WebException ex)
        {
            throw ex; //Request exception.
        }

        try
        {
            WebResponse res = req.GetResponse();
            StreamReader sr = new StreamReader(req.GetResponseStream());
        }
        catch (WebException ex)
        {
            throw ex; //Response exception.
        }
    }

Enter fileUrl variable correctly and take care of uri while creating WebRequest instance, write the url of the locating folder.

mrciga
  • 47
  • 8
  • Actually, the uploadUrl is wrong and the problem caused by that. You should pass the link of the folder not link of the image or file. Please check it. – mrciga Feb 20 '15 at 17:28
  • Thank you, When I just use the Link of the folder, I now get a message saying: 405 Method not allowed. – Abhi Feb 20 '15 at 17:38
  • Thanks for the response, but I'm using asp.net 1.1, Methods like : File.ReadAllBytes don't exist. – Abhi Feb 20 '15 at 17:46
  • Are you sure that, the server supports "POST" method? That error causes due to that generally. – mrciga Feb 20 '15 at 17:48
  • Where do I check that? – Abhi Feb 20 '15 at 17:53
  • Please "Google" it, you should understand the basics of HTTP methods such as "POST, "GET", "PUT" etc. You will understand what is the problem, why that method is not allowed but for now, we solved the problem on the title. – mrciga Feb 20 '15 at 17:59
  • @Abhi: if POST doesn't work for you, then this answer should not be accepted – John Saunders Feb 20 '15 at 19:07