HttpClient異常:ProtocolViolationException

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






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章