简体   繁体   English

从客户端应用程序调用WCF Restful Service的DELETE方法

[英]Call DELETE method of wcf restful service from client applicaiton

Here is how I try to call the DELETE method from my WCF service: 这是我尝试从WCF服务中调用DELETE方法的方法:

string tmpUrl1 = "http://localhost:1234/MyService.svc/EndPoint/MyMethod";
WebRequest request1 = WebRequest.Create(tmpUrl1);
request1.Method = "DELETE";
byte[] byteArray1 = Encoding.UTF8.GetBytes("{\"idName\":" + newIdName + "}");
request1.ContentType = "application/json";
request1.ContentLength = byteArray1.Length;
Stream dataStream1 = request1.GetRequestStream();
dataStream1.Write(byteArray1, 0, byteArray1.Length);
dataStream1.Close();
WebResponse response1 = request1.GetResponse();

But I get error 400. 但我收到错误400。

Here is the method's name in the wcf: 这是wcf中方法的名称:

[OperationContract]
    [WebInvoke(
        Method = "DELETE",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "/MyMethod/{deleteRP}/",
        BodyStyle = WebMessageBodyStyle.Bare
                )]
    MyClass MyMethod(string deleteRP);

Where am I making a mistake? 我在哪里出错?

Try enabling Tracing on your service and inspect the trace log for the actual error. 尝试在服务上启用跟踪 ,并检查跟踪日志中的实际错误。 Also your url address should have something like 另外您的网址也应该是

"http://localhost:1234/MyService.svc/EndPoint/MyMethod/55" 

rather than 而不是

"http://localhost:1234/MyService.svc/EndPoint/MyMethod" “ http:// localhost:1234 / MyService.svc / EndPoint / MyMethod”

UPDATE: 更新:

private static byte[] ToByteArrayUsingDataContractSer<T>(T requestBody)
        {
            byte[] bytes = null;
            var serializer1 = new DataContractSerializer(typeof(T));
            var ms1 = new MemoryStream();
            serializer1.WriteObject(ms1, requestBody);
            ms1.Position = 0;
            var reader = new StreamReader(ms1);
            bytes = ms1.ToArray();
            return bytes;
        }

Now replace the following line 现在替换以下行

byte[] byteArray1 = Encoding.UTF8.GetBytes("{\"idName\":" + newIdName + "}"); 

with

byte[] array = ToByteArrayUsingDataContractSer<string>("{\"idName\":" + newIdName + "}"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM