如何使用 HttpWebRequest.Accept
本文关键字:Accept HttpWebRequest 何使用 | 更新日期: 2023-09-27 18:36:04
这是我用于删除实体的代码,
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"
}
详细JSON
被认为是一种传统格式,只需尝试更改:
request.Accept = "application/json;odata=verbose";
自
request.Accept = "application/json";
request.ContentType = "application/json";