سؤال

Here is a simplified version of the API controller I created.

public class SampleController : ApiController  
{
    [System.Web.Http.HttpGet]
    public string Test(string url)
    {
        try
        {
            using (WebClient webClient = new WebClientEx())
            {
                return webClient.DownloadString(url);
            }
        }
        catch (Exception ex)
        {
            return string.Empty;
        }
    }

Given a perfectly valid url this will throw a WebException that says "The remote name could not be resolved".

If I execute the same method within LinqPad, using the same url, it works. I've tried WebClient and WebRequest with the same results.

To be clear, this isn't a routing issue as I am able to hit and step through the code either way and the url involved is not part of this application.

هل كانت مفيدة؟

المحلول

I figured it out. I was initially thrown off because it appeared to work in a regular Controller but not in an ApiController. After I failed to reproduce my earlier "success", I finally figured out that I needed to use the system proxy.

webClient.Proxy = WebRequest.GetSystemWebProxy();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top