调用通用处理程序 asp c#

Calling generic handler asp c#

我有通用处理程序,return 我 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();

像这样

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


}

您可以使用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