public async Task <JsonResult> Random(string q = "") { using (var client = new HttpClient()) { //client.BaseAddress = new Uri("http://api.giphy.com/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add("User-Agent", "apiroulette.com"); if (!String.IsNullOrWhiteSpace(q)) { UrlEncoder urlEncoder = UrlEncoder.Create(); string encodedUrl = urlEncoder.Encode(q); } string url = APIs.GetQueryString(q); HttpResponseMessage response = await client.GetAsync(url); if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject <dynamic>(json); return(Json(result)); } } return(Json(new {})); }
public ProfessorController(IMapper mapper, IModuloAppService moduloAppService, IUnidadeAppService unidadeAppService, IRecuperarArquivosAppService arquivoAppService, IDelecaoDeArquivosAppService deletarAppService, IEnviarArquivosAppService enviarAquivoAppService, IProfessorAppService professorAppService, ILerArquivoAppService lerArquivoAppService, ILerArquivoEmBytesAppService lerArquivoEmBytesAppService) { _mapper = mapper; _professorAppService = professorAppService; _unidadeAppService = unidadeAppService; _arquivoAppService = arquivoAppService; _enviarAquivoAppService = enviarAquivoAppService; _encoder = UrlEncoder.Create(); }
public CoordenadorController(IMapper mapper, IModuloAppService moduloAppService, IUnidadeAppService unidadeAppService, IRecuperarArquivosAppService recuperarArquivoAppService, IDelecaoDeArquivosAppService deletarArquivoAppService, IEnviarArquivosAppService enviarArquivoAppService, ICoordenadorAppService coordenadorAppService, ILerArquivoEmBytesAppService lerArquivoEmBytesAppService, ILerArquivoAppService lerArquivoAppService) { _mapper = mapper; _moduloAppService = moduloAppService; _unidadeAppService = unidadeAppService; _recuperarArquivoAppService = recuperarArquivoAppService; _enviarArquivoAppService = enviarArquivoAppService; _coordenadorAppService = coordenadorAppService; _encoder = UrlEncoder.Create(); }
/// <summary> /// Adds <see cref="HtmlEncoder"/>, <see cref="JavaScriptEncoder"/> and <see cref="UrlEncoder"/> /// to the specified <paramref name="services" />. /// </summary> /// <param name="services">The <see cref="IServiceCollection"/>.</param> /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns> public static IServiceCollection AddWebEncoders(this IServiceCollection services) { if (services == null) { throw new ArgumentNullException(nameof(services)); } services.AddOptions(); // Register the default encoders // We want to call the 'Default' property getters lazily since they perform static caching services.TryAddSingleton( CreateFactory(() => HtmlEncoder.Default, settings => HtmlEncoder.Create(settings))); services.TryAddSingleton( CreateFactory(() => JavaScriptEncoder.Default, settings => JavaScriptEncoder.Create(settings))); services.TryAddSingleton( CreateFactory(() => UrlEncoder.Default, settings => UrlEncoder.Create(settings))); return(services); }
public IEnumerable <Track> Search(string query) { var client = new HttpClient(); client.BaseAddress = new Uri("https://api.spotify.com/v1/search"); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // List data response. HttpResponseMessage response = client.GetAsync($"?q={UrlEncoder.Create().Encode(query)}&type=track").Result; if (response.IsSuccessStatusCode) { var responseBody = response.Content.ReadAsStringAsync().Result; var jobject = JObject.Parse(responseBody); foreach (var track in jobject["tracks"]["items"]) { yield return(new Track(track["id"].ToString(), track["preview_url"].ToString(), track["artists"].First()["name"].ToString(), track["name"].ToString())); } } }
private static async Task <T> http <T>(HttpMethod method, string url, Dictionary <string, object> opt) { var client = new HttpClient(); var data = new Dictionary <string, string>(); var files = new Dictionary <string, StreamContent>(); var encoder = UrlEncoder.Create(); StringContent jsonBody = null; if (opt != null) { foreach (var key in opt) { var k = key.Key; var v = key.Value; if (k[0] == '@') { client.DefaultRequestHeaders.Add(k.Substring(1), encoder.Encode(v + "")); } else if (k == "json") { jsonBody = new StringContent((v is JObject ? (JObject)v : v) + ""); } else if (v is Stream) { files.Add(k, new StreamContent((Stream)v)); } else { data[k] = v + ""; } } } var request = new HttpRequestMessage(method, url); if (files.Count > 0) { var mc = new MultipartFormDataContent(); foreach (var key in files) { mc.Add(key.Value, key.Key); } foreach (var key in data) { mc.Add(new StringContent(key.Value), key.Key); } request.Content = mc; } else if (data.Count > 0) { if (method == HttpMethod.Get) { request.RequestUri = new Uri(QueryHelpers.AddQueryString(url, data)); } else { request.Content = new FormUrlEncodedContent(data); } } var response = await client.SendAsync(request); switch (TYPES[typeof(T)]) { case "byte[]": return(to <T>(await response.Content.ReadAsByteArrayAsync())); case "stream": return(to <T>(await response.Content.ReadAsStreamAsync())); case "string": return(to <T>(await response.Content.ReadAsStringAsync())); default: log("GET:invalid type - " + typeof(T)); return(default(T)); } }
public static string GetValue(IHtmlHelper helper, object item, DataColumn field) { var urlEncoder = UrlEncoder.Create(new TextEncoderSettings()); var r = string.Empty; #region IMAGE if (field.EditorType == ColumnTypes.Image) { var col = field as ImageColumn; var valUrl = InspectDataFormat(item, field, col.ImageUrlFormat); if (valUrl == null) { return(r); } r += "<img src='" + valUrl + "'"; if (col.ImageSize.Height > 0) { r += " height='" + urlEncoder.Encode(col.ImageSize.Height.ToString()) + "'"; } if (col.ImageSize.Width > 0) { r += " width='" + urlEncoder.Encode(col.ImageSize.Width.ToString()) + "'"; } r += "/>"; } #endregion #region LINK else if (field.EditorType == ColumnTypes.Link) { var col = field as LinkColumn; var valUrl = InspectDataFormat(item, field, col.NavigateUrlFormat); var label = string.IsNullOrWhiteSpace(col.Text) ? InspectDataFormat(item, field) : col.Text; if (valUrl == null) { return(r); } r += "<a href='" + valUrl + "'"; if (!string.IsNullOrWhiteSpace(col.Target)) { r += " target='" + col.Target + "'"; } if (!string.IsNullOrWhiteSpace(col.CssClass)) { r += " class='" + col.CssClass + "'"; } r += ">" + label + "</a>"; } #endregion #region CHECKBOX else if (field.EditorType == ColumnTypes.CheckBox) { var val = InspectDataFormat(item, field); if (val == null) { return(r); } r += "<input name='select-" + field.FieldName + "' id='select-" + field.FieldName + "' type='checkbox' value='" + val + "'/>"; } #endregion else { var val = InspectDataFormat(item, field); if (val == null) { return(r); } var valType = val.GetType(); if (valType.IsEnum) { r = helper.Encode(EnumHelper.GetEnumDisplayText(valType, (int)val)); } else if (valType.FullName == "System.Boolean") { bool bVal; if (bool.TryParse(val.ToString(), out bVal)) { r += "<input type='checkbox' disabled='disabled' value='" + bVal + "'"; if (bVal) { r += " checked='checked'"; } r += "/>"; } else { r = helper.Encode(val); } } else if (valType.FullName.IndexOf("System.DateTime") > -1) { DateTime bVal; r = helper.Encode(DateTime.TryParse(val.ToString(), out bVal) ? bVal.ToString(field.Format) : val); } else if (valType.FullName.IndexOf("System.Int32") > -1) { int bVal; r = helper.Encode(int.TryParse(val.ToString(), out bVal) ? bVal.ToString(field.Format) : val); } else if (valType.FullName.IndexOf("System.Decimal") > -1) { decimal bVal; r = helper.Encode(decimal.TryParse(val.ToString(), out bVal) ? bVal.ToString(field.Format) : val); } else { r = field.EditorType == ColumnTypes.Label ? helper.Encode(val.ToString()) : val.ToString(); } } return(r); }
private static string GetMongoConnStr(string endpoint, string user, string password, string database) { return string.Format(BaseConnStr, user, UrlEncoder.Create().Encode(password), endpoint, database); }
public async Task <ActionResult> Archive(string branch = "live", string tome = "Tome01") { tome = UrlEncoder.Create().Encode(tome); return(Content(await _dbdService.GetCdnContent($"/gameinfo/archiveStories/v1/{tome}.json", branch))); }