Hi ,
I'm trying to upload a file to a remote server using HTTPWebRequest (both PUT and POST) (We are using asp.net 1.1, there's no FTPWebrequest in 1.1 and Webclient doesn't work either). But when I run the code, it gives me this error:
The remote server returned an error: (405) Method Not Allowed
Below is the code I'm using. Please let me know what am I doing wrong here.
try { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"http://FolderPathASURL/"); req.Credentials = new NetworkCredential(@"username", "password"); req.Method = "PUT"; //Tried POST as well req.AllowWriteStreamBuffering = true; // Retrieve request stream Stream reqStream = req.GetRequestStream(); // Open the local file FileStream rdr = new FileStream(@"C:\TestFile.txt", 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(); } catch (System.Net.WebException we) { Console.WriteLine(we.Message); if (we.Status == WebExceptionStatus.ProtocolError) Console.WriteLine(we.Response.ResponseUri); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadLine(); }
Abhi