Пример #1
0
 /// <summary>
 /// 获取在线用户实例
 /// </summary>
 /// <returns>网页实例</returns>
 public static string GetOnline()
 {
     var http = new HttpHelper();
     var item = new HttpItem
         {
             URL = "http://home.51cto.com/index.php?s=/Friend/online/p/",
             Cookie = User.Cookie,
         };
     item.Header.Add("X-Requested-With", "XMLHttpRequest");
     HttpResult result = http.GetHtml(item);
     string html = result.Html;
     return html;
 }
Пример #2
0
 public static string GetPage(string url)
 {
     var http = new HttpHelper();
     var item = new HttpItem
     {
         URL = url,
         Cookie = User.Cookie,
     };
     item.Header.Add("X-Requested-With", "XMLHttpRequest");
     HttpResult result = http.GetHtml(item);
     string html = result.Html;
     return html;
 }
Пример #3
0
 /// <summary>
 /// 获取答案
 /// </summary>
 /// <param name="questionId">题目id</param>
 /// <returns></returns>
 public static string GetAnswer(string questionId)
 {
     var http = new HttpHelper();
     var itemGet = new HttpItem
     {
         URL = "http://selftest.51cto.com/self_showres.php?sid=" + questionId, //URL     必需项
         Method = "get", //URL     可选项 默认为Get
         IsToLower = false, //得到的HTML代码是否转成小写     可选项默认转小写
         Referer = "http://selftest.51cto.com/subread.php?sid=" + questionId,
         Cookie = User.Cookie, //字符串Cookie     可选项
         Timeout = 100000, //连接超时时间     可选项默认为100000
         ReadWriteTimeout = 30000, //写入Post数据超时时间     可选项默认为30000
         UserAgent = "Mozilla/5.0 (Windows NT 5.2; rv:21.0) Gecko/20100101 Firefox/21.0",
         //用户的浏览器类型,版本,操作系统     可选项有默认值
         ContentType = "application/x-www-form-urlencoded; charset=UTF-8", //返回类型    可选项有默认值
         Allowautoredirect = true, //是否根据301跳转     可选项
     };
     return http.GetHtml(itemGet).Html;
 }
Пример #4
0
 public static string Fav(string tid)
 {
     var http = new HttpHelper();
     var itemPost = new HttpItem
     {
         URL = "http://rozbo.blog.51cto.com/mod/favour.php", //URL     必需项
         Method = "POST", //URL     可选项 默认为Get
         IsToLower = false,
         Postdata = "tid=" + tid,
         Cookie = User.Cookie, //字符串Cookie     可选项
         Timeout = 100000, //连接超时时间     可选项默认为100000
         ReadWriteTimeout = 30000, //写入Post数据超时时间     可选项默认为30000
         UserAgent = "Mozilla/5.0 (Windows NT 5.2; rv:21.0) Gecko/20100101 Firefox/21.0",
         //用户的浏览器类型,版本,操作系统     可选项有默认值
         ContentType = "application/x-www-form-urlencoded; charset=UTF-8", //返回类型    可选项有默认值
         Allowautoredirect = true, //是否根据301跳转     可选项
     };
     return http.GetHtml(itemPost).Html;
 }
Пример #5
0
        public static string SendGift(string userId, string reply)
        {
            var http = new HttpHelper();
            var item = new HttpItem
                {
                    URL = "http://home.51cto.com/apps/gift/index.php?s=/Index/send/", //URL     必需项
                    Method = "post", //URL     可选项 默认为Get
                    IsToLower = false, //得到的HTML代码是否转成小写     可选项默认转小写
                    Cookie = User.Cookie, //字符串Cookie     可选项
                    Referer = "http://weiba.weibo.com/10070/t/zstIRyLfE", //来源URL     可选项
                    Postdata =
                        "fri_ids=" + userId + "&sendInfo=" + HttpUtility.UrlEncode(reply, Encoding.UTF8) +
                        "&sendWay=1&giftId=202", //Post数据     可选项GET时不需要写
                    Timeout = 100000, //连接超时时间     可选项默认为100000
                    ReadWriteTimeout = 30000, //写入Post数据超时时间     可选项默认为30000
                    UserAgent = "Mozilla/5.0 (Windows NT 5.2; rv:21.0) Gecko/20100101 Firefox/21.0",
                    //用户的浏览器类型,版本,操作系统     可选项有默认值
                    ContentType = "application/x-www-form-urlencoded; charset=UTF-8", //返回类型    可选项有默认值
                    Allowautoredirect = true, //是否根据301跳转     可选项
                    //CerPath = "d:\123.cer",//证书绝对路径     可选项不需要证书时可以不写这个参数
                    //Connectionlimit = 1024,//最大连接数     可选项 默认为1024                    ProxyIp = "",//代理服务器ID     可选项 不需要代理 时可以不设置这三个参数
                    //ProxyPwd = "123456",//代理服务器密码     可选项
                    //ProxyUserName = "******",//代理服务器账户名     可选项

                };
            item.Header.Add("X-Requested-With", "XMLHttpRequest");
            HttpResult result = http.GetHtml(item);
            var html = result.Html;
            return html;
        }
