static Visit MakeVisitFromOptions(AjaxOptions opts, Ajax ajax) { var visit = new Visit(opts, ajax); var method = visit.Method = (opts.method ?? "GET").ToUpper(); var type = opts.type; MineType requestContentType = null; if (!MineType.MineTypes.TryGetValue(type, out requestContentType)) { requestContentType = MineType.Request; } visit.Content = requestContentType.Serialize(opts.data); var url = opts.url ?? string.Empty; if (method == "GET") { if (url.Contains("?")) { url += "&"; } else { url += "?"; } } visit.RequestUrl = url; return(visit); }
internal static object InternalRequest(Visit visit, Action <object, Visit> success, Func <Exception, Visit, bool> error) { var _webClient = new WebClient(); string responseText = null; try { if (visit.Method == "POST" || visit.Method == "PUT") { responseText = _webClient.UploadString(visit.Method, visit.Content); } else { responseText = _webClient.DownloadString(visit.Url); } } catch (Exception ex) { if (error != null) { if (!error(ex, visit)) { throw ex; } } else { throw ex; } } MineType responseContentType = null; var opts = visit.Options; if (!MineType.MineTypes.TryGetValue(opts.dataType, out responseContentType)) { responseContentType = MineType.Response; } var result = visit.Result = responseContentType.Deserialize(responseText, visit.AjaxObject.ResponseType); if (success != null) { success(result, visit); } return(result); }
static internal async Task <object> InternalRequestAsync(Visit visit, Action <object, Visit> success, Func <Exception, Visit, bool> error) { var opts = visit.Options; HttpRequestMessage requestMessage = new HttpRequestMessage(); using (HttpClient client = new HttpClient()) { HttpRequestMessage request = new HttpRequestMessage(GetMethod(visit.Method), visit.RequestUrl); if (visit.RequstHeaders != null) { foreach (var pair in visit.RequstHeaders) { request.Headers.Add(pair.Key, pair.Value); } } request.Content = new StringContent(visit.Content); //request.Headers.Add("Content-Type", requestContentType.Value); HttpResponseMessage response = null; try { response = await client.SendAsync(request); } catch (Exception ex) { if (error != null) { if (!error(ex, visit)) { throw ex; } return(null); } else { throw ex; } } visit.AjaxObject._ResponseHeaderGetter = (key) => { var header = response.Content.Headers.GetValues(key); return(header?.FirstOrDefault()); }; MineType responseContentType = null; if (!MineType.MineTypes.TryGetValue(opts.dataType, out responseContentType)) { responseContentType = MineType.Response; } if (responseContentType.ResponseKind == MineTypeKinds.Bytes) { var result = visit.Result = await response.Content.ReadAsByteArrayAsync(); if (success != null) { success(result, visit); } return(result); } else if (responseContentType.ResponseKind == MineTypeKinds.Stream) { using (var stream = await response.Content.ReadAsStreamAsync()) { if (responseContentType.ResponseAsync) { var result = visit.Result = await responseContentType.DeserializeAsync(stream); if (success != null) { success(result, visit); } return(result); } else { var result = visit.Result = responseContentType.Deserialize(stream, visit.AjaxObject.ResponseType); if (success != null) { success(result, visit); } return(result); } } } else { var result = visit.Result = responseContentType.Deserialize(await response.Content.ReadAsStringAsync(), null); if (success != null) { success(result, visit); } return(result); } } }