[英]ASP.NET ProtocolViolationException - Cannot send a content-body with this verb-type
Why will req.GetRequestStream().Close();为什么会req.GetRequestStream().Close(); cause " ProtocolViolationException - Cannot send a content-body with this verb-type."导致“ ProtocolViolationException - 无法发送具有此动词类型的内容主体。” The code snippet is from here .代码片段来自这里。 Thanks.谢谢。
WebRequest req = null;
WebResponse rsp = null;
try
{
string fileName = "Login.xml";
string uri = "http://localhost/api/login";
req = WebRequest.Create(uri);
//req.Proxy = WebProxy.GetDefaultProxy(); // Enable if using proxy
req.Method = "POST"; // Post method
req.ContentType = "text/xml"; // content type
// Wrap the request stream with a text-based writer
StreamWriter writer = new StreamWriter(req.GetRequestStream());
// Write the XML text into the stream
writer.WriteLine(this.GetTextFromXMLFile(fileName));
writer.Close();
// Send the data to the webserver
rsp = req.GetResponse();
}
catch (WebException webEx)
{
LOG.Error(webEx.StackTrace.ToString());
}
catch (Exception ex)
{
LOG.Error(ex.StackTrace.ToString());
}
finally
{
if (req != null) req.GetRequestStream().Close();
if (rsp != null) rsp.GetResponseStream().Close();
}
Have you tried using req.ContentType = application/xml
instead of text/xml
?您是否尝试过使用req.ContentType = application/xml
而不是text/xml
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.