3

I have run into an issue where the following code works on a Windows 2003 machine and now that we are upgrading to 2008 and it no longer works. When the code gets to the getResponse(), it is giving me a 405 error.

I'm writing to a virtual directory through IIS.
I also tried making it an application and it makes no difference. We don't have webdav installed/enabled.

I have searched everywhere and can not seem to find the correct solution to allow me to write the file.

I've included the base code that is used to upload the files...

saveLocation.Length = 0;
saveLocation.Append(Path.GetFileName(files[0]));

   inStream = File.Open(files[0], FileMode.Open);
    HttpWebRequest myWebRequest =    (HttpWebRequest)WebRequest.Create("http://localhost/kw/archive/testFile.pdf");

      myWebRequest.ContentType = contentType;
      myWebRequest.Method = "put";
      //myWebRequest.Headers.Add("action", "put");
      myWebRequest.Headers.Add("filename", saveLocation.ToString());
      myWebRequest.ContentLength = inStream.Length;

      dataToRead = (int)inStream.Length;
      outStream = myWebRequest.GetRequestStream();

      while (dataToRead > 0) {
        if (Response.IsClientConnected) {
          length = inStream.Read(buffer, 0, 10000);
          outStream.Write(buffer, 0, length);
          buffer = new Byte[10000];
          dataToRead = dataToRead - length;
        }
        else {
          dataToRead = -1;
        }
      }

      HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();

      myWebResponse.Close();
      inStream.Close();
      outStream.Close();

update: I am currently getting a 405 error.

Jeff Moore
  • 444
  • 2
  • 10

3 Answers3

0

405 error means method not allowed.In the code above you are using "PUT" method i think this method is not allowed in IIS. Try to enable this and then check this. If you have idea how to enable this than try it and if you don't than check this link.I hope this will solve your problem.

Community
  • 1
  • 1
Mairaj Ahmad
  • 14,434
  • 2
  • 26
  • 40
0

405 Means Method Not allowed - the most likely cause for this is that the PUT verb has not been allowed via IIS Settings. I believe that this is a security setting that got tightened up between IIS6 and IIS7 in that PUT and DELETE requests are now blocked by default.

To enable PUT requests in IIS Manager under the site settings there is an icon labelled Request Filtering. If you open this you will see a number of tabs one of which is called HTTP VERBS, switch to this. Select Allow Verb from the Actions Panel on the right and type PUT in the dialog that appears and select OK.

Alternatively you can add the following to your web.config file:

<security>
    <requestFiltering>
        <verbs allowUnlisted="false">
            <add verb="PUT" allowed="true" />
        </verbs>
    </requestFiltering>
</security>

If that does not work we would need to have some more idea what is on the receiving end of this call. What is the code that receives the request?

Community
  • 1
  • 1
Martin Brown
  • 24,692
  • 14
  • 77
  • 122
  • I already had that property set. I made a small tweak to the StaticFile handler and now IIS is returning a 200 status code but the file is still not there. – Jeff Moore Dec 16 '14 at 18:56
  • Just to be clear you are expecting IIS to write the file to disk from a PUT request without any custom code? I don't think the static file handler is capable of writing files, just reading them. – Martin Brown Dec 17 '14 at 21:36
  • Doesn't look like you can do it with WebDAV any more either http://support.microsoft.com/kb/2021641/en-us#. You could possibly do it using WebDAV, but you would have to modify your client to authenticate http://blogs.iis.net/robert_mcmurray/archive/2010/02/09/sending-webdav-requests-in-net.aspx. – Martin Brown Dec 17 '14 at 22:06
  • Is there a different handler that will allow a file to be written? We don't have WebDAV installed. – Jeff Moore Dec 18 '14 at 20:10
  • I don't thinks so. The documentation about what the capabilities of the build in modules are seems rather lacking. The best I could find was the Reference section at the bottom of the following, be prepared to scroll down a lot: http://www.iis.net/learn/get-started/introduction-to-iis/iis-modules-overview – Martin Brown Dec 18 '14 at 20:34
0

IIS 6 use Network Service account for it worker process, IIS 7 use DefaultAppPool identity.

Check that the physical folder has write access for DefaultAppPool identity.

See this article on how to add the DeaultAppPool identity to the folder.

http://www.iis.net/learn/manage/configuring-security/application-pool-identities

Tien Dinh
  • 1,037
  • 7
  • 12