调用泛型处理程序asp c#

时间:2015-01-11 12:21:47

标签: c# asp.net handler

我有通用处理程序,它在字符串中返回XML。我该怎么称呼他?

int userid = 1;

string xmlString = string.Format("~/XMLHandler.ashx?userId={0}", userid); // here I need returned string from handler

System.IO.StreamWriter file = new System.IO.StreamWriter("e:\\vypujcky.xml");
file.WriteLine(xmlString);

file.Close();

2 个答案:

答案 0 :(得分:0)

像这样

int userid = 1;

string xmlString = string.Format("~/XMLHandler.ashx?userId={0}", userid);

WebRequest req = WebRequest.Create(Server.MapPath("~\")+xmlString);
req.Proxy = null;
req.Method = "POST";
string responseFromServer="";
 try
{
WebResponse response = req.GetResponse();
Stream dataStream = response.GetResponseStream();
var statusCode = ((HttpWebResponse)response).StatusCode;

StreamReader reader = new StreamReader(dataStream);
responseFromServer = reader.ReadToEnd();

using(System.IO.StreamWriter file = new System.IO.StreamWriter("e:\\vypujcky.xml"))
{
file.WriteLine(responseFromServer);
}

}
catch (WebException ex)
{


}

答案 1 :(得分:0)

您可以使用System.Net.WebClient.DownloadString()下载资源:

int userid = 1;

Uri resourceUri = new Uri(new Uri(Request.Url.Host), string.Format("XMLHandler.ashx?userId={0}", userid));
System.Net.WebClient webClient = new System.Net.WebClient();
string xmlString = webClient.DownloadString(resourceUri);

// rest of the code is the same