REST Connection using UnityWebRequest via proxy

Viewed 2031

I am trying to make a REST request to a server via proxy using UnityWebRequest. I have tried to use

Network.useProxy = true;

but this fails with the error

Cannot connect to destination host

Network.useProxy has been removed in Unity 2018.2.12. I am able to connect over Postman and curl. Can anybody confirm that UnityWebRequest does not support connection via proxy?

1 Answers

Even if it hasn't been deprecated, it wouldn't still work because Unity's Network.useProxy is used for the legacy networking system and the UnityWebRequest API is not part of that.

There is no support for proxy with UnityWebRequest and there is no plan to add support for this on Unity roadmap. Vote for its support here.

Your only workaround is to use one of the standard C# web request API such as HttpWebRequest and WebClient. They are supported in Unity. With HttpWebRequest, you can use proxy as below:

string proxyHost = "192.168.1.3";
int proxyPort = 8080;
string url = "http://YourUrl.com/blah";

HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Proxy = new WebProxy(proxyHost, proxyPort);

Since this is not UnityWebRequest, you have to do that in another Thread to prevent blocking your game main Thread.

Related