Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello

I am trying to upload a file using this code :
static void UploadFile()
      {
          string dataBoundary = "--xyz";

          HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(serverUrl);
          webRequest.ContentType = "multipart/form-data; boundary=" + dataBoundary;
          webRequest.Method = "POST";

          string datasourceStr = String.Format(
                "--{0}\r\n"
              + "Content-Disposition: form-data; name=\"datasource\"; name=\"files\"\r\n"
              + "Content-Type: application/octet-stream\r\n"
              + "\r\n",
              dataBoundary, "sample");

          string feedtypeStr = String.Format(
                "--{0}\r\n"
              + "Content-Disposition: form-data; name=\"feedtype\"; name=\"files\"\r\n"
              + "Content-Type: application/octet-stream\r\n"
              + "\r\n",
              dataBoundary, "full");

          string fileStr = String.Format(
                "--{0}\r\n"
              + "Content-Disposition: form-data; name=\"data\"; filename=\"C:\\content.txt\"\r\n"
              + "Content-Type: application/octet-stream\r\n"
              + "\r\n",
              dataBoundary, @"C:\content.txt");

          // Get the total form post size
          string totalString = datasourceStr + feedtypeStr + fileStr;
          /*
          byte[] datasourceBytes = Encoding.Default.GetBytes(datasourceStr);
          byte[] feedtypeBytes = Encoding.Default.GetBytes(feedtypeStr);
          byte[] fileBytes = Encoding.Default.GetBytes(fileStr);
          int totalBytes = datasourceBytes.Length + feedtypeBytes.Length + fileStr.Length;
          */
          byte[] totalBytes = Encoding.Default.GetBytes(totalString);
          webRequest.ContentLength = totalBytes.Length;

          // Send the data
          Stream webStream = webRequest.GetRequestStream();
          /*        webStream.Write(datasourceBytes, 0, datasourceBytes.Length);
                    webStream.Write(feedtypeBytes, 0, feedtypeBytes.Length);
                    webStream.Write(fileBytes, 0, fileStr.Length);
           */
          webStream.Write(totalBytes, 0, totalBytes.Length);
          webStream.Close();

          // Read the response
          HttpWebResponse res = (HttpWebResponse)webRequest.GetResponse();
          StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.Default);
          string backstr = sr.ReadToEnd();
          Console.Write(backstr);
          sr.Close();
          res.Close();

      }


But its giving exception at code
HttpWebResponse res = (HttpWebResponse)webRequest.GetResponse();

as:

The remote server returned an error: (405) Method Not Allowed.


Thanks
Posted
Updated 2-Sep-11 0:22am
v2
Comments
Abhinav S 2-Sep-11 6:22am    
Code tags added for better readibility.

1 solution

This should help you out HTTP Error 405 Method not allowed[^]

Btw, that is the first hit on google
(405) Method Not Allowed[^]
 
Share this answer
 
v2
Comments
RaviRanjanKr 2-Sep-11 14:20pm    
My 5+
Simon Bang Terkildsen 2-Sep-11 14:21pm    
Thank you, Ravi

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900