1

I am trying to implement a REST WCF service to post a file from a WPF application to my server, however I am getting the 405 error. Before contacting my ISP, can anyone tell me if my code looks ok? I know that most probably its something to do with the server not accepting the Post method, but just wanted to make sure that my code is ok. Here it is :-

    public static void UploadFile()
    {
        string serverPath = "http://www.mywebsites.com/test/";
        string filePath = "C:\\Testing\\asd_asd_Feedback.xml";

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverPath);
        request.Accept = "text/xml";
        request.Method = "PUT";
        request.Credentials = new System.Net.NetworkCredential("testjo", "");

        using (FileStream fileStream = File.OpenRead(filePath))
        using (Stream requestStream = request.GetRequestStream())
        {
            int bufferSize = 1024;
            byte[] buffer = new byte[bufferSize];
            int byteCount = 0;
            while ((byteCount = fileStream.Read(buffer, 0, bufferSize)) > 0)
            {
                requestStream.Write(buffer, 0, byteCount);
            }
        }

        string result = String.Empty;

        try
        {
            using (WebResponse response = request.GetResponse())
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                result = reader.ReadToEnd();
            }
        }
        catch (Exception exc)
        { 
        }

        Console.WriteLine(result);
    }

Basically its failing on the request.GetResponse()

Thanks for your help!

JMon
  • 3,387
  • 16
  • 63
  • 102
  • You're using the `PUT` method, not the `POST` method. Indeed, maybe your ISP does not support it. – Frédéric Hamidi Apr 15 '11 at 07:56
  • It's not at all uncommon for `PUT` to be denied by default. – Andrew Barber Apr 15 '11 at 07:57
  • 1
    So what can I use instead? Or do I have to contact my ISP? – JMon Apr 15 '11 at 08:00
  • @Johann, you can probably use `POST` instead of `PUT`. See http://stackoverflow.com/questions/630453/put-vs-post-in-rest for a discussion about the differences between the two. – Frédéric Hamidi Apr 15 '11 at 08:05
  • Hi Frederic, I already tried to use the POST but got the same error message – JMon Apr 15 '11 at 08:10
  • @Johann, in that case, I would definitely double-check with my ISP. Banning the `POST` method seems weird because forms hosted in your site would also fail, but maybe they have a good reason to do so. – Frédéric Hamidi Apr 15 '11 at 08:20
  • My ISP told me that the POST is allowed, so I really cannot think of anything that is impeding it. I tried to upload a file with FTP (Filezilla) and that worked also. – JMon Apr 15 '11 at 08:54
  • @Johann, do you have a proxy somewhere on the way? That might explain why `POST` requests are rejected even if your ISP allows them. – Frédéric Hamidi Apr 15 '11 at 10:13
  • Hi Frederic, I managed to get the reponse finally! I had to target the actual getresponse.aspx file – JMon Apr 15 '11 at 10:50

0 Answers0