Пример #6
0
        public static string PostReply(string postsId, string reply, string username = "******")
        {
            var http = new HttpHelper();
            var postType = username == "匿名" ? "1" : "0";
            var item = new HttpItem
            {
                URL = "http://rozbo.blog.51cto.com/comment3.php",
                Method = "post",
                IsToLower = false,
                Cookie = User.Cookie,
                //Referer = "http://weiba.weibo.com/10070/t/zstIRyLfE",
                Postdata =
                    "authnum=&tid=" + postsId + "&username="******"&content=" + HttpUtility.UrlEncode(reply, Encoding.UTF8) + "&niming=" + posttype +
                    "&favour=1&parentid=",
                Timeout = 100000,
                ReadWriteTimeout = 30000,
                UserAgent = "Mozilla/5.0 (Windows NT 5.2; rv:21.0) Gecko/20100101 Firefox/21.0",
                ContentType = "application/x-www-form-urlencoded; charset=UTF-8",
                Allowautoredirect = true,

            };
            item.Header.Add("X-Requested-With", "XMLHttpRequest");
            HttpResult result = http.GetHtml(item);
            var html = result.Html;
            return html;
        }
Пример #7
0
 public static string PostPage(string url,string data)
 {
     var http = new HttpHelper();
     var item = new HttpItem
     {
         URL = url, //URL     必需项
         Method = "post", //URL     可选项 默认为Get
         IsToLower = false, //得到的HTML代码是否转成小写     可选项默认转小写
         Cookie = User.Cookie, //字符串Cookie     可选项
         Postdata =  data,
         Timeout = 100000, //连接超时时间     可选项默认为100000
         ReadWriteTimeout = 30000, //写入Post数据超时时间     可选项默认为30000
         UserAgent = "Mozilla/5.0 (Windows NT 5.2; rv:21.0) Gecko/20100101 Firefox/21.0",
         //用户的浏览器类型,版本,操作系统     可选项有默认值
         ContentType = "application/x-www-form-urlencoded; charset=UTF-8", //返回类型    可选项有默认值
         Allowautoredirect = true, //是否根据301跳转     可选项
     };
     HttpResult result = http.GetHtml(item);
     return  result.Html;
 }
Пример #8
0
 /// <summary>
 /// 登录51cto
 /// </summary>
 /// <param name="tid"></param>
 /// <returns></returns>
 public static string Login(string email,string password,string autologin= "******")
 {
     var http = new HttpHelper();
     var itemPost = new HttpItem
     {
         URL = "http://home.51cto.com/index.php?s=%2FIndex%2FdoLogin", //URL     必需项
         Method = "POST", //URL     可选项 默认为Get
         IsToLower = false,
         Postdata = "email=" + HttpUtility.UrlEncode(email, Encoding.UTF8) + "&passwd=" + password + "&autologin="******"&reback=http%253A%252F%252Fbbs.51cto.com&button.x=31&button.y=15",
         Timeout = 100000, //连接超时时间     可选项默认为100000
         ReadWriteTimeout = 30000, //写入Post数据超时时间     可选项默认为30000
         UserAgent = "Mozilla/5.0 (Windows NT 5.2; rv:21.0) Gecko/20100101 Firefox/21.0",
         //用户的浏览器类型,版本,操作系统     可选项有默认值
         ContentType = "application/x-www-form-urlencoded; charset=UTF-8", //返回类型    可选项有默认值
         Allowautoredirect = true, //是否根据301跳转     可选项
     };
     return http.GetHtml(itemPost).Html;
 }
Пример #9
0
 /// <summary>
 /// 设置证书
 /// </summary>
 /// <param name="objhttpItem"></param>
 private void SetCer(HttpItem objhttpItem)
 {
     if (!string.IsNullOrEmpty(objhttpItem.CerPath))
     {
         //这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。
         ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
         //初始化对像,并设置请求的URL地址
         request = (HttpWebRequest)WebRequest.Create(objhttpItem.URL);
         SetCerList(objhttpItem);
         //将证书添加到请求里
         request.ClientCertificates.Add(new X509Certificate(objhttpItem.CerPath));
     }
     else
     {
         //初始化对像,并设置请求的URL地址
         request = (HttpWebRequest)WebRequest.Create(objhttpItem.URL);
         SetCerList(objhttpItem);
     }
 }
Пример #10
0
 /// <summary>
 /// 获取社区在线列表
 /// </summary>
 /// <param name="page">页数</param>
 /// <returns>网页实例</returns>
 public static string GetUsers(string page = "1")
 {
     var http = new HttpHelper();
     var item = new HttpItem
         {
             URL = "http://bbs.51cto.com/member.php?action=list&listgid=&srchmem=&order=uid&type=&page=" + page,
             Cookie = User.Cookie,
         };
     item.Header.Add("X-Requested-With", "XMLHttpRequest");
     HttpResult result = http.GetHtml(item);
     string html = result.Html;
     return html;
 }
Пример #11
0
        public static string PostReply(string postsId, string reply, string username="******")
        {
            var http = new HttpHelper();
            var posttype = username == "匿名"?"1":"0";
            var item = new HttpItem
                {
                    URL = "http://rozbo.blog.51cto.com/comment3.php", //URL     必需项
                    Method = "post", //URL     可选项 默认为Get
                    IsToLower = false, //得到的HTML代码是否转成小写     可选项默认转小写
                    Cookie = User.Cookie, //字符串Cookie     可选项
                    Referer = "http://weiba.weibo.com/10070/t/zstIRyLfE", //来源URL     可选项
                    Postdata =
                        "authnum=&tid=" + postsId + "&username="******"&content=" + HttpUtility.UrlEncode(reply, Encoding.UTF8) + "&niming=" + posttype + "&favour=1&parentid=",
                    //Post数据     可选项GET时不需要写
                    Timeout = 100000, //连接超时时间     可选项默认为100000
                    ReadWriteTimeout = 30000, //写入Post数据超时时间     可选项默认为30000
                    UserAgent = "Mozilla/5.0 (Windows NT 5.2; rv:21.0) Gecko/20100101 Firefox/21.0",
                    //用户的浏览器类型,版本,操作系统     可选项有默认值
                    ContentType = "application/x-www-form-urlencoded; charset=UTF-8", //返回类型    可选项有默认值
                    Allowautoredirect = true, //是否根据301跳转     可选项
                    //CerPath = "d:\123.cer",//证书绝对路径     可选项不需要证书时可以不写这个参数
                    //Connectionlimit = 1024,//最大连接数     可选项 默认为1024                    ProxyIp = "",//代理服务器ID     可选项 不需要代理 时可以不设置这三个参数
                    //ProxyPwd = "123456",//代理服务器密码     可选项
                    //ProxyUserName = "******",//代理服务器账户名     可选项

                };
            item.Header.Add("X-Requested-With", "XMLHttpRequest");
            HttpResult result = http.GetHtml(item);
            var html = result.Html;
            return html;
        }
