Exemplo n.º 1
0
        public static Object HttpGet(HttpItem item)
        {
            HttpWebRequest  request  = null;
            HttpWebResponse response = null;

            try
            {
                //拼接参数
                string postData = string.Empty;
                if (item.PostData != null)
                {
                    item.PostData.All(o =>
                    {
                        if (String.IsNullOrEmpty(postData))
                        {
                            postData = string.Format("?{0}={1}", o.Key, o.Value);
                        }
                        else
                        {
                            postData += string.Format("&{0}={1}", o.Key, o.Value);
                        }
                        return(true);
                    });
                }
                request             = (HttpWebRequest)WebRequest.Create(item.URL + postData);
                request.ContentType = item.ContentType;
                request.Method      = "GET";
                request.Accept      = item.Accept;
                request.UserAgent   = item.UserAgent;
                if (item.NeedInitReqCookieContainer)
                {
                    request.CookieContainer = new CookieContainer();
                }
                //添加Referer
                if (!String.IsNullOrEmpty(item.Referer))
                {
                    request.Referer = item.Referer;
                }
                //添加Cookie
                if (!String.IsNullOrEmpty(item.Cookie))
                {
                    request.Headers[HttpRequestHeader.Cookie] = item.Cookie;
                }
                response = (HttpWebResponse)request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                if (!String.IsNullOrEmpty(BeforLoginCookie))
                {
                    BeforLoginCookie = String.Empty;
                }
                BeforLoginCookie = response.GetResponseHeader("Set-Cookie");
                BeforLoginCookie = Regex.Replace(BeforLoginCookie, "path(?:[^,]+),?", "", RegexOptions.IgnoreCase);
                //第一次初始化客户端Cookie
                if (item.IsInitialLoaded)
                {
                    InitialCookie = response.GetResponseHeader("Set-Cookie");
                    InitialCookie = Regex.Replace(InitialCookie, "path(?:[^,]+),?", "", RegexOptions.IgnoreCase);
                }
                //设置系统Cookie
                if (item.NeedSetJSessionID)
                {
                    JSessionCookie = response.GetResponseHeader("Set-Cookie");
                    JSessionCookie = Regex.Replace(JSessionCookie, "path(?:[^,]+),?", "", RegexOptions.IgnoreCase);
                    SystemCache.SetSysObj("JSessionCookieID", JSessionCookie);
                }
                string responseStr = String.Empty;
                if (responseStream != null)
                {
                    StreamReader streamReader = new StreamReader(responseStream, item.Encoding);
                    responseStr = streamReader.ReadToEnd();
                    streamReader.Close();
                }
                responseStream.Close();
                response.Close();
                request.Abort();

                return(responseStr);
            }
            catch (Exception ex)
            {
                LogHelper.Log("HttpGet", ex.Message);
                return(null);
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 发起一个HTTP请求(以POST方式)
        /// </summary>
        /// <param name="Url">url地址</param>
        /// <param name="data">发送数据</param>
        /// <returns></returns>
        public static string HttpPost(HttpItem item)
        {
            HttpWebResponse response       = null;
            HttpWebRequest  request        = null;
            Stream          responseStream = null;
            StreamReader    answerData     = null;
            String          srcString      = "";

            try
            {
                //拼接参数
                string postData = string.Empty;
                if (item.PostData != null)
                {
                    item.PostData.All(o =>
                    {
                        if (String.IsNullOrEmpty(postData))
                        {
                            postData = string.Format("{0}={1}", o.Key, o.Value);
                        }
                        else
                        {
                            postData += string.Format("&{0}={1}", o.Key, o.Value);
                        }
                        return(true);
                    });
                }
                var    retString = string.Empty;
                byte[] postDatas = Encoding.UTF8.GetBytes(postData);

                // 设置提交的相关参数
                request = WebRequest.Create(item.URL) as HttpWebRequest;
                request.ServicePoint.ConnectionLimit = item.Connectionlimit;
                request.Timeout = item.Timeout; //设置超时时间
                request.Method  = "POST";
                //webRequest.ClientCertificates.Add();//客户端证书
                request.KeepAlive         = false;
                request.ProtocolVersion   = item.ProtocolVersion;//Http协议版本
                request.AllowAutoRedirect = false;
                request.ContentType       = item.ContentType;
                request.UserAgent         = item.UserAgent;
                request.ContentLength     = postDatas.Length;
                request.Accept            = item.Accept;
                request.Headers.Add("x-requested-with", "XMLHttpRequest");
                //添加Cookie
                if (!String.IsNullOrEmpty(item.Cookie))
                {
                    request.Headers[HttpRequestHeader.Cookie] = item.Cookie;
                }
                // 提交请求数据
                Stream outputStream = request.GetRequestStream();
                outputStream.Write(postDatas, 0, postDatas.Length);
                outputStream.Close();
                //请求返回**********************************************************************************
                response = request.GetResponse() as HttpWebResponse;
                //CookieCollection cookieCollect = response.Cookies;
                string set_cookie = response.GetResponseHeader("Set-Cookie");
                //AfterLoginCookie = set_cookie;
                responseStream = response.GetResponseStream();
                answerData     = new StreamReader(responseStream, Encoding.UTF8); //Encoding.GetEncoding("UTF-8")
                srcString      = answerData.ReadToEnd();
                retString      = srcString;                                       //返回值赋值
                if (item.IsInitialLoaded)
                {
                    AuthFirstCookie = response.GetResponseHeader("Set-Cookie");
                    AuthFirstCookie = Regex.Replace(AuthFirstCookie, "path(?:[^,]+),?", "", RegexOptions.IgnoreCase);
                }
                answerData.Close();
                responseStream.Close();
                response.Close();
                request.Abort();

                return(retString);
            }
            catch (Exception ex)
            {
                LogHelper.Log("HttpPost", ex.Message);
                throw new Exception(ex.Message);
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
                if (responseStream != null)
                {
                    responseStream.Close();
                }
                if (answerData != null)
                {
                    answerData.Close();
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 发起一个HTTP请求(以GET方式)
        /// </summary>
        /// <param name="sourceUrl">Url原下载地址</param>
        /// <param name="localUrl">本地下载目录地址</param>
        /// <returns></returns>
        public static Object HttpGet(String sourceUrl, String localUrl)
        {
            HttpWebRequest  request        = null;
            HttpWebResponse response       = null;
            Stream          responseStream = null;

            try
            {
                request             = (HttpWebRequest)WebRequest.Create(sourceUrl + localUrl);
                request.ContentType = "application/x-www-form-urlencoded";
                request.Method      = "GET";
                request.KeepAlive   = true;
                request.Accept      = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
                request.UserAgent   = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36";
                //"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
                //"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0.1) Gecko/20100101 Firefox/5.0.1"
                response       = (HttpWebResponse)request.GetResponse();
                responseStream = response.GetResponseStream();
                //设置客户端Cookie
                BeforLoginCookie = response.GetResponseHeader("Set-Cookie");
                BeforLoginCookie = Regex.Replace(BeforLoginCookie, "path(?:[^,]+),?", "", RegexOptions.IgnoreCase);

                if (responseStream != null)
                {
                    Bitmap img     = new Bitmap(responseStream);
                    string dirpath = localUrl + @"Temp\";
                    //创建文件夹
                    if (!Directory.Exists(dirpath))
                    {
                        Directory.CreateDirectory(dirpath);
                    }
                    dirpath = dirpath + @"captcha-image.jpg";
                    //删除过期图片(自动覆盖)
                    //if (File.Exists(dirpath))
                    //{
                    //    File.Delete(dirpath);
                    //}
                    img.Save(dirpath, ImageFormat.Jpeg);
                    img.Dispose();

                    responseStream.Close();
                    response.Close();
                    request.Abort();

                    return(dirpath);
                }
                return(String.Empty);
            }
            catch (Exception ex)
            {
                LogHelper.Log("HttpGet", ex.Message);
                return(String.Empty);
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
        }