使用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);
}