gpt4 book ai didi

c# - ASP.NET ProtocolViolationException - 无法发送具有此动词类型的内容正文

转载 作者:行者123 更新时间:2023-11-30 17:17:24 25 4
gpt4 key购买 nike

为什么 req.GetRequestStream().Close(); 会导致“ProtocolViolationException - 无法发送具有此动词类型的内容主体。”代码片段来自here .谢谢。

        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();
}

最佳答案

您是否尝试过使用 req.ContentType = application/xml 而不是 text/xml

关于c# - ASP.NET ProtocolViolationException - 无法发送具有此动词类型的内容正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6505843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com