日期:2014-05-17  浏览次数:20568 次

js中XMLHttpRequest请求.ashx页面,在C#中用哪个
在C#中用哪个可以实现与js中XMLHttpRequest对象一样以get方式请求页面

------解决方案--------------------
C# code

  StringBuilder builder = new StringBuilder(); 
        builder.Append("http://xxx.com.cn/misc.ashx"); 
        builder.Append("?"); 
        builder.Append("action=gold"); 
        builder.Append("&"); 
        builder.Append("token="+strToken+""); 
        HttpWebResponse rsp = null; 
        HttpWebRequest req = null; 
        req = (HttpWebRequest)WebRequest.Create(new Uri(builder.ToString())); 
        req.ContentType = "application/x-www-form-urlencoded"; 
        req.Accept = "*/*"; 
        req.Timeout = 30000;//30秒连接不成功就中断 
        req.Method = "GET"; 
        rsp = (HttpWebResponse)req.GetResponse(); 
        Stream rspStream = rsp.GetResponseStream(); 

        StreamReader sr = new StreamReader(rspStream, Encoding.GetEncoding("utf-8")); 
        result = sr.ReadToEnd(); 
        sr.Close(); 


      
        XmlDocument xmlDoc = new XmlDocument(); 

        xmlDoc.LoadXml(result);