当方法为HttpWebRequest.GetRequestStream
时,有没有办法阻止GET
抛出异常?
我知道HEAD
和GET
不应包含有效HTTP中的正文,但有些服务器无论如何都会接受这些方法的正文(例如,Elasticsearch的搜索功能)。我必须调用一些get API方法来从请求体中检索参数。
var uri = new Uri(@"someurl");
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
request.Accept = "*/*";
request.ContentType = "application/json";
var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(JsonConvert.SerializeObject(someObject));
request.ContentLength = bytes.Length;
using (var writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, 0, bytes.Length);
}