asp.net将xml发布到重定向页面
本文关键字:重定向 net xml asp | 更新日期: 2023-09-27 18:24:52
我从Desktop应用程序向asp.net wep页面发布了一个xml数据。我可以在网页上读取xml,但如果我将页面重定向到另一个页面,我会在Desktop应用程序中出错。我的代码如下。谢谢你的帮助。
桌面应用程序:
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");
}
试试这个代码:
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();
}
}