使用WCF REST PUT和POST之间的区别 [英] Difference between PUT and POST using WCF REST

查看:179
本文介绍了使用WCF REST PUT和POST之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试以探索PUT和POST动词之间的差异来实现REST WCF。我已经uploded在使用该服务的位置的文件

I have tried to implement a REST WCF in order to explore difference between PUT and POST verb. 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();



}

}

客户端代码如下

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();



该文件正在上传和响应状态代码被返回200 OK。该萨特斯代码在所有脑干或上载位置文件的非脑干的情况相同。

The file is being uploaded and the response status code is being returned as "200 OK". The satus code is same in case of existance or non-existance of the file in the upload location.

我已经改变了REST动词PUT和状态代码同上。

I have changed the REST verb to PUT and the status code is same as above.

可能有人解释,我怎么能确定在这一背景下,动词之间的区别是什么?我不能能够模拟生成弗朗客户端代码continious请求。如果该行为将在这样的不同,可能有人帮助我在修改中ordrr客户端代码发送连续continious请求?

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 ?

推荐答案

当你创建一个新的资源(在你的情况下的一个文件)使用POST谓词和反复操作将在服务器上创建多个资源。这个动词将使意义,如果多次上传文件具有相同的名称在服务器上创建多个文件。

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. This verb would make sense if uploading a file with the same name multiple times creates multiple files on the server.

PUT动词使用时,要更新现有资源或创建具有预定ID的新资源。多个操作将重新创建或更新服务器上相同的资源。如果上传与第二,第三......时间将覆盖先前上传的文件同名的文件,这个动词是有意义的。

PUT verb is used when you are updating an existing resource or creating a new resource with a predefined id. 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.

这篇关于使用WCF REST PUT和POST之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