Comment faire une requête HTTP PUT?

Quelle est la meilleure façon de composer une requête PUT de repos en C #?

La demande doit également envoyer un object non présent dans l’URI.

using(var client = new System.Net.WebClient()) { client.UploadData(address,"PUT",data); } 

Mon approche finale:

  public void PutObject(ssortingng postUrl, object payload) { var request = (HttpWebRequest)WebRequest.Create(postUrl); request.Method = "PUT"; request.ContentType = "application/xml"; if (payload !=null) { request.ContentLength = Size(payload); Stream dataStream = request.GetRequestStream(); Serialize(dataStream,payload); dataStream.Close(); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); ssortingng returnSsortingng = response.StatusCode.ToSsortingng(); } public void Serialize(Stream output, object input) { var ser = new DataContractSerializer(input.GetType()); ser.WriteObject(output, input); } 
 protected void UpdateButton_Click(object sender, EventArgs e) { var values = ssortingng.Format("Name={0}&Family={1}&Id={2}", NameToUpdateTextBox.Text, FamilyToUpdateTextBox.Text, IdToUpdateTextBox.Text); var bytes = Encoding.ASCII.GetBytes(values); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ssortingng.Format("http://localhost:51436/api/employees")); request.Method = "PUT"; request.ContentType = "application/x-www-form-urlencoded"; using (var requestStream = request.GetRequestStream()) { requestStream.Write(bytes, 0, bytes.Length); } var response = (HttpWebResponse) request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) UpdateResponseLabel.Text = "Update completed"; else UpdateResponseLabel.Text = "Error in update"; } 

Comment utiliser la méthode PUT en utilisant WebRequest.

  //JsonResultModel class public class JsonResultModel { public ssortingng ErrorMessage { get; set; } public bool IsSuccess { get; set; } public ssortingng Results { get; set; } } // HTTP_PUT Function public static JsonResultModel HTTP_PUT(ssortingng Url, ssortingng Data) { JsonResultModel model = new JsonResultModel(); ssortingng Out = Ssortingng.Empty; ssortingng Error = Ssortingng.Empty; System.Net.WebRequest req = System.Net.WebRequest.Create(Url); try { req.Method = "PUT"; req.Timeout = 100000; req.ContentType = "application/json"; byte[] sentData = Encoding.UTF8.GetBytes(Data); req.ContentLength = sentData.Length; using (System.IO.Stream sendStream = req.GetRequestStream()) { sendStream.Write(sentData, 0, sentData.Length); sendStream.Close(); } System.Net.WebResponse res = req.GetResponse(); System.IO.Stream ReceiveStream = res.GetResponseStream(); using (System.IO.StreamReader sr = new System.IO.StreamReader(ReceiveStream, Encoding.UTF8)) { Char[] read = new Char[256]; int count = sr.Read(read, 0, 256); while (count > 0) { Ssortingng str = new Ssortingng(read, 0, count); Out += str; count = sr.Read(read, 0, 256); } } } catch (ArgumentException ex) { Error = ssortingng.Format("HTTP_ERROR :: The second HttpWebRequest object has raised an Argument Exception as 'Connection' Property is set to 'Close' :: {0}", ex.Message); } catch (WebException ex) { Error = ssortingng.Format("HTTP_ERROR :: WebException raised! :: {0}", ex.Message); } catch (Exception ex) { Error = ssortingng.Format("HTTP_ERROR :: Exception raised! :: {0}", ex.Message); } model.Results = Out; model.ErrorMessage = Error; if (!ssortingng.IsNullOrWhiteSpace(Out)) { model.IsSuccess = true; } return model; }