Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Make web requests with a proxy

Discussion in 'Multiplayer' started by Ramjii, Sep 4, 2018.

  1. Ramjii

    Ramjii

    Joined:
    Sep 4, 2018
    Posts:
    5
    We currently have customers who are using a proxy in their company and thus can't use our Unity app, since we need to make HTTP requests.

    Since most posts I see about this subject are rather old, I was wondering if there is now a way to use a proxy with the WWW class or anything in the Unity API? No, my app doesn't run in the webplayer

    If still not possible, I started to use HttpWebRequest but I get a TlsException “The authentication or decryption has failed”. I tried what's explained in this answer but it didn't solve my problem: https://stackoverflow.com/questions...h-the-authentication-or-decryption-has-failed

    Any idea ? Either for using Unity classes or C# classes?
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,637
    UnityWebRequest and WWW should use proxy configured on the operating system. You can also manually set proxy related HTTP headers (it is more convenient to do this in UnityWebRequest).
     
  3. Ramjii

    Ramjii

    Joined:
    Sep 4, 2018
    Posts:
    5
    Thanks for your answer!

    However, I'm sorry, I'm definitely not expert with proxies and HTTP requests. What headers am I supposed to use for that?
    I thought I was supposed to use the header Host to set the proxy address, but I get that exception:
    InvalidOperationException: Cannot override system-specified headers.

    Just realized I forgot to mention we're on Unity 2017.4.10
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,637
  5. Ramjii

    Ramjii

    Joined:
    Sep 4, 2018
    Posts:
    5
    Omg, UnityWebRequest indeed uses system proxy settings, the issue is not on our side...
    For our client's needs, the headers you gave should do the trick.

    Thanks a lot
     
  6. crol

    crol

    Joined:
    Aug 6, 2013
    Posts:
    30
  7. LimLan

    LimLan

    Joined:
    Dec 6, 2018
    Posts:
    4
    I did not want to change the environment variables, because a proxy is needed for only one request
    Header "Proxy-Authenticate" doesn't work.
    I was able to work around the problem with Mono.


    ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;
    HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(API_URL);
    string proxyHost = "162.22.22.22";
    int proxyPort = 3222;
    httpRequest.Proxy = new WebProxy(proxyHost, proxyPort);
    httpRequest.ContentType = "application/json";
    httpRequest.Method = "POST";

    using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
    {
    string json = "jsontext";

    streamWriter.Write(json);
    }

    var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
    var result = streamReader.ReadToEnd();
    }


    I use this code in the editor and rarely, so I did not make a separate thread


    public static bool MyRemoteCertificateValidationCallback(System.Object sender,
    X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
    bool isOk = true;
    // If there are errors in the certificate chain,
    // look at each error to determine the cause.
    if (sslPolicyErrors != SslPolicyErrors.None) {
    for (int i=0; i<chain.ChainStatus.Length; i++) {
    if (chain.ChainStatus[i].Status == X509ChainStatusFlags.RevocationStatusUnknown) {
    continue;
    }
    chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain;
    chain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
    chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan (0, 1, 0);
    chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
    bool chainIsValid = chain.Build ((X509Certificate2)certificate);
    if (!chainIsValid) {
    isOk = false;
    break;
    }
    }
    }
    return isOk;
    }
     
    Shda likes this.