Skip to content

Instantly share code, notes, and snippets.

@darrelmiller
Created February 1, 2019 20:07
Show Gist options
  • Save darrelmiller/64e23b5805231e464e58d36bc7e7c977 to your computer and use it in GitHub Desktop.
Save darrelmiller/64e23b5805231e464e58d36bc7e7c977 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp10
{
class Program
{
static void Main(string[] args)
{
var httpClient = new HttpClient();
httpClient.SendAsync(
CreateRequest(HttpMethod.Get, new Uri("https://bing.com"))
.WithAccept("application/json"));
}
public static HttpRequestMessage CreateRequest(HttpMethod method, Uri requestUrl)
{
return new HttpRequestMessage()
{
Method = method,
RequestUri = requestUrl
};
}
}
public static class HttpRequestMessageExtensions {
public static HttpRequestMessage WithAccept(this HttpRequestMessage hrm, string mediaType)
{
hrm.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(mediaType));
return hrm;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment