Answer the question
In order to leave comments, you need to log in
C# WebException How to get JSON and not 401 error?
I send a GET request for authorization in VKontakte, I should receive a JSON response in response. If the authorization is successful, then I get an access_token and everything is fine. But, if the login / password is incorrect or a captcha is required, then instead of the corresponding JSON result (as everything happens if you do the same procedure through the browser manually), I get a WebException 401 (unauthorized), error status - ProtocolError. Here is a snippet of a GET request:
WebRequest req = WebRequest.Create(Url + "?" + Data);
WebResponse resp = req.GetResponse();
Stream stream = resp.GetResponseStream();
StreamReader sr = new StreamReader(stream);
string Out = sr.ReadToEnd();
sr.Close();
return Out;
Answer the question
In order to leave comments, you need to log in
google Vk Api .Net
there is already written lib for everything
download and experiment
401 is a failed authentication error.
And the appearance on the horizon of WebException is the expected behavior from the code.
Catch the WebException and tell the calling logic to re-authenticate with the correct username/password pair.
Look here for the resulting http codes , you will learn a lot of new things
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question