Example #1
0
        /// <summary>
        /// 请求方法
        /// </summary>
        /// <param name="bPara">设置请求参数</param>
        /// <returns></returns>
        public string GetWebData(RequestBase request)
        {
            try
            {
                BrowserPara bPara = new BrowserPara();
                switch (request.contentType)
                {
                case ContentTypeEnums.POSTDATA:
                    var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");
                    bPara.ContentType = ContentTypeEnums.POSTDATA.GetEnumDescription() + ";boundary = " + boundary;
                    break;

                default:
                    bPara.ContentType = request.contentType.GetEnumDescription();
                    break;
                }
                InitializeSessionToken(request);
                bPara.Headers = request.HeaderValue;
                if (request.contentType == ContentTypeEnums.GETFORM)
                {
                    bPara.Uri    = request.GetReqUrl();
                    bPara.Method = HttpMethod.Get.Method.ToString();
                    return(webUtils.DoGet(bPara));
                }
                bPara.Uri      = request.Url;
                bPara.PostData = request.GetRequestData();
                bPara.Method   = HttpMethod.Post.Method.ToString();
                return(webUtils.DoPost(bPara));
            }
            catch (Exception ex)
            {
                JsonResponses responses = new JsonResponses(JsonResponses.Failed.code, ex.Message);
                return(JsonConvert.SerializeObject(responses));
            }
        }
Example #2
0
        /// <summary>
        /// 请求方法
        /// </summary>
        /// <param name="bPara">设置请求参数</param>
        /// <returns></returns>
        public string GetWebData(BrowserPara bPara)
        {
            try
            {
                ServicePointManager.SecurityProtocol =
                    SecurityProtocolType.Tls12 |
                    SecurityProtocolType.Tls11 |
                    SecurityProtocolType.Tls |
                    (SecurityProtocolType)192 |
                    (SecurityProtocolType)768 |
                    SecurityProtocolType.Ssl3 |
                    (SecurityProtocolType)3072;
                ServicePointManager.ServerCertificateValidationCallback = RemoteCertificateValidate;
                ServicePointManager.CheckCertificateRevocationList      = false;
                ServicePointManager.DefaultConnectionLimit = 512;
                ServicePointManager.Expect100Continue      = false;
                var myRequest = (HttpWebRequest)WebRequest.Create(bPara.Uri);
                myRequest.Accept    = bPara.Accept;
                myRequest.UserAgent = bPara.UserAgent;
                myRequest.Referer   = bPara.Referer;
                if (bPara.Cookies != null)
                {
                    foreach (var cookie in bPara.Cookies)
                    {
                        cookieContainer.SetCookies(new Uri(bPara.Uri), string.Format(" {0}={1}; ", cookie.Key, cookie.Value));
                    }
                }
                if (bPara.Headers != null)
                {
                    foreach (var header in bPara.Headers)
                    {
                        myRequest.Headers.Add(header.Key, header.Value);
                    }
                }
                myRequest.CookieContainer = cookieContainer;
                myRequest.Method          = EnumsHelper.ValueOfRequestType(bPara.Method);
                if (bPara.Method == RequestTypeEnums.GET)
                {
                    myRequest.ContentType = EnumUtil.GetEnumExtendAttribute <ContentTypeEnums>(ContentTypeEnums.GETFORM).Desc;
                }
                else
                {
                    if (bPara.Method == RequestTypeEnums.POST_FORM)
                    {
                        var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");
                        myRequest.ContentType = EnumUtil.GetEnumExtendAttribute <ContentTypeEnums>(ContentTypeEnums.POSTFORM).Desc + "boundary = " + boundary;
                    }
                    else
                    {
                        myRequest.ContentType = EnumUtil.GetEnumExtendAttribute <ContentTypeEnums>(ContentTypeEnums.POSTJSON).Desc;
                    }
                    myRequest.MediaType = bPara.MediaType;
                    byte[] byteRequest = Encoding.UTF8.GetBytes(bPara.PostData);
                    Stream rs          = myRequest.GetRequestStream();
                    rs.Write(byteRequest, 0, byteRequest.Length);
                    rs.Close();
                }

                var myResponse = (HttpWebResponse)myRequest.GetResponse();

                CookieStr += myResponse.Headers.Get("Set-Cookie");
                ProcessCookies(myResponse.Cookies);
                var stream       = myResponse.GetResponseStream();
                var returnStream = new StreamReader(stream, Encoding.UTF8);
                var html         = returnStream.ReadToEnd();
                returnStream.Close();
                myResponse.Close();
                Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");
                return(reg.Replace(html, delegate(Match m) { return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString(); }));
            }
            catch (System.Exception ex)
            {
                JsonResponses responses = new JsonResponses(JsonResponses.Failed.code, ex.Message);
                return(JsonConvert.SerializeObject(responses));
            }
        }