Search code examples
servicestackservicestack-text

ServiceStack HttpUtils + Proxy Server


I am using ServiceStack HttpUtils to connect to a third-party REST API. How do I pass in Proxy Server and Proxy Port when making requests?

Thanks rudrvij


Solution

  • HTTP Utils is a wrapper over .NET's HttpWebRequest so you can use the same functionality to specify a Proxy, e.g:

    url.GetJsonFromUrl(requestFilter: req.Proxy = new WebProxy("http://webproxy:80/"));
    

    Or set a Proxy globally with:

    WebProxy proxyObject = new WebProxy("http://webproxy:80/");  
    GlobalProxySelection.Select = proxyObject;  
    

    Or configure it in Web.config:

    <configuration>  
      <system.net>  
        <defaultProxy>  
          <proxy  
            usesystemdefault="true"  
            proxyaddress="http://192.168.1.10:3128"  
            bypassonlocal="true"  
          />  
          <bypasslist  
            <add address="[a-z]+\.contoso\.com" />  
          </bypasslist>  
        </defaultProxy>  
      </system.net>  
    </configuration>