简体   繁体   English

使用WCF REST在PUT和POST之间的区别

[英]Difference between PUT and POST using WCF REST

I have tried to implement a REST WCF in order to explore difference between PUT and POST verb. 我试图实现REST WCF以探索PUT和POST动词之间的区别。 I have uploded a file in a location using the service. 我已使用该服务在某个位置上传了一个文件。

The service implementation is as folowing: 服务实现如下:

[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 following: 客户端代码如下:

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 being uploaded and the response status code is being returned as "200 OK". 正在上载文件,响应状态代码将返回“200 OK”。 The satus code is same in case of existance or non-existance of the file in the upload location. 在上传位置存在或不存在文件的情况下,satus代码是相同的。

I have changed the REST verb to PUT and the status code is same as above. 我已将REST动词更改为PUT,状态代码与上面相同。

Could anybody explain, how I can identify the differences between the verbs in this context? 任何人都可以解释一下,在这种情况下我如何识别动词之间的差异? I couldn't able to simulate generating continious request fron client code. 我无法模拟从客户端代码生成连续请求。 If the behaviour will differ on doing so, could anybody help me in modifying the client code in ordrr to send continious request in a row ? 如果这样做的行为不同,是否有人可以帮我修改ordrr中的客户端代码以连续发送连续请求?

POST verb is used when are you creating a new resource (a file in your case) and repeated operations would create multiple resources on the server. 在创建新资源(在您的情况下为文件)时使用POST谓词,并且重复操作将在服务器上创建多个资源。 This verb would make sense if uploading a file with the same name multiple times creates multiple files on the server. 如果多次上传具有相同名称的文件在服务器上创建多个文件,则此动词将有意义。

PUT verb is used when you are updating an existing resource or creating a new resource with a predefined id. 当您更新现有资源或创建具有预定义ID的新资源时,将使用PUT谓词。 Multiple operations would recreate or update the same resource on the server. 多个操作将重新创建或更新服务器上的相同资源。 This verb would make sense if uploading a file with the same name for the second, third... time would overwrite the previously uploaded file. 如果为第二个,第三个...时间上传一个具有相同名称的文件将覆盖以前上传的文件,则此动词将有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM