HttpWebRequest PUT(ADO.NET 数据服务框架)

可通过使用 HTTP PUT 请求修改 ADO.NET 数据服务部署的数据。使用此方法可以更改数据实例的大多数属性值,但无法更改任何数据实例的键属性。PUT 请求可应用于实体类型、链接、导航属性和复杂类型。有关更多信息,请参见 PUT 方法(ADO.NET 数据服务框架)

更新单个属性的 PUT 请求

将由 PUT 请求更改的数据将格式化为一个字符串,该字符串将成为 HTTP 请求正文的一部分。首先,PUT 请求通过以下 URI 标识 ID=2 中的 AdventureWorksModel``Address 实体:"https://localhost:50781/AdvWksSalesS.svc/Address(2)". 通过语法 "{AddressLine1:'1600 1st St.'}" 指定对 Address 实体的 AddressLine1 属性的更改。如果要修改的实体是继承层次结构的一部分,则 __metadata 语法元素是必需的:

  "{__metadata:{Uri:'/Address(2)/', " +
      "Type:'AdventureWorksModel.Address'}, " +
      "AddressLine1:'500 5th St.'}"

此说明结尾处的代码块中演示了一个使用 JSON 语法表示 AddressLine1 属性的更新数据的完整示例。

该代码更新 AdventureWorksModelAddress 实体的 AddressLine1 属性。使用要修改的实体的 URI https://localhost:50781/AdvWksSalesS.svc/Address(2)(作为其构造函数的单个参数)创建 HttpWebRequest r。如前面所述,请求的正文将分配给一个名为 requestPayload 的字符串。该方法将设置为 "PUT"ContentType 将分配给 json 协议。r.Accept = "application/json" 行告知服务器发送回使用 json 协议编码的响应(如果有)。

如果利用基于传输的身份验证方案(如 HTTP 基本身份验证)对服务进行保护,则可以通过将凭据分配给请求的方式来传递凭据。对于此示例,使用 DefaultCredentials

请求 r 的格式将设置为 Unicode 文本。UTF8Encoding 变量用于获取请求的长度(以字节为单位),以便将数字数据写入到 Stream 请求对象中。通过对请求调用 GetResponse 来分配 HttpWebResponse 对象。代码 r.GetResponse 将发送数据并获取响应。另一个 Stream 对象 rspStm 用于包含由 GetResponseStream 返回的数据。

    HttpWebRequest r =
WebRequest.Create("https://localhost:50781/AdvWksSalesS.svc/Address(2)")
       as HttpWebRequest;

    // __metadata is only required if inheritance is used,
    // but __metadata syntax is used for completeness. Simple syntax:
    //string requestPayload = "{AddressLine1:'1600 1st St.'}";

    string requestPayload = "{__metadata:{Uri:'/Address(2)/', " +
        "Type:'AdventureWorksModel.Address'}, " +
        "AddressLine1: '1600 1st St.'}";

    r.Method = "PUT";
    UTF8Encoding encoding = new UTF8Encoding();
    r.ContentLength = encoding.GetByteCount(requestPayload);
    r.Credentials = CredentialCache.DefaultCredentials;
    r.Accept = "application/json";
    r.ContentType = "application/json";

    //Write the payload to the request body.
    using (Stream requestStream = r.GetRequestStream())
    {
        requestStream.Write(encoding.GetBytes(requestPayload), 0,
            encoding.GetByteCount(requestPayload));
    }

    try
    {
        HttpWebResponse response = r.GetResponse() as HttpWebResponse;
        string responseBody = "";
        using (Stream rspStm = response.GetResponseStream())
        {
            using (StreamReader reader = new StreamReader(rspStm))
            {
                textBoxResponse.Text = textBoxResponse.Text + 
                    "Response Description: " + 
                        response.StatusDescription;
                textBoxResponse.Text = textBoxResponse.Text + 
                    "Response Status Code: " + response.StatusCode;
                textBoxResponse.Text =
                          textBoxResponse.Text + "\r\n\r\n";
                responseBody = reader.ReadToEnd();
            }
        }
        textBoxResponse.Text = "Success: " + 
                         response.StatusCode.ToString();
    }
    catch (System.Net.WebException ex)
    {
        textBoxResponse.Text = textBoxResponse.Text + 
            "Exception message: " + ex.Message;
        textBoxResponse.Text = textBoxResponse.Text + 
            "\r\nResponse Status Code: " + ex.Status;
        textBoxResponse.Text = textBoxResponse.Text + "\r\n\r\n";

        // get error details sent from the server
        StreamReader reader =
           new StreamReader(ex.Response.GetResponseStream());
        textBoxResponse.Text = textBoxResponse.Text +
           reader.ReadToEnd();
        
    }

另请参见

概念

HttpWebRequest GET(ADO.NET 数据服务框架)
HttpWebRequest POST(ADO.NET 数据服务框架)
HttpWebRequest DELETE(ADO.NET 数据服务框架)
通用 HTTP 要求(ADO.NET 数据服务框架)
.NET 客户端库(ADO.NET 数据服务框架)

其他资源

实体数据模型