0

I am trying to pull REST data from an API but I need to handle the calls to the API with some server side solution. I have tried using the following code

try
{
    HttpClient client = new HttpClient();
    client.Timeout = TimeSpan.FromSeconds(60);

    var request = new HttpRequestMessage()
    {
        RequestUri = new Uri(string.Format("https://jsonodds.com/{0}{1}{2}", "api/odds/", "?source=", "3")),
        Method = HttpMethod.Get,
    };

    request.Headers.Add("JsonOdds-API-Key", "your key");

    HttpResponseMessage response = client.SendAsync(request).Result;

    if (response.IsSuccessStatusCode)
    {
        String.Format("Success");
    }
}
catch (Exception ex)
{ //log error }

I receive a 407() error. Any ideas or tips how to do this?

1
  • 5
    407 = "HTTP Error 407 Proxy authentication required" <= seems your credentials are not in order. Commented Apr 20, 2017 at 17:26

1 Answer 1

1

If you are going through a proxy server then you need to use a different constructor for HttpClient.

        _httpClient = new HttpClient(new HttpClientHandler
           {
               UseProxy = true,
               Proxy = new WebProxy
               {
                   Address = new Uri(proxyUrl),
                   BypassProxyOnLocal = false,
                   UseDefaultCredentials = true
               }
           })
           {
               BaseAddress = url
           };

Replace proxyUrl with your proxy address then replacing the credential with those that are valid for your proxy. This example uses the default credentials, but you can pass a NetworkCredential to the WebProxy.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.