c# - 如何将完整实体发送到 WCF Restful 服务(发布)进行更新?
问题描述
使用 RestfulServiceMgr 类,我能够 GetProducts 和 GetProductById。但不确定如何更新产品,当我需要将整个实体发送到 WCF restful 服务进行更新时。
WCF 宁静服务:
[OperationContract]
[WebInvoke(Method = "POST")]
void UpdateProduct(Entity_Product eProduct);
[OperationContract]
[WebInvoke(Method = "POST")]
void UpdateProductDetails(Entity_ProductDetails eProductDetails);
使用 RestfulServiceMgr 类调用 Resful 服务:
public void UpdateProduct(Entity_Product ProductData, Entity_ProductDetails ProductDetailsData)
{
client.EndPoint = @Constants.Url_UpdateProduct;
client.Method = HttpVerb.POST;
client.PostData = "{postData: value}";
client.MakeRequest();
client.EndPoint = @Constants.Url_UpdateProductDetails;
client.Method = HttpVerb.POST;
client.PostData = "{postData: value}";
client.MakeRequest();
}
RestfulServiceMgr Restful 调用 Helper 类:
public class RestfulServiceMgr
{
public string EndPoint { get; set; }
public HttpVerb Method { get; set; }
public string ContentType { get; set; }
public string PostData { get; set; }
public RestfulServiceMgr()
{
EndPoint = "";
Method = HttpVerb.GET;
ContentType = "application/json";
PostData = "";
}
public RestfulServiceMgr(string endpoint)
{
EndPoint = endpoint;
Method = HttpVerb.GET;
ContentType = "application/json";
PostData = "";
}
public RestfulServiceMgr(string endpoint, HttpVerb method)
{
EndPoint = endpoint;
Method = method;
ContentType = "application/json";
PostData = "";
}
public RestfulServiceMgr(string endpoint, HttpVerb method, string postData)
{
EndPoint = endpoint;
Method = method;
ContentType = "application/json";
PostData = postData;
}
public string MakeRequest()
{
return MakeRequest("");
}
public string MakeRequest(string parameters)
{
var request = (HttpWebRequest)WebRequest.Create(EndPoint + parameters);
request.Method = Method.ToString();
request.ContentLength = 0;
request.ContentType = ContentType;
if (!string.IsNullOrEmpty(PostData) && Method == HttpVerb.POST)
{
var encoding = new UTF8Encoding();
var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(PostData);
request.ContentLength = bytes.Length;
using (var writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, 0, bytes.Length);
}
}
using (var response = (HttpWebResponse)request.GetResponse())
{
var responseValue = string.Empty;
if (response.StatusCode != HttpStatusCode.OK)
{
var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
throw new ApplicationException(message);
}
// grab the response
using (var responseStream = response.GetResponseStream())
{
if (responseStream != null)
using (var reader = new StreamReader(responseStream))
{
responseValue = reader.ReadToEnd();
}
}
return responseValue;
}
}
}
解决方案
工作!
client.EndPoint = @Constants.Url_UpdateProduct;
client.Method = HttpVerb.POST;
client.PostData = new JavaScriptSerializer().Serialize(ProductData);
client.MakeRequest();
推荐阅读
- javascript - Javascript forEach - TypeError:无法设置未定义的属性
- html - Textarea cols 对于表情符号不正确
- matlab - 如何从文本文件中保存图像的 url?
- android - AutoCompleteTextView : AfterTextChange 和 OnItemClick 一起工作
- angular - How to Select if div has class in component side on Angular
- uml - 序列图:有什么区别?
- c++ - 标头中的类不包括所有变量和函数?
- javascript - 为什么 Bootstrap 会破坏我的正常 CSS 布局,我该如何解决?
- reactjs - 如何从此 API 对象的响应中显示 quoteText
- html - 如何在模态打开时防止正文滚动