Пример #1
0
        /// <summary>
        /// 向服务器发送 HTTP POST 请求
        /// </summary>
        /// <param name="url">完整的网页地址
        ///		<para>必须包含 "http://" 或 "https://"</para>
        ///	</param>
        /// <param name="data">请求所需的上传数据</param>
        /// <param name="contentType">Content-Type HTTP 标头</param>
        /// <param name="referer">参考页链接
        ///		<para>告知服务器, 访问时的来源地址</para>
        /// </param>
        /// <param name="userAgent">User-Agent HTTP 标头</param>
        /// <param name="accept">Accept HTTP 标头</param>
        /// <param name="timeout">超时时间</param>
        /// <param name="cookies">请求附带的 Cookies
        ///		<para>此参数支持自动更新 <see cref="CookieContainer"/>, 若 <see cref="AutoCookieMerge"/> 参数为 True, 将合并新旧 Cookie</para>
        /// </param>
        /// <param name="headers">请求附带的 Headers
        ///		<para>此参数支持自动更新 <see cref="WebHeaderCollection"/></para>
        /// </param>
        /// <param name="proxy">代理 <see cref="HttpWebClient"/> 的 <see cref="WebProxy"/> 实例</param>
        /// <param name="encoding">文本编码</param>
        /// <param name="allowAutoRedirect">跟随重定向响应</param>
        /// <param name="autoCookieMerge">指定自动 <see cref="CookieContainer"/> 合并</param>
        /// <returns>返回从 Internal 读取的 <see cref="byte"/> 数组</returns>
        public static byte[] Post(string url, byte[] data, string contentType, string referer, string userAgent, string accept, int timeout, ref CookieCollection cookies, ref WebHeaderCollection headers, WebProxy proxy, Encoding encoding, bool allowAutoRedirect = true, bool autoCookieMerge = true)
        {
            HttpWebClient httpWebClient = new HttpWebClient();

            httpWebClient.ContentType       = contentType;
            httpWebClient.Referer           = referer;
            httpWebClient.UserAgent         = userAgent;
            httpWebClient.Accept            = accept;
            httpWebClient.TimeOut           = timeout;
            httpWebClient.CookieCollection  = cookies;
            httpWebClient.Headers           = headers;
            httpWebClient.Proxy             = proxy;
            httpWebClient.AutoCookieMerge   = autoCookieMerge;
            httpWebClient.AllowAutoRedirect = allowAutoRedirect;
            byte[] result = httpWebClient.UploadData(new Uri(url), data);
            headers = httpWebClient.ResponseHeaders;
            cookies = httpWebClient.CookieCollection;
            return(result);
        }
Пример #2
0
        private void GetWeiBoData()
        {
            //设置url和contentType
            //string url = "https://s.weibo.com/weibo?q=%E5%88%86%E6%89%8B&page=1";
            string url         = "https://s.weibo.com/weibo?q=%E5%88%86%E6%89%8B&nodup=1";
            string contentType = "text/html; charset=UTF-8";

            //将用户的cookie放入容器
            CookieCollection cookie;

            try
            {
                cookie = SplitCookie(TxtCookie.Text);
            }
            catch
            {
                MessageBox.Show("cookie输入错误,请重新检查!");
                return;
            }


            //post访问
            byte[] webData = HttpWebClient.Post(url, new byte[0], contentType, "", ref cookie);

            //返回数据编码
            string        strData = Encoding.UTF8.GetString(webData);
            List <string> strList = StrongString.BetweenArr(strData, "<p class=\"txt\" node-type=\"feed_list_content\"", "</p>");

            //对数据去重
            Deduplication(strList);

            //放入数据源
            //datas.Clear();
            for (int i = 0; i < strList.Count; i++)
            {
                WeiboData tmp = new WeiboData();
                tmp.Time = DateTime.Now.ToString();
                //格式化内容(防止出现太多的html代码
                tmp.Content = StrongString.GetRight(strList[i], ">");
                tmp.Content = tmp.Content.Replace("<em class=\"s-color-red\">", "");
                tmp.Content = tmp.Content.Replace("</em>", "");
                tmp.Content = tmp.Content.Replace("\n", "");

                //去重检测
                if (datas.Count == 0)
                {
                    datas.Insert(0, tmp);
                }
                else
                {
                    for (int j = 0; j < datas.Count; j++)
                    {
                        if (datas[j].Content == tmp.Content)
                        {
                            break;
                        }
                        else if (j == datas.Count - 1)
                        {
                            datas.Insert(0, tmp);
                            break;
                        }
                    }
                }
            }
        }