public async Task <U> ExecAsync <U>(HttpIORequest request) where U : new() { string url = request.URI; if (request.QueryString != null) { var builder = new UriBuilder(request.URI); builder.Port = -1; // Use default builder.Query = ToQueryString(request.QueryString); url = builder.ToString(); } switch (request.Verb) { case HttpVerb.GET: return(await ExecAsyncGet <U>(request, url).ConfigureAwait(false)); } return(default(U)); }
public async Task <List <Image> > GetImagesHttp() { ImageList.IO.HttpIORequest request = new ImageList.IO.HttpIORequest(); request.URI = "http://md5.jsontest.com/"; request.Verb = ImageList.IO.HttpVerb.GET; request.ContentType = ImageList.IO.DataType.JSON; request.Authentication = ImageList.IO.AuthType.NONE; request.QueryString = new Dictionary <string, string>(); request.QueryString["text"] = "example_text"; TransactionCookie httpTransaction = new TransactionCookie(1, this); httpTransaction.Request = request; HttpIO <HttpIOHandler> httpIO = new HttpIO <HttpIOHandler>(httpTransaction); //httpIO.OnHttpAppException += ((msg) => //{ // //Log(msg); //}); //httpIO.OnHttpFatalException += ((msg) => //{ // //Log(msg); //}); List <Image> imgLst = await httpIO.ExecAsync <List <Image> >(request).ConfigureAwait(false); return(imgLst); // JSON parsing without serialization... if (imgLst != null) { string md5 = ""; bool bReadNxt = false; JsonTextReader reader = new JsonTextReader(new StringReader(imgLst.ToString())); while (reader.Read()) { /* * TokenType "PropertyName" means its a JSON Key * Corresponding Value represents the key Name * TokenType for a JSON value is the data type like String, Boolean, Integer... * Corresponding Value is the value of the JSON Value */ if (reader.Value != null) { //Log(string.Format("Token: {0}, Value: {1}", reader.TokenType, reader.Value)); if (bReadNxt) { md5 = reader.Value.ToString(); bReadNxt = false; //Log(string.Format("VALUE: {0}", md5)); } if (reader.TokenType.ToString() == "PropertyName" && reader.Value.ToString() == "md5") { bReadNxt = true; } } else { //Log(string.Format("Token: {0}", reader.TokenType)); } } } }
private async Task <U> ExecAsyncGet <U>(HttpIORequest request, string url) where U : new() { U deserializedObj = default(U); using (var client = new HttpClient()) { HttpResponseMessage response = null; client.Timeout = new TimeSpan(0, 0, 9000); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); if (request.Authentication == AuthType.BASIC) { string sAuth = request.BasicAuthCreds.Username + ":" + request.BasicAuthCreds.Password; var byteArray = Encoding.UTF8.GetBytes(sAuth); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); } else if (request.Authentication == AuthType.OAUTH2) { /* * To generate oauth2 header following items are needed * 1. Consumer key * 2. Consumer secret * 3. Token (optional) * 4. Token secret (optional) */ //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", authHeader); } try { response = await client.GetAsync(url).ConfigureAwait(false); if (response.IsSuccessStatusCode) { string strRspns = await response.Content.ReadAsStringAsync().ConfigureAwait(false); deserializedObj = await Task.Run ( () => JsonConvert.DeserializeObject <U>(strRspns) ).ConfigureAwait(false); } else { string msg = "Http Server threw exception"; if (response != null) { msg += string.Format(": HTTP {0}\n{1}", response.StatusCode, Convert.ToString(response)); } (_handlerObject as I_IOHandler).OnBusinessError(this, msg, _consumerCookie); } } catch (Exception ex) { // Most likely Severe System or Networkr related exception string msg = "Http fatal error: " + ex.Message; (_handlerObject as I_IOHandler).OnNetworkError(this, msg, AbortSource.Server, AbortReason.NotSpecified, _consumerCookie); //throw new HttpIOException("Http operation failed with message:" + ex.Message); } } return(deserializedObj); }
public async Task<List<Image>> GetImagesHttp() { ImageList.IO.HttpIORequest request = new ImageList.IO.HttpIORequest(); request.URI = "http://md5.jsontest.com/"; request.Verb = ImageList.IO.HttpVerb.GET; request.ContentType = ImageList.IO.DataType.JSON; request.Authentication = ImageList.IO.AuthType.NONE; request.QueryString = new Dictionary<string, string>(); request.QueryString["text"] = "example_text"; TransactionCookie httpTransaction = new TransactionCookie(1, this); httpTransaction.Request = request; HttpIO<HttpIOHandler> httpIO = new HttpIO<HttpIOHandler>(httpTransaction); //httpIO.OnHttpAppException += ((msg) => //{ // //Log(msg); //}); //httpIO.OnHttpFatalException += ((msg) => //{ // //Log(msg); //}); List<Image> imgLst = await httpIO.ExecAsync<List<Image>>(request).ConfigureAwait(false); return imgLst; // JSON parsing without serialization... if (imgLst != null) { string md5 = ""; bool bReadNxt = false; JsonTextReader reader = new JsonTextReader(new StringReader(imgLst.ToString())); while (reader.Read()) { /* * TokenType "PropertyName" means its a JSON Key * Corresponding Value represents the key Name * TokenType for a JSON value is the data type like String, Boolean, Integer... * Corresponding Value is the value of the JSON Value */ if (reader.Value != null) { //Log(string.Format("Token: {0}, Value: {1}", reader.TokenType, reader.Value)); if (bReadNxt) { md5 = reader.Value.ToString(); bReadNxt = false; //Log(string.Format("VALUE: {0}", md5)); } if (reader.TokenType.ToString() == "PropertyName" && reader.Value.ToString() == "md5") bReadNxt = true; } else { //Log(string.Format("Token: {0}", reader.TokenType)); } } } }