Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Simple C# .NET 4.5 HTTPClient Request Using Basic Auth and Proxy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net;
namespace HTTP_Test
class program
static void Main()
Task t = new Task(HTTP_GET);
static async void HTTP_GET()
var TARGETURL = "";
HttpClientHandler handler = new HttpClientHandler()
Proxy = new WebProxy(""),
UseProxy = true,
Console.WriteLine("GET: + " + TARGETURL);
// ... Use HttpClient.
HttpClient client = new HttpClient(handler);
var byteArray = Encoding.ASCII.GetBytes("username:password1234");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
HttpResponseMessage response = await client.GetAsync(TARGETURL);
HttpContent content = response.Content;
// ... Check Status Code
Console.WriteLine("Response StatusCode: " + (int)response.StatusCode);
// ... Read the string.
string result = await content.ReadAsStringAsync();
// ... Display the result.
if (result != null &&
result.Length >= 50)
Console.WriteLine(result.Substring(0, 50) + "...");
Copy link

quarinteen commented Oct 28, 2019

Not sure what gives but for what ever reason creating the task and running it in main with t.start() does nothing. I don't get any errors it just stops when it hits the closing {} of Main(string[] args). The only thing different I added was user input on user name and password and I added an encryption method .

So I have the Main(string[] args then I have the following

` public async Task EncodePass(string password)

        byte[] bytes = Encoding.Unicode.GetBytes(password);
        byte[] inArray = HashAlgorithm.Create("SHA1").ComputeHash(bytes);
        return Convert.ToBase64String(inArray);

Then I have
`static async void HTTP_GET()


Copy link

Mskirvk commented Jan 29, 2020


Copy link

dogac00 commented Dec 16, 2020

Thank you very much.

Copy link

JustinaMary commented Dec 17, 2020

Thank you very much, you saved my day

Copy link

BerkAngay-terminal commented Mar 29, 2021

thankkk youuuu

Copy link

thomasroodnl commented Mar 31, 2021

It took me a while to find out how to encode the username and password into the authorization header, your code saved me a lot of trouble, thanks!

Copy link

kvadrakot commented Dec 14, 2021


Copy link

wicky05 commented Mar 16, 2022

Great Work?

Can you guide me

How to handle below json format using C# (with Authentication) - Post Method with Body(Raw)


"CustomerCode": "F101",
"CusReferenceNo": "3",
"StationName": "fsa",
"SalesmanId": "5",
"OrderStatus": "CLS",
"PostingDate": "2021-01-07",


"ItemCode": "100-0001-00002",
"Quantity": "25000.000000",



Appreciated, if you guide me and share sample code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment