private OperateResult GetOperationResult(RestResponse result)
		{
			string jsonString = result.GetBody();
			if (!string.IsNullOrEmpty(jsonString))
			{
				var hitResult = JsonSerializer.Get<OperateResult>(jsonString);
				hitResult.JsonString = jsonString;
				return hitResult;
			}
			return new OperateResult();
		}
        public RestResponse Process(string clusterName, string strUrl, string reqdata, string encoding, Method method)
        {
            DateTime start = DateTime.Now;

            var result = new RestResponse();
            string responseStr = string.Empty;
            string url = null;
            try
            {
                if (!strUrl.StartsWith("/"))
                {
                    strUrl = "/" + strUrl;
                }
                url = ESNodeManager.Instance.GetHttpNode(clusterName) + strUrl;
                WebRequest request = WebRequest.Create(url);
                request.Method = method.ToString();
                if (reqdata != null)
                {
                    byte[] buf = Encoding.GetEncoding(encoding).GetBytes(reqdata);
                    request.ContentType = "application/json; charset=" + encoding;
                    request.ContentLength = buf.Length;

                    if (method != Method.GET || reqdata.Length > 0)
                    {
                        Stream s = request.GetRequestStream();
                        s.Write(buf, 0, buf.Length);
                        s.Close();
                    }
                }

                using (var webResponse = (HttpWebResponse) request.GetResponse())
                {
                    using (Stream stream = webResponse.GetResponseStream())
                    {
                        long contentLength = webResponse.ContentLength;
                        var bytes = new byte[contentLength];
                        bytes = ReadFully(stream);
                        stream.Close();
                        responseStr = Encoding.GetEncoding(encoding).GetString(bytes);
                        result.SetBody(responseStr);
                        result.Status = Status.OK;
                    }
                }

                DateTime endtime = DateTime.Now;
                _logger.InfoFormat("Request Success,Method:{2},Url:{0},Body:{1},Time:{3}", url, reqdata, method,
                                   endtime - start);
            }
            catch (WebException e)
            {
                DateTime endtime = DateTime.Now;
                if (e.Response != null)
                {
                    Stream stream = e.Response.GetResponseStream();
                    if (stream != null)
                    {
                        try
                        {
                            var reader = new StreamReader(stream);
                            responseStr = reader.ReadToEnd();
                            result.SetBody(responseStr);
                        }
                        catch (System.Exception exception)
                        {
                            _logger.Error(exception);
                        }
                    }
                }
                string msg = string.Format("Method:{2}, Url: {0},Body:{1},Encoding:{3},Time:{5},Response:{4}", url,
                                           reqdata,
                                           method, encoding, responseStr, endtime - start);
                result.Status = Status.INTERNAL_SERVER_ERROR;
                ExceptionHandler.HandleExceptionResponse(responseStr, msg);
            }
            return result;
        }
Beispiel #3
0
			public void Read(TProtocol iprot)
			{
				TField field;
				iprot.ReadStructBegin();
				while (true)
				{
					field = iprot.ReadFieldBegin();
					if (field.Type == TType.Stop)
					{
						break;
					}
					switch (field.ID)
					{
						case 0:
							if (field.Type == TType.Struct)
							{
								success = new RestResponse();
								success.Read(iprot);
								__isset.success = true;
							}
							else
							{
								TProtocolUtil.Skip(iprot, field.Type);
							}
							break;
						default:
							TProtocolUtil.Skip(iprot, field.Type);
							break;
					}
					iprot.ReadFieldEnd();
				}
				iprot.ReadStructEnd();
			}