public void Request(string url, string postString)
        {
            CookieContainer cookieContainer = new CookieContainer();
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);//创建req
            req.Accept = "*/*";//接受任意文件
            req.UserAgent = " Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727)"; // 模拟使用IE在浏览
            req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16";
            req.CookieContainer = cookieContainer;
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            if ((postString != null & postString.Length > 0) || url.Contains("?"))
            {
                byte[] postdata = Encoding.UTF8.GetBytes(postString);
                req.BeginGetRequestStream(async1 =>
                {
                    using (Stream stream = req.EndGetRequestStream(async1))
                        stream.Write(postdata, 0, postdata.Length);
                    req.BeginGetResponse(async2 =>
                    {
                        WebResponse rep = req.EndGetResponse(async2);
                        using (Stream stream = rep.GetResponseStream())
                        using (StreamReader sr = new StreamReader(stream))
                        {
                            string content = sr.ReadToEnd();
                        }
                    }, null);
                }, null);
            }
        }

 

经验证,该方法能够成功使用。

posted on 2012-02-13 20:26  Quibbler  阅读(602)  评论(5)    收藏  举报