[英]WCF restful service: How to send long string parameters in POST requests?
[英]How to send full entity to WCF Restful service (Post) to do an update?
使用RestfulServiceMgr類,我能夠GetProducts和GetProductById。 但是當我需要將整個實體發送到WCF靜態服務進行更新時,不確定如何對更新產品執行此操作。
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 Calls 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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.