我在 .Net Micro Framework 中使用 HttpWebRequest 方法。我正在尝试使用以下方法将数据发布到另一台服务器。我得到以下信息:
例外:
“System.Net.ProtocolViolationException:HTTP 方法不正确:GET”错误。
堆栈跟踪:
System.Net.HttpWebRequest::ValidateGetRequestStream System.Net.HttpWebRequest::GetRequestStream
这个异常是否告诉我应该在 POST 时进行 GET ?如果是这样,我有 request.Method = "POST" 那么是什么导致它使用 GET ?
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(@"http://192.168.2.1:3322/home/PostEvent");
Stream dataStream = webReq.GetRequestStream();
UTF8Encoding enc = new UTF8Encoding();
byte[] data = UTF8Encoding.UTF8.GetBytes(strMachineEvt.ToString());
dataStream.Write(data, 0, data.Length);
dataStream.Close();
webReq.Method = "POST";
webReq.ContentType = "application/json";
webReq.ContentLength = data.Length;
WebResponse response = webReq.GetResponse();
//HttpWebResponse resp = (HttpWebResponse)webReq.GetResponse();
Debug.Print(((HttpWebResponse)response).StatusDescription);
Stream respData = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
// Display the content.
Debug.Print(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();