示例#1
0
        /// <summary>
        ///     Send request to the remote uri
        /// </summary>
        /// <param name="uri">The remote uri</param>
        /// <param name="requestMethod">The http request method</param>
        /// <param name="headers">The http reqeust headers</param>
        /// <param name="requestData">The request data</param>
        /// <param name="timeout">Timeout in milliseconds</param>
        /// <returns>Server response</returns>
        public static async Task <FHResponse> SendAsync(Uri uri, string requestMethod,
                                                        IDictionary <string, string> headers, object requestData, TimeSpan timeout)
        {
            var timer = new Stopwatch();

            var logger = ServiceFinder.Resolve <ILogService>();
            var online = await IsOnlineAsync();

            FHResponse fhres;

            if (!online)
            {
                var exception = new FHException("offline", FHException.ErrorCode.NetworkError);
                fhres = new FHResponse(exception);
                return(fhres);
            }
            Contract.Assert(null != uri, "No request uri defined");
            Contract.Assert(null != requestMethod, "No http request method defined");
            var httpClient = FHHttpClientFactory.Get();

            try
            {
                logger.d(LogTag, "Send request to " + uri, null);
                httpClient.DefaultRequestHeaders.Add("User-Agent", "FHSDK/DOTNET");
                httpClient.MaxResponseContentBufferSize = BufferSize;
                httpClient.Timeout = timeout;


                var requestMessage = new HttpRequestMessage(new HttpMethod(requestMethod),
                                                            BuildUri(uri, requestMethod, requestData));
                if (null != headers)
                {
                    foreach (var item in headers)
                    {
                        requestMessage.Headers.Add(item.Key, item.Value);
                    }
                }

                if (requestMethod != null && ("POST".Equals(requestMethod.ToUpper()) || "PUT".Equals(requestMethod.ToUpper())))
                {
                    if (null != requestData)
                    {
                        var requestDataStr = JsonConvert.SerializeObject(requestData);
                        requestMessage.Content = new StringContent(requestDataStr, Encoding.UTF8, "application/json");
                    }
                }

                timer.Start();
                var responseMessage = await httpClient.SendAsync(requestMessage, CancellationToken.None);

                timer.Stop();
                logger.d(LogTag, "Reqeust Time: " + timer.ElapsedMilliseconds + "ms", null);
                var responseStr = await responseMessage.Content.ReadAsStringAsync();

                logger.d(LogTag, "Response string is " + responseStr, null);
                if (responseMessage.IsSuccessStatusCode)
                {
                    fhres = new FHResponse(responseMessage.StatusCode, responseStr);
                }
                else
                {
                    var ex = new FHException("ServerError", FHException.ErrorCode.ServerError);
                    fhres = new FHResponse(responseMessage.StatusCode, responseStr, ex);
                }
            }
            catch (HttpRequestException he)
            {
                logger.e(LogTag, "HttpRequestException", he);
                var fhexception = new FHException("HttpError", FHException.ErrorCode.HttpError, he);
                fhres = new FHResponse(fhexception);
            }
            catch (Exception e)
            {
                logger.e(LogTag, "Exception", e);
                var fhexception = new FHException("UnknownError", FHException.ErrorCode.UnknownError, e);
                fhres = new FHResponse(fhexception);
            }
            httpClient.Dispose();
            return(fhres);
        }
示例#2
0
        /// <summary>
        /// Send request to the remote uri
        /// </summary>
        /// <param name="uri">The remote uri</param>
        /// <param name="requestMethod">The http request method</param>
        /// <param name="headers">The http reqeust headers</param>
        /// <param name="requestData">The request data</param>
        /// <param name="timeout">Timeout in milliseconds</param>
        /// <returns>Server response</returns>
		public static async Task<FHResponse> SendAsync(Uri uri, string requestMethod, IDictionary<string, string> headers, object requestData, TimeSpan timeout)
        {
			Stopwatch timer = new Stopwatch ();

			ILogService logger = ServiceFinder.Resolve<ILogService> ();
            bool online = await IsOnlineAsync();
            FHResponse fhres = null;
            if (!online)
            {
                FHException exception = new FHException("offline", FHException.ErrorCode.NetworkError);
                fhres = new FHResponse(exception);
                return fhres;
            }
			Contract.Assert (null != uri, "No request uri defined");
			Contract.Assert (null != requestMethod, "No http request method defined");
			HttpClient httpClient = FHHttpClientFactory.Get ();

            try
            {
				logger.d(LOG_TAG, "Send request to " + uri, null);
				httpClient.DefaultRequestHeaders.Add("User-Agent", "FHSDK/DOTNET");
                httpClient.MaxResponseContentBufferSize = BUFFER_SIZE;
				httpClient.Timeout = timeout;
                

				HttpRequestMessage requestMessage = new HttpRequestMessage(new HttpMethod(requestMethod), BuildUri(uri, requestMethod, requestData));
				if(null != headers){
					foreach (var item in headers) {
						requestMessage.Headers.Add(item.Key, item.Value);
					}
				}

				if("POST".Equals(requestMethod.ToUpper()) || "PUT".Equals(requestMethod.ToUpper())){
					if(null != requestData){
						string requestDataStr = JsonConvert.SerializeObject(requestData);
						requestMessage.Content = new StringContent(requestDataStr, Encoding.UTF8, "application/json");
					}
				}

				timer.Start ();
				HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage);
				timer.Stop();
				logger.d(LOG_TAG, "Reqeust Time: " + timer.ElapsedMilliseconds + "ms", null);
                string responseStr = await responseMessage.Content.ReadAsStringAsync();
				logger.d(LOG_TAG, "Response string is " + responseStr, null);
                if (responseMessage.IsSuccessStatusCode)
                {
                    fhres = new FHResponse(responseMessage.StatusCode, responseStr);
                }
                else
                {
                    FHException ex = new FHException("ServerError", FHException.ErrorCode.ServerError);
                    fhres = new FHResponse(responseMessage.StatusCode, responseStr, ex);
                }
            }
            catch (HttpRequestException he)
            {
				logger.e (LOG_TAG, "HttpRequestException", he);
                FHException fhexception = new FHException("HttpError", FHException.ErrorCode.HttpError, he);
                fhres = new FHResponse(fhexception);
            }
            catch (Exception e)
            {
				logger.e (LOG_TAG, "Exception", e);
                FHException fhexception = new FHException("UnknownError", FHException.ErrorCode.UnknownError, e);
                fhres = new FHResponse(fhexception);
            }
            httpClient.Dispose();
            return fhres;
        }