コード例 #1
0
ファイル: HttpUtils.cs プロジェクト: yearlingvirus/ypt
        /// <summary>
        /// HttpWebRequest 通过get
        /// </summary>
        /// <param name="url">URI</param>
        /// <returns></returns>
        public static string GetDataGetHtml(string url, CookieContainer cookie)
        {
            HttpWebRequest  req = null;
            HttpWebResponse res = null;

            try
            {
                ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
                req             = (HttpWebRequest)WebRequest.Create(url);
                req.ContentType = "application/x-www-form-urlencoded";
                req.Method      = "GET";
                req.UserAgent   = YUConst.HTTP_CHROME_UA;
                //对发送的数据不使用缓存
                req.AllowWriteStreamBuffering = false;
                req.Timeout = 10000;
                req.ServicePoint.Expect100Continue = false;

                if (cookie == null || cookie.Count == 0)
                {
                    req.CookieContainer = new CookieContainer();
                    cookie = req.CookieContainer;
                }
                else
                {
                    req.Headers.Add(HttpRequestHeader.Cookie, YUUtils.GetCookieFromContainer(cookie, new Uri(url)));
                    //req.CookieContainer = cookie;
                }

                res = (HttpWebResponse)req.GetResponse();
                Stream webStream = res.GetResponseStream();
                if (webStream == null)
                {
                    return("网络错误(Network error):" + new ArgumentNullException("webStream"));
                }
                StreamReader streamReader    = new StreamReader(webStream, Encoding.UTF8);
                string       responseContent = streamReader.ReadToEnd();

                res.Close();
                streamReader.Close();

                return(responseContent);
            }
            catch (Exception ex)
            {
                string msg = "网络错误(Network error):" + ex.GetInnerExceptionMessage();
                Logger.Error(msg, ex);
                return(msg);
            }
            finally
            {
                if (req != null)
                {
                    req.Abort();//销毁关闭连接
                }
                if (res != null)
                {
                    res.Close();//销毁关闭响应
                }
            }
        }
コード例 #2
0
ファイル: HttpUtils.cs プロジェクト: yearlingvirus/ypt
        public static string PostDataGetHtml(string url, string postData, CookieContainer cookie, bool isFormDataType = false)
        {
            HttpWebRequest  req = null;
            HttpWebResponse res = null;

            try
            {
                byte[] data = Encoding.UTF8.GetBytes(postData);
                Uri    uri  = new Uri(url);
                ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
                req = WebRequest.Create(uri) as HttpWebRequest;
                if (req == null)
                {
                    return("Network error:" + new ArgumentNullException("req").Message);
                }
                req.Accept    = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*;q=0.8";
                req.Method    = "POST";
                req.UserAgent = YUConst.HTTP_CHROME_UA;
                if (isFormDataType)
                {
                    req.ContentType = string.Format("multipart/form-data; boundary=----{0}", YUConst.POST_BOUNDARY);
                }
                else
                {
                    req.ContentType = "application/x-www-form-urlencoded";
                }
                req.ContentLength     = data.Length;
                req.Timeout           = 10000;
                req.AllowAutoRedirect = true;
                req.Referer           = url;

                if (cookie == null || cookie.Count == 0)
                {
                    req.CookieContainer = new CookieContainer();
                }
                else
                {
                    req.Headers.Add(HttpRequestHeader.Cookie, YUUtils.GetCookieFromContainer(cookie, uri));
                    //req.CookieContainer = cookie;
                }

                Stream outStream = req.GetRequestStream();
                outStream.Write(data, 0, data.Length);
                outStream.Close();
                res = req.GetResponse() as HttpWebResponse;
                if (res == null)
                {
                    return("Network error:" + new ArgumentNullException("HttpWebResponse").Message);
                }
                Stream inStream   = res.GetResponseStream();
                var    sr         = new StreamReader(inStream, Encoding.UTF8);
                string htmlResult = sr.ReadToEnd();
                return(htmlResult);
            }
            catch (Exception ex)
            {
                string msg = "网络错误(Network error):" + ex.GetInnerExceptionMessage();
                Logger.Error(msg, ex);
                return(msg);
            }
            finally
            {
                if (req != null)
                {
                    req.Abort();//销毁关闭连接
                }
                if (res != null)
                {
                    res.Close();//销毁关闭响应
                }
            }
        }