public static string Upload(this Uri uri, NameValueCollection collection, UriContext context) { return uri.DownloadImpl(context, (client, u) => { var response = client.UploadValues(u, "POST", collection); return Encoding.UTF8.GetString(response); }); }
public static string Download(this Uri uri, UriContext context, Action<Exception> handler) { try { return uri.DownloadImpl(context, (client, u) => client.DownloadString(u)); } catch (Exception e) { if (handler != null) { handler(e); return string.Empty; } throw; } }
private static string DownloadImpl(this Uri uri, UriContext context, Func<WebClient, Uri, string> grab) { context = context ?? UriContext.Default; using (var client = new WebClient()) { client.Encoding = context.Encoding ?? Encoding.UTF8; if (context.IncludeDefaultHeaders) { // TODO: pass those via context client.Headers.Add(HttpRequestHeader.Accept, "text/html,application/xhtml+xml,application/xml"); client.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.5"); client.Headers.Add(HttpRequestHeader.Referer, "http://www.xboxachievements.com/search.php"); client.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded"); } // headers given with context should overwrite default headers client.Headers.Add(context.Headers ?? new NameValueCollection()); return grab(client, uri); } }
public static string Download(this Uri uri, UriContext context) { return Download(uri, context, exception => { }); // suppressing exception by default }