Difference between PUT and POST using WCF REST

I tried implementing REST WCF to learn the difference between a PUT and a POST verb. I used a file with the extension using this service.

The service implementation is as follows:

[OperationContract]
[WebInvoke(UriTemplate = "/UploadFile", Method = "POST")]
void UploadFile(Stream fileContents);

public void UploadFile(Stream fileContents)
{
 byte[] buffer = new byte[32768];
 MemoryStream ms = new MemoryStream();
 int bytesRead, totalBytesRead = 0;
 do
 {
       bytesRead = fileContents.Read(buffer, 0, buffer.Length);
       totalBytesRead += bytesRead;

       ms.Write(buffer, 0, bytesRead);
  } while (bytesRead > 0);

  using (FileStream fs = File.OpenWrite(@"C:\temp\test.txt")) 
  { 
      ms.WriteTo(fs); 
   }

  ms.Close();

}

Client code is as follows:

HttpWebRequest request =     (HttpWebRequest)HttpWebRequest.Create("http://localhost:1922   /EMPRESTService.svc/UploadFile");
        request.Method = "POST";
        request.ContentType = "text/plain";

        byte[] fileToSend = File.ReadAllBytes(@"C:\TEMP\log.txt");  // txtFileName contains the name of the file to upload. 
        request.ContentLength = fileToSend.Length;

        using (Stream requestStream = request.GetRequestStream())
        {
            // Send the file as body request. 
            requestStream.Write(fileToSend, 0, fileToSend.Length);
            //requestStream.Close();
        }

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            Console.WriteLine("HTTP/{0} {1} {2}", response.ProtocolVersion, (int)response.StatusCode, response.StatusDescription);
        Console.ReadLine();

The file is downloaded and the response status code is returned as "200 OK." The satus code is the same if the file exists or does not exist at the download location.

I changed the REST verb to PUT and the status code will be the same as above.

Can anyone explain how I can distinguish between verbs in this context? I could not simulate the generation of a continuous request for client code. If the behavior will be different in this case, can someone help me in changing the client code in ordrr to send a continuous request to a string?

+5
1

POST , ( ), . , .

PUT , . . , , ... .

+2

All Articles