Ejemplo n.º 1
0
 public Task<HttpResponse> ExecuteAsStringAsync(HttpRequest request)
 {
     return Task.Factory.StartNew(() => ExecuteAsString(request));
 }
Ejemplo n.º 2
0
 public HttpResponse ExecuteAsString(HttpRequest request)
 {
     UniHttpRequest uniRequest = ConvertRequest(request);
     return ConvertResponse(uniRequest.asString());
 }
Ejemplo n.º 3
0
        private static UniHttpRequest ConvertRequest(HttpRequest request)
        {
            var uniMethod = ConvertHttpMethod(request.HttpMethod);
            var queryUrl = request.QueryUrl;

            //instantiate unirest request object
            UniHttpRequest uniRequest = new UniHttpRequest(uniMethod,queryUrl);

            //set request payload
            if (request.Body != null)
            {
                uniRequest.body(request.Body);
            }
            else if (request.FormParameters != null)
            {
                if (request.FormParameters.Any(p => p.Value is Stream || p.Value is FileStreamInfo))
                {
                    //multipart
                    foreach (var kvp in request.FormParameters)
                    {
                        if (kvp.Value is FileStreamInfo){
                            var fileInfo = (FileStreamInfo) kvp.Value;
                            uniRequest.field(kvp.Key,fileInfo.FileStream);
                            continue;
                        }
                        uniRequest.field(kvp.Key,kvp.Value);
                    }
                }
                else
                {
                    //URL Encode params
                    var paramsString = string.Join("&",
                        request.FormParameters.Select(kvp =>
                        string.Format("{0}={1}", Uri.EscapeDataString(kvp.Key), Uri.EscapeDataString(kvp.Value.ToString()))));
                    uniRequest.body(paramsString);
                    uniRequest.header("Content-Type", "application/x-www-form-urlencoded");

                }

            }

            //set request headers
            Dictionary<String, Object> headers = request.Headers.ToDictionary(item=> item.Key,item=> (Object) item.Value);
            uniRequest.headers(headers);

            //Set basic auth credentials if any
            if (!string.IsNullOrWhiteSpace(request.Username))
            {
                uniRequest.basicAuth(request.Username, request.Password);
            }
            return uniRequest;
        }
Ejemplo n.º 4
0
 public HttpResponse ExecuteAsBinary(HttpRequest request)
 {
     UniHttpRequest uniRequest = ConvertRequest(request);
     return ConvertResponse(uniRequest.asBinary());
 }
Ejemplo n.º 5
0
 public HttpContext(HttpRequest request, HttpResponse response)
 {
     Request = request;
     Response = response;
 }