Esempio n. 1
0
        public string HttpPost(string url, string strJson)
        {
            string result = "";

            try
            {
                if (!IsInternet)
                {
                    ResponseData responseData = new ResponseData();
                    responseData.result  = "";
                    responseData.message = "当前不存在网络,请使用离线模式。";
                    return(JsonHelper.Serialize(responseData));
                }
                HttpWebRequest request    = (HttpWebRequest)HttpWebRequest.Create(url);
                Stream         dataStream = null;


                request.Method      = Method;
                request.ContentType = ContentType;
                if (!string.IsNullOrEmpty(TGT))
                {
                    request.Headers.Add("Cookie", TGT);
                }

                request.Timeout                      = this.TimeOut;
                request.ReadWriteTimeout             = this.ReadWriteTimeout;
                request.MaximumAutomaticRedirections = 100;
                byte[] bytes = Encoding.UTF8.GetBytes(strJson);
                using (dataStream = request.GetRequestStream())
                {
                    dataStream.Write(bytes, 0, bytes.Length);
                }

                var response = (HttpWebResponse)request.GetResponse();
                ResponseExceptionBase responseExceptionBase = new ResponseExceptionBase(response.StatusCode);
                if (responseExceptionBase.IsSuccess)
                {
                    using (var stream = response.GetResponseStream())
                    {
                        var resultBuffer = this.GetResponseBuffter(stream);
                        result = ResponseEncoding.GetString(resultBuffer);
                    }
                }
                response.Close();
                if (!responseExceptionBase.IsSuccess)
                {
                    return(null);
                }

                return(result);
            }
            catch (Exception ex)
            {
                ResponseData responseData = new ResponseData();
                responseData.result  = "1111";
                responseData.message = ex.Message;
                return(JsonHelper.Serialize(responseData));
            }
        }
Esempio n. 2
0
        public string HttpGet(string url, IDictionary <string, string> queryStrings = null)
        {
            string result = "";

            try
            {
                string fullUrl = url;
                if (queryStrings != null)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (var query in queryStrings)
                    {
                        if (query.Value != null)
                        {
                            sb.Append(string.Format("&{0}={1}", query.Key, query.Value));
                        }
                    }
                    if (sb.Length > 0)
                    {
                        fullUrl = string.Format("{0}?{1}", url, sb.ToString().Trim('&'));
                    }
                }

                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(fullUrl);
                request.Method      = Method;
                request.ContentType = ContentType;
                if (!string.IsNullOrEmpty(TGT))
                {
                    if (TGT.Contains("CASTGC"))
                    {
                        request.Headers.Add("Cookie", TGT);
                    }
                    else
                    {
                        request.Headers.Add("TGT", TGT);
                    }
                }

                request.Timeout                      = this.TimeOut;
                request.ReadWriteTimeout             = this.ReadWriteTimeout;
                request.MaximumAutomaticRedirections = 100;
                var response = (HttpWebResponse)request.GetResponse();
                ResponseExceptionBase responseExceptionBase = new ResponseExceptionBase(response.StatusCode);
                if (responseExceptionBase.IsSuccess)
                {
                    using (var stream = response.GetResponseStream())
                    {
                        var resultBuffer = this.GetResponseBuffter(stream);
                        result = ResponseEncoding.GetString(resultBuffer);
                    }
                }
                response.Close();
                if (!responseExceptionBase.IsSuccess)
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(result);
        }