【问题标题】:Posting XML over HTTP - Webforms通过 HTTP 发布 XML - 网络表单
【发布时间】:2015-01-13 16:31:32
【问题描述】:

所以我得到了这段代码,最好不要改变。在我的测试站点上,此代码执行良好并将新数据发布到数据库中。但是在实时站点上,我收到以下错误。谁能建议这可能是什么?

protected void Button1_Click(object sender, EventArgs e)
{
    WebRequest req = null;
    WebResponse rsp = null;
    //  try
    //  {
    string fileName = Server.MapPath("ExampleXML.xml");
    string uri = "http://XXX/api/index.aspx";

    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)
    //  {

    //  }
    //  catch (Exception ex)
    //  {

    // }
    // finally
    //{
    if (req != null) req.GetRequestStream().Close();
    if (rsp != null) rsp.GetResponseStream().Close();
    //}
}

仅现场站点上的错误是:-

Server Error in '/' Application.

Cannot send a content-body with this verb-type.

Line 47:         // finally
Line 48:         //{
Line 49:         if (req != null) req.GetRequestStream().Close();
Line 50:         if (rsp != null) rsp.GetResponseStream().Close();
Line 51:         //}

但我正在发送一个 POST 请求,这适用于我的测试站点,而不是实时站点。我已确保复制代码并且所有文件都存在。

为什么这段代码只适用于一个而不适用于另一个?

干杯, 迈克。

【问题讨论】:

标签: c# asp.net .net xml http


【解决方案1】:

对于那些感兴趣的人,这里的问题是在 web.config 中重写 URL,它被设置为用 / 替换 .aspx。这阻止了正在发送的 XML 数据并导致上述错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 2014-05-02
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 2021-07-12
    • 2017-12-28
    相关资源
    最近更新 更多