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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

The name 'HttpUtility' does not exist in the current context.

Discussion in 'Scripting' started by zyonneo, Aug 22, 2019.

  1. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    I am getting this error when I opened a github repository in unity.When I searched stackoverflow I got the feedback to use
    Code (CSharp):
    1.  using System.Web;
    Even after using that it still shows error.In C# projects it might be useful how to use it in Unity.

    Code (CSharp):
    1. var queryString = HttpUtility.ParseQueryString(string.Empty); //Showing error "The name 'HttpUtility' does not exist in the current context"
     
    bekici likes this.
  2. alexddhuang

    alexddhuang

    Joined:
    Jul 30, 2019
    Posts:
    20
    Can you show all of your code?
     
  3. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    It was inside the below code
    Code (CSharp):
    1. public static IEnumerator MakeHandwritingRequest(byte[] bytes, Text textComponent)
    2.     {
    3.         var headers = new Dictionary<string, string>() {
    4.              {"Ocp-Apim-Subscription-Key", VISION_API_SUBSCRIPTION_KEY },
    5.              {"Content-Type","application/octet-stream"}
    6.         };
    7.         var queryString = HttpUtility.ParseQueryString(string.Empty);
    8.         queryString["mode"] = "Printed";
    9.         string uri = VISION_API_BASE_URL + "/vision/v1.0/recognizeText?" + queryString;
    10.         if ((bytes != null) && (bytes.Length > 0))
    11.         {
    12.             WWW www = new WWW(uri, bytes, headers);
    13.             yield return www;
    14.  
    15.             if (www.error != null)
    16.             {
    17.                 textComponent.text = "Error 1: " + www.error;
    18.             }
    19.             else
    20.             {
    21.  
    22.                 string operationLocation = www.responseHeaders["Operation-Location"];
    23.                 var headers2 = new Dictionary<string, string>() {
    24.                      {"Ocp-Apim-Subscription-Key", VISION_API_SUBSCRIPTION_KEY }
    25.                 };
    26.                 string statusRunning = "{\"status\":\"Running\"}";
    27.                 string status = "{\"status\":\"Running\"}";
    28.                 do
    29.                 {
    30.                     System.Threading.Thread.Sleep(500);
    31.                     WWW www2 = new WWW(operationLocation, null, headers2);
    32.                     yield return www2;
    33.  
    34.                     if (www2.error != null)
    35.                     {
    36.                         textComponent.text = "Error 2: " + www.error;
    37.                     }
    38.                     else
    39.                     {
    40.                         status = www2.text;
    41.                         textComponent.text = "Processing...";
    42.                     }
    43.                 } while (status == statusRunning);
    44.                 HandwritingAPIResults results = JsonUtility.FromJson<HandwritingAPIResults>(status);
    45.                 status = results.ToString();
    46.                 textComponent.text = status;
    47.                 print(status);
    48.  
    49.             }
    50.         }
    51.     }
     
  4. alexddhuang

    alexddhuang

    Joined:
    Jul 30, 2019
    Posts:
    20
    Which .Net version do you use?
     
  5. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    .net 4
     
  6. alexddhuang

    alexddhuang

    Joined:
    Jul 30, 2019
    Posts:
    20
    Very weird. I didn't meet this error. I have added
    using System.Web;
     
  7. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
  8. alexddhuang

    alexddhuang

    Joined:
    Jul 30, 2019
    Posts:
    20
    Well, I have cloned your project and built it, but I didn't meet any problem.
     
  9. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    :( is it supported inn MacOS? may be because of that?
     
  10. alexddhuang

    alexddhuang

    Joined:
    Jul 30, 2019
    Posts:
    20
    This is my configuration, and I am using Unity 2019.1.
     

    Attached Files:

  11. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386


    I am using 2019.2
     
  12. alexddhuang

    alexddhuang

    Joined:
    Jul 30, 2019
    Posts:
    20
    :DMaybe you can try to use a lower version.
     
    zyonneo likes this.
  13. bekici

    bekici

    Joined:
    Nov 1, 2018
    Posts:
    13
    I have faced with the same issue. Have you resolved it? @zyonneo
     
  14. Jnug

    Jnug

    Joined:
    Aug 30, 2019
    Posts:
    1
  15. DoPinG83

    DoPinG83

    Joined:
    Aug 26, 2013
    Posts:
    5
  16. avinash213

    avinash213

    Joined:
    Dec 4, 2018
    Posts:
    5
    In my case changing the API Compatibility Level From .NET 4.x to .NET Standard 2.0 fixed the problem.
     
  17. bekici

    bekici

    Joined:
    Nov 1, 2018
    Posts:
    13
    I'm already using .NET Standard 2.0 but no fixes. Still using Unity 2019.1.14f to avoid the problem.
     
  18. rabishan-maharjan

    rabishan-maharjan

    Joined:
    Jul 3, 2016
    Posts:
    18
    thanks. this works for me.
     
  19. dabdaddydarko

    dabdaddydarko

    Joined:
    Jul 17, 2018
    Posts:
    14
    okay but how do I add this csc.rsp file to my assets folder. do I simply create a txt file in notepad called csc.rsp, paste the 1 line of code and then drag and drop it into my assets folder?
     
  20. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    That's the first thing I would try. No idea if that's what he meant, but it's free to try!
     
  21. dabdaddydarko

    dabdaddydarko

    Joined:
    Jul 17, 2018
    Posts:
    14
    that didn't work so I guess im dumb
     
  22. dabdaddydarko

    dabdaddydarko

    Joined:
    Jul 17, 2018
    Posts:
    14
    I lied I guess it does work
     
  23. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,559
    I am facing this as well, trying to migrate from 2018.4 --> 2019.4. Is this a bug ? is it planned to be fixed?
     
  24. tonialatalo

    tonialatalo

    Joined:
    Apr 23, 2015
    Posts:
    60
    It seems like a feature - you can add the libraries you need like described in here above and in the docs at https://docs.unity3d.com/Manual/dot...5.2108058825.1597740329-1791289007.1508351871

    I got this with HttpUtility.ParseQueryString and added csp.rsp with -r:System.Web.dll
     
    EKG8g likes this.
  25. wykj-luke

    wykj-luke

    Joined:
    Feb 11, 2020
    Posts:
    6
    thanks a lot. it works
     
  26. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,136
    I had same issue on 2018 - 2019 upgrade, though System.Net [WebUtility] has some similar functions to use instead like maybe WebUtility.HtmlDecode instead of HttpUtility.HtmlDecode
     
  27. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,539
    To me it seems to be a total waste to include System.Web.dll just for dealing with URL encoding. Especially in the case of the OP. Using this:

    Code (CSharp):
    1. var queryString = HttpUtility.ParseQueryString(string.Empty);
    2. queryString["mode"] = "Printed";
    3. string uri = VISION_API_BASE_URL + "/vision/v1.0/recognizeText?" + queryString;
    just do do this:

    Code (CSharp):
    1. string uri = VISION_API_BASE_URL + "/vision/v1.0/recognizeText?mode=Printed";
    seems to just adds unnecessary complexity. I do understand that a proper url encoder might be useful when you're dealing with user provided text so special characters gets escaped properly. However in this case I really don't get it.
     
  28. tonialatalo

    tonialatalo

    Joined:
    Apr 23, 2015
    Posts:
    60
    Fair enough - I'm not using it for URL encoding, but for parsing query params.
     
  29. btschumy

    btschumy

    Joined:
    Jul 31, 2019
    Posts:
    91
    I'm running into this problem as well. Works fine on macOS where I originally wrote the code. However when trying to build on Windows I get the "not found" error. Tried both .NET 4.x and .NET Standard 2.0

    ETA: OK, I got finally it working with the csc.rsp solution. It didn't initially re-read the file.

    ETA (again): I take it back. Although the project builds and runs in the Editor, when I try to build a UWP all, it fails to find the HttpUtility class.
     
    Last edited: May 7, 2021
    holo-krzysztof and tonialatalo like this.
  30. DeathPro

    DeathPro

    Joined:
    Jul 28, 2018
    Posts:
    71