读取网络文件并获取字符窜(如获取网络Xml文件)

public string RunRegisterNoParam(string strRegisterUrl, string param)
    {
        string strRen = "";
        try
        {
            CookieContainer container = new CookieContainer();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strRegisterUrl + "?" + param);
            request.CookieContainer = container;
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "POST";
            Stream myRequestStream = request.GetRequestStream();
            StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("utf-8"));
            myStreamWriter.Write(param);
            //把数据写入HttpWebRequest的Request流
            myStreamWriter.Close();
            myRequestStream.Close();
            //关闭打开对象
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)request.GetResponse();
            //新建一个HttpWebResponse
            myHttpWebResponse.Cookies = container.GetCookies(request.RequestUri);
            //获取一个包含url的Cookie集合的CookieCollection
            Stream myResponseStream = myHttpWebResponse.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            strRen = myStreamReader.ReadToEnd();
            //把数据从HttpWebResponse的Response流中读出
            myStreamReader.Close();
            myResponseStream.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return strRen;
    }

posted @ 2011-09-28 15:42  kevinzw  阅读(449)  评论(0编辑  收藏  举报