public void Shorten(string longUrl, BitlyCallback<BitlyResponse> callback) { var scheme = DetermineScheme(longUrl); if (scheme != null && longUrl.IndexOf(scheme, StringComparison.CurrentCultureIgnoreCase) == -1) { longUrl = string.Format("{0}://{1}", scheme, longUrl); } if (!IsValidUrl(longUrl)) { callback(new BitlyResponse { Status = "INVALID_URI", StatusCode = 500 }); return; } if (!NetworkInterface.GetIsNetworkAvailable()) { callback(new BitlyResponse { StatusCode = 500, Status = "NO_INTERNET_CONNECTION" }); return; } var urlToShorten = UrlEncode(InsertForwardSlashBeforeQueryPath(longUrl)); var requestUri = new StringBuilder(BitlyApi.EndPoint); requestUri.AppendFormat("?apiKey={0}&", BitlyApi.ApiKey); requestUri.AppendFormat("login={0}&", BitlyApi.Login); requestUri.AppendFormat("longUrl={0}", urlToShorten); var request = (HttpWebRequest) WebRequest.Create(requestUri.ToString()); request.Method = "GET"; request.BeginGetResponse( ar => { var response = (HttpWebResponse)request.EndGetResponse(ar); using (var reader = new StreamReader(response.GetResponseStream())) { var json = JObject.Parse(reader.ReadToEnd()); var bitlyResponse = new BitlyResponse { StatusCode = json["status_code"].ToObject<int>(), Status = json["status_txt"].ToString() }; if (bitlyResponse.StatusCode == 200) { bitlyResponse.Data = JsonConvert.DeserializeObject<BitlyUrl>(json["data"].ToString()); } callback(bitlyResponse); } }, null); }
public void Shorten(string longUrl, BitlyCallback <BitlyResponse> callback) { var scheme = DetermineScheme(longUrl); if (scheme != null && longUrl.IndexOf(scheme, StringComparison.CurrentCultureIgnoreCase) == -1) { longUrl = string.Format("{0}://{1}", scheme, longUrl); } if (!IsValidUrl(longUrl)) { callback(new BitlyResponse { Status = "INVALID_URI", StatusCode = 500 }); return; } if (!NetworkInterface.GetIsNetworkAvailable()) { callback(new BitlyResponse { StatusCode = 500, Status = "NO_INTERNET_CONNECTION" }); return; } var urlToShorten = UrlEncode(InsertForwardSlashBeforeQueryPath(longUrl)); var requestUri = new StringBuilder(BitlyApi.EndPoint); requestUri.AppendFormat("?apiKey={0}&", BitlyApi.ApiKey); requestUri.AppendFormat("login={0}&", BitlyApi.Login); requestUri.AppendFormat("longUrl={0}", urlToShorten); var request = (HttpWebRequest)WebRequest.Create(requestUri.ToString()); request.Method = "GET"; request.BeginGetResponse( ar => { var response = (HttpWebResponse)request.EndGetResponse(ar); using (var reader = new StreamReader(response.GetResponseStream())) { var json = JObject.Parse(reader.ReadToEnd()); var bitlyResponse = new BitlyResponse { StatusCode = json["status_code"].ToObject <int>(), Status = json["status_txt"].ToString() }; if (bitlyResponse.StatusCode == 200) { bitlyResponse.Data = JsonConvert.DeserializeObject <BitlyUrl>(json["data"].ToString()); } callback(bitlyResponse); } }, null); }
private void BitlyShortenCallback(BitlyResponse bitlyUrl) { try { Action updateUi; if (bitlyUrl.StatusCode == 200) { updateUi = () => { Url = bitlyUrl.Data.Url; }; } else { updateUi = () => { Error = DescriptionAttribute.GetDescription(bitlyUrl.Error); }; } DispatcherHelper.CheckBeginInvokeOnUI(updateUi); } finally { DispatcherHelper.CheckBeginInvokeOnUI(() => { IsLoading = false; UrlFocused = true; }); } }