WebClient GET Request Fails With 401 Unauthorized
up vote
0
down vote
favorite
I am trying to make a GET api request with the below C# code but it fails with
System.Net.WebException: The remote server returned an error: (401) Unauthorized.
at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
at System.Net.WebClient.DownloadString(Uri address)
at System.Net.WebClient.DownloadString(String address)
at Rextester.Program.callAPI()
at Rextester.Program.Main(String args)
Note that the same API request works through postman.
private static void callAPI()
{
WebClient client = new WebClient();
client.UseDefaultCredentials = false;
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
client.Headers.Add("Authorization", "Basic " + Base64Encode("<id:password>"));
client.Headers.Add("Content-Type", "application/json");
client.Headers.Add("Environment-Id", "325");
client.QueryString.Add("query", "%7B%7D");
string reply = client.DownloadString("https://<server-api-url>");
Console.WriteLine(reply);
}
private static string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
var encodedS = System.Convert.ToBase64String(plainTextBytes);
Console.WriteLine(encodedS);
return encodedS;
}
One more strange fact is that I see the exception printed before the encoded string. It should I think the other way round. The encoded string should be printed before since the method call happens before making the api call.
c# api webclient unauthorized
|
show 4 more comments
up vote
0
down vote
favorite
I am trying to make a GET api request with the below C# code but it fails with
System.Net.WebException: The remote server returned an error: (401) Unauthorized.
at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
at System.Net.WebClient.DownloadString(Uri address)
at System.Net.WebClient.DownloadString(String address)
at Rextester.Program.callAPI()
at Rextester.Program.Main(String args)
Note that the same API request works through postman.
private static void callAPI()
{
WebClient client = new WebClient();
client.UseDefaultCredentials = false;
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
client.Headers.Add("Authorization", "Basic " + Base64Encode("<id:password>"));
client.Headers.Add("Content-Type", "application/json");
client.Headers.Add("Environment-Id", "325");
client.QueryString.Add("query", "%7B%7D");
string reply = client.DownloadString("https://<server-api-url>");
Console.WriteLine(reply);
}
private static string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
var encodedS = System.Convert.ToBase64String(plainTextBytes);
Console.WriteLine(encodedS);
return encodedS;
}
One more strange fact is that I see the exception printed before the encoded string. It should I think the other way round. The encoded string should be printed before since the method call happens before making the api call.
c# api webclient unauthorized
Where's the API request that works with Postman? If you use a debugging proxy like Fiddler to see what's actually going on I'll bet you'll find the requests are not the same.
– Panagiotis Kanavos
Nov 19 at 8:14
What does"<id:password>"
represent? In Basic authentication the string should beusername:password
, eg$"{username}:{password}"
orusername + ":" + password
orString.Format("{0}:{1}", username,password)
– Panagiotis Kanavos
Nov 19 at 8:20
I have added dummy values for not exposing production environment details. You are right about the format of the authentication string. It is in the format "username:password".
– Andy Dufresne
Nov 19 at 8:25
You are forcing people to guess right now. Are you using<>
or not? If yes, that's a mistake. If not, there's nothing else that can help understand what's wrong. What should the request look like? What did the Postman request look like in Fiddler? What did the WebClient look like? If the authentication headers are different you'll know theBase64Encode
methods is wrong.If there's another difference, what is it?
– Panagiotis Kanavos
Nov 19 at 8:30
Thanks for pointing me in the right direction. Through fiddler I understood that the api call is actually getting redirected (with http status code 307) and on the second request the authorization header is not being passed. How do I enable redirects in C#?
– Andy Dufresne
Nov 19 at 9:06
|
show 4 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to make a GET api request with the below C# code but it fails with
System.Net.WebException: The remote server returned an error: (401) Unauthorized.
at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
at System.Net.WebClient.DownloadString(Uri address)
at System.Net.WebClient.DownloadString(String address)
at Rextester.Program.callAPI()
at Rextester.Program.Main(String args)
Note that the same API request works through postman.
private static void callAPI()
{
WebClient client = new WebClient();
client.UseDefaultCredentials = false;
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
client.Headers.Add("Authorization", "Basic " + Base64Encode("<id:password>"));
client.Headers.Add("Content-Type", "application/json");
client.Headers.Add("Environment-Id", "325");
client.QueryString.Add("query", "%7B%7D");
string reply = client.DownloadString("https://<server-api-url>");
Console.WriteLine(reply);
}
private static string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
var encodedS = System.Convert.ToBase64String(plainTextBytes);
Console.WriteLine(encodedS);
return encodedS;
}
One more strange fact is that I see the exception printed before the encoded string. It should I think the other way round. The encoded string should be printed before since the method call happens before making the api call.
c# api webclient unauthorized
I am trying to make a GET api request with the below C# code but it fails with
System.Net.WebException: The remote server returned an error: (401) Unauthorized.
at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
at System.Net.WebClient.DownloadString(Uri address)
at System.Net.WebClient.DownloadString(String address)
at Rextester.Program.callAPI()
at Rextester.Program.Main(String args)
Note that the same API request works through postman.
private static void callAPI()
{
WebClient client = new WebClient();
client.UseDefaultCredentials = false;
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
client.Headers.Add("Authorization", "Basic " + Base64Encode("<id:password>"));
client.Headers.Add("Content-Type", "application/json");
client.Headers.Add("Environment-Id", "325");
client.QueryString.Add("query", "%7B%7D");
string reply = client.DownloadString("https://<server-api-url>");
Console.WriteLine(reply);
}
private static string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
var encodedS = System.Convert.ToBase64String(plainTextBytes);
Console.WriteLine(encodedS);
return encodedS;
}
One more strange fact is that I see the exception printed before the encoded string. It should I think the other way round. The encoded string should be printed before since the method call happens before making the api call.
c# api webclient unauthorized
c# api webclient unauthorized
edited Nov 19 at 10:05
Ashish Kamble
629419
629419
asked Nov 19 at 8:10
Andy Dufresne
3,51362964
3,51362964
Where's the API request that works with Postman? If you use a debugging proxy like Fiddler to see what's actually going on I'll bet you'll find the requests are not the same.
– Panagiotis Kanavos
Nov 19 at 8:14
What does"<id:password>"
represent? In Basic authentication the string should beusername:password
, eg$"{username}:{password}"
orusername + ":" + password
orString.Format("{0}:{1}", username,password)
– Panagiotis Kanavos
Nov 19 at 8:20
I have added dummy values for not exposing production environment details. You are right about the format of the authentication string. It is in the format "username:password".
– Andy Dufresne
Nov 19 at 8:25
You are forcing people to guess right now. Are you using<>
or not? If yes, that's a mistake. If not, there's nothing else that can help understand what's wrong. What should the request look like? What did the Postman request look like in Fiddler? What did the WebClient look like? If the authentication headers are different you'll know theBase64Encode
methods is wrong.If there's another difference, what is it?
– Panagiotis Kanavos
Nov 19 at 8:30
Thanks for pointing me in the right direction. Through fiddler I understood that the api call is actually getting redirected (with http status code 307) and on the second request the authorization header is not being passed. How do I enable redirects in C#?
– Andy Dufresne
Nov 19 at 9:06
|
show 4 more comments
Where's the API request that works with Postman? If you use a debugging proxy like Fiddler to see what's actually going on I'll bet you'll find the requests are not the same.
– Panagiotis Kanavos
Nov 19 at 8:14
What does"<id:password>"
represent? In Basic authentication the string should beusername:password
, eg$"{username}:{password}"
orusername + ":" + password
orString.Format("{0}:{1}", username,password)
– Panagiotis Kanavos
Nov 19 at 8:20
I have added dummy values for not exposing production environment details. You are right about the format of the authentication string. It is in the format "username:password".
– Andy Dufresne
Nov 19 at 8:25
You are forcing people to guess right now. Are you using<>
or not? If yes, that's a mistake. If not, there's nothing else that can help understand what's wrong. What should the request look like? What did the Postman request look like in Fiddler? What did the WebClient look like? If the authentication headers are different you'll know theBase64Encode
methods is wrong.If there's another difference, what is it?
– Panagiotis Kanavos
Nov 19 at 8:30
Thanks for pointing me in the right direction. Through fiddler I understood that the api call is actually getting redirected (with http status code 307) and on the second request the authorization header is not being passed. How do I enable redirects in C#?
– Andy Dufresne
Nov 19 at 9:06
Where's the API request that works with Postman? If you use a debugging proxy like Fiddler to see what's actually going on I'll bet you'll find the requests are not the same.
– Panagiotis Kanavos
Nov 19 at 8:14
Where's the API request that works with Postman? If you use a debugging proxy like Fiddler to see what's actually going on I'll bet you'll find the requests are not the same.
– Panagiotis Kanavos
Nov 19 at 8:14
What does
"<id:password>"
represent? In Basic authentication the string should be username:password
, eg $"{username}:{password}"
or username + ":" + password
or String.Format("{0}:{1}", username,password)
– Panagiotis Kanavos
Nov 19 at 8:20
What does
"<id:password>"
represent? In Basic authentication the string should be username:password
, eg $"{username}:{password}"
or username + ":" + password
or String.Format("{0}:{1}", username,password)
– Panagiotis Kanavos
Nov 19 at 8:20
I have added dummy values for not exposing production environment details. You are right about the format of the authentication string. It is in the format "username:password".
– Andy Dufresne
Nov 19 at 8:25
I have added dummy values for not exposing production environment details. You are right about the format of the authentication string. It is in the format "username:password".
– Andy Dufresne
Nov 19 at 8:25
You are forcing people to guess right now. Are you using
<>
or not? If yes, that's a mistake. If not, there's nothing else that can help understand what's wrong. What should the request look like? What did the Postman request look like in Fiddler? What did the WebClient look like? If the authentication headers are different you'll know the Base64Encode
methods is wrong.If there's another difference, what is it?– Panagiotis Kanavos
Nov 19 at 8:30
You are forcing people to guess right now. Are you using
<>
or not? If yes, that's a mistake. If not, there's nothing else that can help understand what's wrong. What should the request look like? What did the Postman request look like in Fiddler? What did the WebClient look like? If the authentication headers are different you'll know the Base64Encode
methods is wrong.If there's another difference, what is it?– Panagiotis Kanavos
Nov 19 at 8:30
Thanks for pointing me in the right direction. Through fiddler I understood that the api call is actually getting redirected (with http status code 307) and on the second request the authorization header is not being passed. How do I enable redirects in C#?
– Andy Dufresne
Nov 19 at 9:06
Thanks for pointing me in the right direction. Through fiddler I understood that the api call is actually getting redirected (with http status code 307) and on the second request the authorization header is not being passed. How do I enable redirects in C#?
– Andy Dufresne
Nov 19 at 9:06
|
show 4 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
After getting some guidance from @Panagiotis Kanavos I traced the api calls from Fiddler and understood that the api call was getting redirected (http status code - 307). The C# client is by default configured to not pass the authorization header on a redirect. Hence I got a 401 Unauthorized error.
This was fixed by adding a check for 401 status code and resending the api request with the authorization header.
Thanks @Panagiotis Kanavos for the help
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
After getting some guidance from @Panagiotis Kanavos I traced the api calls from Fiddler and understood that the api call was getting redirected (http status code - 307). The C# client is by default configured to not pass the authorization header on a redirect. Hence I got a 401 Unauthorized error.
This was fixed by adding a check for 401 status code and resending the api request with the authorization header.
Thanks @Panagiotis Kanavos for the help
add a comment |
up vote
0
down vote
accepted
After getting some guidance from @Panagiotis Kanavos I traced the api calls from Fiddler and understood that the api call was getting redirected (http status code - 307). The C# client is by default configured to not pass the authorization header on a redirect. Hence I got a 401 Unauthorized error.
This was fixed by adding a check for 401 status code and resending the api request with the authorization header.
Thanks @Panagiotis Kanavos for the help
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
After getting some guidance from @Panagiotis Kanavos I traced the api calls from Fiddler and understood that the api call was getting redirected (http status code - 307). The C# client is by default configured to not pass the authorization header on a redirect. Hence I got a 401 Unauthorized error.
This was fixed by adding a check for 401 status code and resending the api request with the authorization header.
Thanks @Panagiotis Kanavos for the help
After getting some guidance from @Panagiotis Kanavos I traced the api calls from Fiddler and understood that the api call was getting redirected (http status code - 307). The C# client is by default configured to not pass the authorization header on a redirect. Hence I got a 401 Unauthorized error.
This was fixed by adding a check for 401 status code and resending the api request with the authorization header.
Thanks @Panagiotis Kanavos for the help
answered Nov 22 at 3:49
Andy Dufresne
3,51362964
3,51362964
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370591%2fwebclient-get-request-fails-with-401-unauthorized%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Where's the API request that works with Postman? If you use a debugging proxy like Fiddler to see what's actually going on I'll bet you'll find the requests are not the same.
– Panagiotis Kanavos
Nov 19 at 8:14
What does
"<id:password>"
represent? In Basic authentication the string should beusername:password
, eg$"{username}:{password}"
orusername + ":" + password
orString.Format("{0}:{1}", username,password)
– Panagiotis Kanavos
Nov 19 at 8:20
I have added dummy values for not exposing production environment details. You are right about the format of the authentication string. It is in the format "username:password".
– Andy Dufresne
Nov 19 at 8:25
You are forcing people to guess right now. Are you using
<>
or not? If yes, that's a mistake. If not, there's nothing else that can help understand what's wrong. What should the request look like? What did the Postman request look like in Fiddler? What did the WebClient look like? If the authentication headers are different you'll know theBase64Encode
methods is wrong.If there's another difference, what is it?– Panagiotis Kanavos
Nov 19 at 8:30
Thanks for pointing me in the right direction. Through fiddler I understood that the api call is actually getting redirected (with http status code 307) and on the second request the authorization header is not being passed. How do I enable redirects in C#?
– Andy Dufresne
Nov 19 at 9:06