Пример #12
0
 /// <summary>
 /// 为请求准备参数
 /// </summary>
 ///<param name="objhttpItem">参数列表</param>
 private void SetRequest(HttpItem objhttpItem)
 {
     //设置安全协议
     ServicePointManager.SecurityProtocol = objhttpItem.SecurityProtocolType;
     // 验证证书
     SetCer(objhttpItem);
     //设置Header参数
     if (objhttpItem.Header != null && objhttpItem.Header.Count > 0) foreach (string item in objhttpItem.Header.AllKeys)
         {
             request.Headers.Add(item, objhttpItem.Header[item]);
         }
     // 设置代理
     SetProxy(objhttpItem);
     if (objhttpItem.ProtocolVersion != null) request.ProtocolVersion = objhttpItem.ProtocolVersion;
     request.ServicePoint.Expect100Continue = objhttpItem.Expect100Continue;
     //请求方式Get或者Post
     request.Method = objhttpItem.Method;
     request.Timeout = objhttpItem.Timeout;
     request.ReadWriteTimeout = objhttpItem.ReadWriteTimeout;
     //Accept
     request.Accept = objhttpItem.Accept;
     //ContentType返回类型
     request.ContentType = objhttpItem.ContentType;
     //UserAgent客户端的访问类型,包括浏览器版本和操作系统信息
     request.UserAgent = objhttpItem.UserAgent;
     // 编码
     encoding = objhttpItem.Encoding;
     //设置Cookie
     SetCookie(objhttpItem);
     //来源地址
     request.Referer = objhttpItem.Referer;
     //是否执行跳转功能
     request.AllowAutoRedirect = objhttpItem.Allowautoredirect;
     //设置Post数据
     SetPostData(objhttpItem);
     //设置最大连接
     if (objhttpItem.Connectionlimit > 0) request.ServicePoint.ConnectionLimit = objhttpItem.Connectionlimit;
 }
Пример #13
0
 /// <summary>
 /// 设置代理
 /// </summary>
 /// <param name="objhttpItem">参数对象</param>
 private void SetProxy(HttpItem objhttpItem)
 {
     if (!string.IsNullOrEmpty(objhttpItem.ProxyIp))
     {
         //设置代理服务器
         if (objhttpItem.ProxyIp.Contains(":"))
         {
             string[] plist = objhttpItem.ProxyIp.Split(':');
             WebProxy myProxy = new WebProxy(plist[0].Trim(), Convert.ToInt32(plist[1].Trim()));
             //建议连接
             myProxy.Credentials = new NetworkCredential(objhttpItem.ProxyUserName, objhttpItem.ProxyPwd);
             //给当前请求对象
             request.Proxy = myProxy;
         }
         else
         {
             WebProxy myProxy = new WebProxy(objhttpItem.ProxyIp, false);
             //建议连接
             myProxy.Credentials = new NetworkCredential(objhttpItem.ProxyUserName, objhttpItem.ProxyPwd);
             //给当前请求对象
             request.Proxy = myProxy;
         }
         //设置安全凭证
         request.Credentials = CredentialCache.DefaultNetworkCredentials;
     }
 }
Пример #14
0
 /// <summary>
 /// 设置Post数据
 /// </summary>
 /// <param name="objhttpItem">Http参数</param>
 private void SetPostData(HttpItem objhttpItem)
 {
     //验证在得到结果时是否有传入数据
     if (request.Method.Trim().ToLower().Contains("post"))
     {
         if (objhttpItem.PostEncoding != null)
         {
             postencoding = objhttpItem.PostEncoding;
         }
         byte[] buffer = null;
         //写入Byte类型
         if (objhttpItem.PostDataType == PostDataType.Byte && objhttpItem.PostdataByte != null && objhttpItem.PostdataByte.Length > 0)
         {
             //验证在得到结果时是否有传入数据
             buffer = objhttpItem.PostdataByte;
         }//写入文件
         else if (objhttpItem.PostDataType == PostDataType.FilePath && !string.IsNullOrEmpty(objhttpItem.Postdata))
         {
             StreamReader r = new StreamReader(objhttpItem.Postdata, postencoding);
             buffer = postencoding.GetBytes(r.ReadToEnd());
             r.Close();
         } //写入字符串
         else if (!string.IsNullOrEmpty(objhttpItem.Postdata))
         {
             buffer = postencoding.GetBytes(objhttpItem.Postdata);
         }
         if (buffer != null)
         {
             request.ContentLength = buffer.Length;
             request.GetRequestStream().Write(buffer, 0, buffer.Length);
         }
     }
 }
Пример #15
0
 /// <summary>
 /// 设置Cookie
 /// </summary>
 /// <param name="objhttpItem">Http参数</param>
 private void SetCookie(HttpItem objhttpItem)
 {
     if (!string.IsNullOrEmpty(objhttpItem.Cookie))
         //Cookie
         request.Headers[HttpRequestHeader.Cookie] = objhttpItem.Cookie;
     //设置Cookie
     if (objhttpItem.CookieCollection != null)
     {
         request.CookieContainer = new CookieContainer();
         request.CookieContainer.Add(objhttpItem.CookieCollection);
     }
 }
