简体   繁体   English

通过HTTP发布XML-Webforms

[英]Posting XML over HTTP - Webforms

So I've got this code, which ideally must not change. 因此,我有此代码,理想情况下,该代码不得更改。 On my test site, this code executes fine and posts new data into the database. 在我的测试站点上,此代码可以很好地执行并将新数据发布到数据库中。 But on the live site I get the below error. 但是在实时站点上,我收到以下错误。 Can anyone advise on what this might be? 谁能建议这可能是什么?

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

The error on the live site only is:- 仅实时站点上的错误是:-

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:         //}

But I am sending a POST request, and this works on my test site, just not the live site. 但是我正在发送一个POST请求,它可以在我的测试站点上运行,而不能在实时站点上运行。 I've ensured the code is copied over and all the files exist. 我确保代码已复制并且所有文件都存在。

Why would this code work on one and not the other? 为什么此代码只能在一个而不是另一个上工作?

Cheers, Mike. 干杯,迈克。

For those interested, the issue here was a URL re-write in the web.config, which was set to replace .aspx with a /. 对于有兴趣的人,这里的问题是在web.config中重写URL,该URL设置为用/替换.aspx。 This stoppped the XML data being sent through and resulted in the above erro. 这阻止了正在发送的XML数据,并导致了上述错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM