首页 新闻 赞助 找找看

post数据问题---急啊急啊,再此谢谢各位GGMM

0
悬赏园豆:80 [已关闭问题] 关闭于 2009-08-27 21:35

我把代码写在web页面里,用个按钮提交可以获取到目标页面的源代码,但是我把代码写在winform里,用按钮提交,为什么获取不到目标页面的源代码呢?高手们,帮帮小弟吧!谢谢呀。。。急啊。

代码:

  /// <summary>
        /// 提供通过POST方法获取页面的方法
        /// </summary>
        /// <param name="urlString">请求的URL</param>
        /// <param name="encoding">页面使用的编码</param>
        /// <param name="postDataString">POST数据</param>
        /// <returns>获取的页面</returns>
        public static string GetHtmlFromPost(string urlString, Encoding encoding, string postDataString)
        {
            //定义局部变量
            System.Net.ServicePointManager.Expect100Continue = false;
            CookieContainer cookieContainer = new CookieContainer();
            HttpWebRequest httpWebRequest = null;
            HttpWebResponse httpWebResponse = null;
            Stream inputStream = null;
            Stream outputStream = null;
            StreamReader streamReader = null;
            string htmlString = string.Empty;

            //转换POST数据
            byte[] postDataByte = encoding.GetBytes(postDataString);
            //建立页面请求
            try
            {
                httpWebRequest = WebRequest.Create(urlString) as HttpWebRequest;
            }
            //处理异常
            catch (Exception ex)
            {
                throw new Exception("建立页面请求时发生错误!", ex);
            }
            //指定请求处理方式
            httpWebRequest.Method = "POST";
            httpWebRequest.KeepAlive = false;
            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            httpWebRequest.CookieContainer = cookieContainer;
            httpWebRequest.ContentLength = postDataByte.Length;
            //向服务器传送数据
            try
            {
                inputStream = httpWebRequest.GetRequestStream();
                inputStream.Write(postDataByte, 0, postDataByte.Length);
            }
            //处理异常
            catch (Exception ex)
            {
                throw new Exception("发送POST数据时发生错误!", ex);
            }
            finally
            {
                inputStream.Close();
            }
            //接受服务器返回信息
            try
            {
                httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
                outputStream = httpWebResponse.GetResponseStream();
                streamReader = new StreamReader(outputStream, encoding);
                htmlString = streamReader.ReadToEnd();
            }
            //处理异常
            catch (Exception ex)
            {
                throw new Exception("接受服务器返回页面时发生错误!", ex);
            }
            finally
            {
                streamReader.Close();
            }
            foreach (Cookie cookie in httpWebResponse.Cookies)
            {
                cookieContainer.Add(cookie);
            }
            return htmlString;
        }

问题补充: 返回异常:远程服务器返回错误: (417) Expectation failed。 一般是什么原因啊?改怎么修改啊
xiaofei_leo的主页 xiaofei_leo | 初学一级 | 园豆:0
提问于:2009-08-27 15:53
< >
分享
所有回答(2)
0

抓包看你发的HTTP请求到底是什么

eaglet | 园豆:17139 (专家六级) | 2009-08-27 16:02
0

.........................好累..代码看不下去!! 自己好好debug调试吧,像楼上的说的..抓包看看!!!!!然后可以搜索一下 什么开心网外挂的资料看看...我都用控制台程序写过 偷菜程序,自己纯属无聊.....

糊涂而已 | 园豆:135 (初学一级) | 2009-08-27 21:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册