Tobias Lindberg
Tobias Lindberg

Reputation: 141

Get html from Webclient even then Webclient throws exception

I'm working against an web API and then i authenticate myself to the webb API and provide the wrong username or password the site returns "The remote server returned an error: (401) Unauthorized." and that's fine. But the site also return detailed information as JSON but I can't find out how to access this information then I'm getting a 401 exception.

Do anybody have a clue? Here is the code I'm using:

private string Post(string data, string URI)
    {
        string response = string.Empty;
        using (var wc = new WebClient())
        {
            wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            try
            {
                response = wc.UploadString(URI, data);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

        }
        return response;
    }

Thanks

Upvotes: 0

Views: 294

Answers (1)

weismat
weismat

Reputation: 7411

You should receive a WebException.
The WebReception has a

Response

property, which should contain what you are looking for. You may check all other properties as well.

Upvotes: 2

Related Questions