2

Does anyone see what I'm doing wrong? Why can't I download a calendar file from outlook.com using HttpClient?

Whatever I try, Outlook will throw a http 302 error and redirect to an OWA error page saying that "my browser is not supported".

When using the web browser (Edge, Safari), there is no problem! It must be something I miss in my request.

As you can see in the code I think I've tried it all, added all the headers the browser do. Of course that should not be necessary but I'm desperate here.

var iCal_Url = "https://outlook.live.com/owa/calendar/00000000-0000-0000-0000-000000000000/c8f7ad62-8337-47f3-be26-652c182b58c2/cid-B505A4E6E6934159/calendar.ics";
using var http = new HttpClient(new HttpClientHandler
{
 AllowAutoRedirect = false,
 MaxAutomaticRedirections = 10
});
http.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0");
http.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
http.DefaultRequestHeaders.Add("upgrade-insecure-requests", "1");
http.DefaultRequestHeaders.Add("sec-ch-ua", "\"Not;A=Brand\";v=\"99\", \"Microsoft Edge\";v=\"139\", \"Chromium\";v=\"139\"");
http.DefaultRequestHeaders.Add("sec-ch-ua-mobile", "?0");
http.DefaultRequestHeaders.Add("sec-ch-ua-platform", "\"Windows\"");
http.DefaultRequestHeaders.Add("sec-ch-ua-platform-version", "\"15.0.0\"");
// TEST 1
//icsData = await response.Content.ReadAsStringAsync();
// TEST 2
//using (var s = await http.GetStreamAsync(iCal_Url))
//{
// using (var fs = new FileStream("c:\\temp\\data2.txt", FileMode.CreateNew))
// {
// await s.CopyToAsync(fs);
// }
//}
// TEST 3
var response = await http.GetAsync(iCal_Url);
var content = await response.Content.ReadAsByteArrayAsync();
await System.IO.File.WriteAllBytesAsync("c:\\temp\\data.txt", content);

The response just keeps returning:

StatusCode: 302, ReasonPhrase: '', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent ...

With the location /owa/auth/errorfe.aspx?httpCode=500&msg=....

Of course I've also tried to let the HttpClient follow the 302 and also built by custom forwarder and loop through the locations but it is only returning an error page.

Dmitry Streblechenko
67.1k4 gold badges56 silver badges84 bronze badges
asked Aug 30, 2025 at 9:26
11
  • 2
    You are missing an http header (or using the wrong header). Try making connection with Postman. Postman is robust and will automatically add HTTP header. Postman has a control panel that shows the Raw HTTP message and you can compare your request with the postman request. Then add missing headers to you c# code. Commented Aug 30, 2025 at 10:20
  • Read the notes and take the code you find here - as it is - and test it out Commented Aug 30, 2025 at 10:41
  • 2
    Instead of trying to hack Outlook.com, why not use the correct API (using the MS Graph SDK for C#) and authenticate properly? Outlook Web Access was the name given to the very first Web UI for Exchange back in the 00s - that was one of the first real AJAX applications. Then there was a SOAP API but even that is deprecated now. All Office 365 services use Microsoft Graph instead. If you really need to hack access (why?) use Playwright or Selenium to actually drive a browser Commented Aug 30, 2025 at 17:00
  • 2
    @jdweng It's essentially web-scraping (eg OP is not running any of the Javascript and just hoping enough of the page loads without it) so yes it is a hack. The front-end pages are not designed to be used by automated tools, the API is there for that. Commented Aug 30, 2025 at 21:08
  • 1
    Go to dev tools in browser, network tab. Then navigate to desired page and take find the request that got respond with desired data. Then right click it - there you should be able to copy it is cURL, powershell code etc. In my case, i would copy it as powershell, as in windows i can test it easily - so test it if it still works outside browser. Once confirmed, ask chat gpt (or yourself) to write the code snippet in C#. Simple as that Commented Aug 31, 2025 at 5:55

1 Answer 1

1

Go to dev tools in browser, network tab. Then navigate to desired page and find the request that got respond with desired data.

Then right click it - there you should be able to copy it is cURL, powershell code etc.

enter image description here

In my case, i would copy it as powershell, as in windows i can test it easily - so test it if it still works outside browser. Once confirmed, ask chat gpt (or yourself) to write the code snippet in C#.

answered Aug 31, 2025 at 10:17
Sign up to request clarification or add additional context in comments.

5 Comments

You can get the same using Postman Control Panel and getting the Raw HTTP. Like I said above the HTTP Header UserAgent was set wrong in this case.
Postman and Bruno did not work for me, they behave exactly the same as any low level client, you need to know and provide the correct useragent och accept which is the problem here. Thanx anyways.
Useragent is the type of browser being used. When useragent is wrong usually a bad request 400 is returned and not 302. Postman will not change the useragent. How did you find the error?
Awesome Michał. I did try to copy the same header values (among MANY others) as the Edge browser use but that did not do it so I kind of let it go coz I though it udner the hood added something else to the reqest. However, when I let the browser generate a curl and ps script it modified the useragent to a working one. Many thanks for the suggestion!
@jdweng yea I agree it is weird in this case to get a 302 to an error html-page, downloading ics-files is probably not meant for a browser anyways. But I guess people download the file using the browser and import it or whatever so maybe they want to give a nice looking error page (that shows "error 500" and nothing else :-D). The error code is sent to the errorpage in the query-pars. Anyways, the problem is solved, Edge helped me generate the working useragent. Thanks!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.