使用HttpClient进行Get请求的时候,发生了这个异常:ProtocolViolationException,无法发生具有此谓词类型的内容正文。
代码如下:
var reqMsg = new HttpRequestMessage
{
Method = httpItem.Method,
Content = httpItem.RequestContent,
RequestUri = new Uri(httpItem.RequestUrl),
};
如果使用的是Get请求,那么就不能设置发送的报文内容:Content,那么这里改造一下就好了:
if (reqMsg.Method != HttpMethod.Get)
{
reqMsg.Content = httpItem.RequestContent; //Do not set a Content - Type in the GET request
reqMsg.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
}