Z
Z
zepars2015-07-08 15:07:33
JSON
zepars, 2015-07-08 15:07:33

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;

Throws an exception on the second line.
How to get a JSON response from the server containing the corresponding error?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Tsiren Naimanov, 2015-07-08
@ImmortalCAT

google Vk Api .Net
there is already written lib for everything
download and experiment

L
Larry Underwood, 2015-07-08
@Hydro

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 question

Ask a Question

731 491 924 answers to any question