I m 从事GET、POST和DELETE业务,来自我的WCF REST服务。 虽然GET & POST运行良好,但DELETE没有发挥作用。
From my client application, when i try to call delete it says below error. Error Message: The remote server returned an error: (403) Forbidden.
<><>My Code:
<>Data合同:
[WebInvoke(Method = "DELETE", UriTemplate = "users/", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
string DeleteUser(UserInfo userinfo);
<>执行>:
public string DeleteUser(UserInfo userinfo)
{
string sResult = "";
try
{
UserDataAccess dataAccess = new UserDataAccess();
sResult = dataAccess.DeleteUser(userinfo.GEId);
}
catch (Exception ex)
{
oLog.LogError(ex, true);
sResult = ex.Message;
}
return sResult;
}
www.un.org/Depts/DGACM/index_spanish.htm 消费法:
protected void btDeleteAsJson_Click(object sender, EventArgs e)
{
UserInfo user = new UserInfo();
user.GEId = Convert.ToInt32(txtGEID.Text);
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
string output = jsSerializer.Serialize(user);
SendMessageDeleteJson(apiUrl, output);
}
public void SendMessageDeleteJson(string endPoint, string output)
{
string data = output;
byte[] bytes = Encoding.UTF8.GetBytes(data);
HttpWebRequest request = CreateWebRequest1(endPoint, bytes.Length, "application/json");
using (var requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}
using (var response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode != HttpStatusCode.OK)
{
string message = String.Format("DELETE failed. Received HTTP {0}", response.StatusCode);
throw new ApplicationException(message);
}
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string sResponseStream = reader.ReadToEnd();
lblDeleteSuccess.Text = "Success! User has beed deleted";
}
}
private HttpWebRequest CreateWebRequest1(string endPoint, Int32 contentLength, string ContentType)
{
var request = (HttpWebRequest)WebRequest.Create(endPoint);
request.Method = "DELETE";
request.ContentLength = contentLength;
//request.ContentType = "application/x-www-form-urlencoded";
request.ContentType = ContentType;
return request;
}
Could you please point me where I m wrong?