asp.net将xml发布到重定向页面

时间:2014-09-29 09:07:15

标签: c# asp.net xml redirect post

我将桌面应用程序中的xml数据发布到asp.net wep页面。我可以在网页上阅读xml,但如果我将页面重定向到另一个页面,我会在桌面应用程序中收到错误。我的代码如下。 谢谢你的帮助。

桌面应用:

    static void Main(string[] args)
    {
        post();

        Console.WriteLine("Post process is successed...");
        Console.Read();
    }

    private static void post() {
        System.Net.WebRequest req = null;
        System.Net.WebResponse rsp = null;
        try {
            req = System.Net.WebRequest.Create("http://localhost:5318/Account/XMLReader.aspx");
            req.Method = "POST";
            req.ContentType = "text/xml";

            StreamWriter writer = new System.IO.StreamWriter(req.GetRequestStream());
            writer.WriteLine(getXmlData());
            writer.Flush();
            writer.Close();

            rsp = req.GetResponse();
        } catch {
            throw;
        } finally {
            if (req != null) req.GetRequestStream().Close(); // *****Error occures here****
            if (rsp != null) rsp.GetResponseStream().Close();
        }
    }

asp.net页面代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Response.ContentType = "text/xml";
        System.IO.StreamReader reader =
        new System.IO.StreamReader(Page.Request.InputStream);
        String xmlData = reader.ReadToEnd();

        Response.Redirect("/Account/Login.aspx");
    }

1 个答案:

答案 0 :(得分:0)

试试这段代码:

private static void post()
        {
            System.Net.WebRequest req = null;
            System.Net.WebResponse rsp = null;
            Stream newStream = null;
            try
            {
                req = System.Net.WebRequest.Create("http://localhost:39384/Default.aspx");
                req.Method = "POST";
                req.ContentType = "text/xml";

                newStream = req.GetRequestStream();
                StreamWriter writer = new System.IO.StreamWriter(newStream);
                writer.WriteLine("ddd");
                writer.Flush();
                writer.Close();

                rsp = req.GetResponse();
            }
            catch(Exception e)
            {
                throw;
            }
            finally
            {
                if (req != null) newStream.Close(); // *****Error occures here****
                if (rsp != null) rsp.GetResponseStream().Close();
            }
        }