Refit: How to make Refit pass through proxy? (Charlie)

Created on 26 Jan 2015  路  11Comments  路  Source: reactiveui/refit

Refit is not using the proxy settings of iOS, so Charlie can't capture the Api calls.

Is there a way to make Refit use iOS proxy settings or can I programmatically specify which proxy to use?

outdated

All 11 comments

You can in a preconfigured HttpClient when creating your Refit service:

var handler = new HttpClientHandler
{
    UseProxy = true,
    Proxy = new WebProxy("http://localhost:8888", false) // Or whatever Charles is set up as
};
var client = new HttpClient(new HttpClientHandler())
{
    BaseAddress = new Uri("https://api.example.com/")
};
var foo = RestService.For<IFooApi>(client);

However, if the proxy is already set as your system proxy, it will be picked up by default but just not using it for the requests because you're accessing a local address. To be able to debug with Fiddler on Windows, I have to map the API I'm debugging to a made up hostname and add a mapping in HOSTS.

If you control the app, a much better way to do this is simply to use http://runscope.com instead of dealing with proxy servers. It's free for simple developer use and is a great experience

@paulcbetts thanks, I'll try RunScope. Does it support .NET? I'm currently trying to use Crittercism but it just can't capture calls made with System.Net.Http.

@bennor thanks for your answer, it worked! Although I'm still trying to make Crittercism capture my calls.

@RTodorov It's a server-side solution so it supports every language

Oh I see, Runscope work as a gateway. I really don't want my production calls to use a 3rd party gateway. I may use it for debug though.

Crittercism SDK on the other hand automatically captures every http call made with AFNetwork / NSUrlConnection on iOS and java.net.URLConnection on Android. But it doesn't capture System.Net.Http* on .NET.

@RTodorov It's designed to do so actually, if you want to do that

Crittercism SDK on the other hand automatically captures every http call made with AFNetwork / NSUrlConnection on iOS and java.net.URLConnection on Android.

Sounds like if you use ModernHttpClient as your backing HttpMessageHandler, Crittercism will work then, no?

Yes, I'm trying it right now! Just bumped into your post about CFNetwork / NativeMessageHandler while searching for solutions.

It should definitely work but the app hangs when I make a call with Refit using my client:

var client = new HttpClient (new NativeMessageHandler ()) { BaseAddress = new Uri (APIUrl) };
var api = RestService.For (client);

api.TestCall(); // hangs here!!

If I initialize Refit using this:

var api = RestService.For (APIUrl);

it works fine (but doesn't use NativeMessageHandler).

Weird.... I'm still investigating but Refit doesn't print any errors..

Forget about it, ModernHttpClient solved my problem completely! I think there was an issue with RefitStubs not being regenerated. I cleaned the project, closed and reopened Xamarin and it worked!

That's awesome! Refit + ModernHttpClient + Crittercism working on both Android and iOS apps!

(Clearly Refit + ModernHttpClient is a great combination, once HttpClientHandler is much slower. Maybe you could mention it on Refit's README.md?)

May we close this issue? Refit + ModernHttpClient solves it.

@bennor FYI, about your statement:

However, if the proxy is already set as your system proxy, it will be picked up by default

That doesn't work, unless I really create and set a HttpClientHandler to HttpClient

Was this page helpful?
0 / 5 - 0 ratings