0

I'm completely at a loss here. The code itself usually works. Every once in a while I end up with this rather cryptic "Success" error instead, however. Rebooting seemed to help once, but now even that doesn't seem to work. I have no idea how to get more information on how to troubleshoot this. Url is always the same and I've verified the "BasicCreds" is returning the proper information (i.e. same as when it works). Any ideas how to troubleshoot further or what this "error" supposedly means?

In case it matters: I'm always behind a proxy and pinging a Jazz RTC server.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("Authorization", BasicCreds());
try
{
    using (WebResponse response = request.GetResponse()) // immediate exception for success (sometimes)
    {
CodeMonkey
  • 1,795
  • 3
  • 16
  • 46
  • 1
    First thing I could do here - use [`HttpClient`](https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netframework-4.8) instead of ancient `HttpWebRequest`. – aepot Oct 12 '20 at 22:26
  • @aepot, what would be the equivalent code? Authentication is failing with this: HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", BasicCreds()); var result = client.GetAsync(url); – CodeMonkey Oct 13 '20 at 16:01
  • 1) `var result = await client.GetAsync(url)` or better `using(var response = await client.GetAsync(url)) { ... }` - welcome to [Asynchronous Programming](https://learn.microsoft.com/en-us/dotnet/csharp/async) 2) guessing that creds must be additionally encoded with Base64. StackOverflow contains many examples for `HttpClient` including authentication but it can be specific to your server. – aepot Oct 13 '20 at 16:15
  • 3) What `BasicCreds()` returns? `"Basic "`? Then `new AuthenticationHeaderValue("Basic", BasicCreds());` and change method to return creds without prefix `Basic`. – aepot Oct 13 '20 at 16:18
  • @aepot, that seemed to do it! Not sure why HttpWebRequest was so finicky, but updating to HttpClient seemed to work. At least, for now... Thanks! Feel free to make it an answer and I'll accept it. – CodeMonkey Oct 14 '20 at 13:18
  • I didn't answer but suggested. You may answer your own question and post the actual code there. Then accept it. It's ok for StackOverflow. – aepot Oct 14 '20 at 13:20

1 Answers1

0

I have no clue what caused the "Success" error, but as suggested, I switched to using HttpClient instead and do not yet have any issues. Here's hoping it remains that way :-)

HttpClient client = new HttpClient();
// Modified BasicCreds() to return encoded "username:password" instead of encoded "Basic username:password"
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", BasicCreds());
try{
     using (HttpResponseMessage response = await client.GetAsync(url))
     {
CodeMonkey
  • 1,795
  • 3
  • 16
  • 46