Пример #16
0
 /// <summary>
 /// 设置多个证书
 /// </summary>
 /// <param name="objhttpItem"></param>
 private void SetCerList(HttpItem objhttpItem)
 {
     if (objhttpItem.ClentCertificates != null && objhttpItem.ClentCertificates.Count > 0)
     {
         foreach (X509Certificate item in objhttpItem.ClentCertificates)
         {
             request.ClientCertificates.Add(item);
         }
     }
 }
Пример #17
0
 /// <summary>
 /// 获取51cto博文列表
 /// </summary>
 /// <param name="page">页码</param>
 /// <returns>网页实体</returns>
 public static string GetPostsListView(string page = "1")
 {
     var http = new HttpHelper();
     var item = new HttpItem
         {
             URL = "http://blog.51cto.com/original/0/" + page, //URL     必需项
             Cookie = User.Cookie,
             Allowautoredirect = true,
             ContentType = "application/x-www-form-urlencoded",
             Accept = " text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
         };
     item.Header.Add("X-Requested-With", "XMLHttpRequest");
     HttpResult result = http.GetHtml(item);
     string html = result.Html;
     return html;
 }
Пример #18
0
 /// <summary>
 /// 根据相传入的数据,得到相应页面数据
 /// </summary>
 /// <param name="objhttpitem">参数类对象</param>
 /// <returns>返回HttpResult类型</returns>
 public HttpResult GetHtml(HttpItem objhttpitem)
 {
     //返回参数
     HttpResult result = new HttpResult();
     try
     {
         //准备参数
         SetRequest(objhttpitem);
     }
     catch (Exception ex)
     {
         result = new HttpResult()
         {
             Cookie = "",
             Header = null,
             Html = ex.Message,
             StatusDescription = "配置参数时出错:" + ex.Message
         };
         return result;
     }
     try
     {
         #region 得到请求的response
         using (response = (HttpWebResponse)request.GetResponse())
         {
             result.StatusCode = response.StatusCode;
             result.StatusDescription = response.StatusDescription;
             result.Header = response.Headers;
             if (response.Cookies != null) result.CookieCollection = response.Cookies;
             if (response.Headers["set-cookie"] != null) result.Cookie = response.Headers["set-cookie"];
             MemoryStream _stream = new MemoryStream();
             //GZIIP处理
             if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
             {
                 //开始读取流并设置编码方式
                 //new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream, 10240);
                 //.net4.0以下写法
                 _stream = GetMemoryStream(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress));
             }
             else
             {
                 //开始读取流并设置编码方式
                 //response.GetResponseStream().CopyTo(_stream, 10240);
                 //.net4.0以下写法
                 _stream = GetMemoryStream(response.GetResponseStream());
             }
             //获取Byte
             byte[] RawResponse = _stream.ToArray();
             _stream.Close();
             //是否返回Byte类型数据
             if (objhttpitem.ResultType == ResultType.Byte) result.ResultByte = RawResponse;
             //从这里开始我们要无视编码了
             if (encoding == null)
             {
                 Match meta = Regex.Match(Encoding.Default.GetString(RawResponse), "<meta([^<]*)charset=([^<]*)[\"']", RegexOptions.IgnoreCase);
                 string charter = (meta.Groups.Count > 1) ? meta.Groups[2].Value.ToLower() : string.Empty;
                 if (charter.Length > 2)
                     encoding = Encoding.GetEncoding(charter.Trim().Replace("\"", "").Replace("'", "").Replace(";", "").Replace("iso-8859-1", "gbk"));
                 else
                 {
                     if (string.IsNullOrEmpty(response.CharacterSet)) encoding = Encoding.UTF8;
                     else encoding = Encoding.GetEncoding(response.CharacterSet);
                 }
             }
             //得到返回的HTML
             result.Html = encoding.GetString(RawResponse);
         }
         #endregion
     }
     catch (WebException ex)
     {
         //这里是在发生异常时返回的错误信息
         response = (HttpWebResponse)ex.Response;
         result.Html = ex.Message;
         if (response != null)
         {
             result.StatusCode = response.StatusCode;
             result.StatusDescription = response.StatusDescription;
         }
     }
     catch (Exception ex)
     {
         result.Html = ex.Message;
     }
     if (objhttpitem.IsToLower) result.Html = result.Html.ToLower();
     return result;
 }