0

这是我用于删除实体的代码,

private void Button_Click_1(object sender, RoutedEventArgs e)
{
var request = HttpWebRequest.Create(uri) as HttpWebRequest;
request.Accept = "application/json;odata=verbose";
request.Method = "DELETE";
request.BeginGetResponse(new AsyncCallback(GotResponse), request);
}

private void GotResponse(IAsyncResult asynchronousResult)
{
    string data;
    HttpWebRequest myrequest = (HttpWebRequest)asynchronousResult.AsyncState;
    using (HttpWebResponse response = (HttpWebResponse)myrequest.EndGetResponse(asynchronousResult))
        {
            System.IO.Stream responseStream = response.GetResponseStream();
            using (var reader = new System.IO.StreamReader(responseStream))
            {
                data = reader.ReadToEnd();
            }
            responseStream.Close();
        }
        this.Dispatcher.BeginInvoke(() =>
        {
            MessageBox.Show(data);
        });
}

使用上述方法时,应用程序在执行该行时崩溃

using (HttpWebResponse response = (HttpWebResponse)myrequest.EndGetResponse(asynchronousResult))

发送崩溃消息,如果进行了适当的异常处理,则可以安全运行。但是当我删除线时

request.Accept = "application/json;odata=verbose";

一切正常。

谁能解释我为什么会这样?

附加信息:我的服务器对成功删除的响应是,

{
 "code":0,
 "message":"Successfully Deleted"
}
4

1 回答 1

3

详细JSON被认为是一种遗留格式,只需尝试更改:

request.Accept = "application/json;odata=verbose";

request.Accept = "application/json";
request.ContentType = "application/json";
于 2013-10-18T06:42:46.070 回答