Search Unity

Alternative for httpWebRequest HttpWebResponse

Discussion in 'Editor & General Support' started by Sprynxky, Jul 2, 2010.

  1. Sprynxky

    Sprynxky

    Joined:
    Jul 2, 2010
    Posts:
    2
    Is it possible to make direct http requests instead of using the asynchronous WWW class?

    Our entire application uses the HttpWebRequest and HttpWebResponse classes at the moment for all database calls (we use an intermediate php layer) like this :

    Code (csharp):
    1. HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    2. HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    Everything works fine in the editor and in the stand alone player, but the web player gives a NotSupported exception. Using the WWW class is not really an option, as changing all the database calls to work with callbacks would take far too much time and we often need the responses before resuming the code after a database call.

    Our application is intended for the webplayer, so it's pretty important that we find a solution for this.
     
  2. shawn

    shawn

    Unity Technologies

    Joined:
    Aug 4, 2007
    Posts:
    552
    I'm not understanding why using the WWW class should make any difference for your database calls.

    Isn't this effectively the same thing?

    Code (csharp):
    1. var www : WWW = new WWW(url);
    2. yield www;
    3.  
    Maybe a better question, what are you using from a HttpWebResponse that a WWW doesn't give you?
     
  3. Sprynxky

    Sprynxky

    Joined:
    Jul 2, 2010
    Posts:
    2
    Well, we're doing our DB calls from within C# classes, where u can't use 'yield return www;'.

    We tried making a gameobject with a script (that we call from within the C# code). We have a function in that script that makes the actual WWW object, but if we yield it that function returns immediately so we don't have the answer in our C# code.

    We also tried yielding combinations and startcoroutines etc, but we can't make it that the first function doesn't return without the result.

    While loops like
    while(!www.isDone) { wait 1 sec; }
    also make the web player crash.

    So in short we want this :

    C# code :
    Code (csharp):
    1. private string SendQueryToPHPScript(string query)
    2. {
    3. string url = ... + query;
    4. m_DBMySQLThroughPHPScript.GetResponse(url);
    5. string result = m_DBMySQLThroughPHPScript.m_www.data;
    6. }
    The DBMySQLThroughPHP script :
    Code (csharp):
    1.  
    2. public WWW m_www = null;
    3.  
    4. public IEnumerator GetResponse(string url)
    5. {
    6. m_www = new WWW(url);
    7. yield return www;
    8. }
    9.  
    But this implementation doesn't work because the GetResponse function returns before the WWW has finished downloading. So we need some implementation of the GetResponse function that doesn't return before it's done.
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    If you rewrite the code that will work fine.

    Just pass a delegate to GetResponse that processes the download once down (-> is called after the yield return www; line and optimally just passes through the WWW object).


    HTTP requests are pretty restricted in the webplayer. for example all of them that access system.environment in any way (like all https related ones for sure, potentially even more never tried them outside that part) will not work etc.
     
  5. Benglin

    Benglin

    Joined:
    Jul 21, 2012
    Posts:
    3
    For one, it does not give us PUT/DELETE http verb :)

    The game we are working on for three month just came to a stop because we are unable to remove a message from Azure Queue Storage as it requires DELETE verb (Queue REST API documentation). We could have used web roles for APIs but that will severely limit the scalability of the service :(

    I'm not sure if I'm allowed to "advertise" here, but I'd recommend people needing this functionality to cast your vote with the following link :)

    Vote for PUT and DELETE http verbs in WWW class
     
  6. liju_techtreeit

    liju_techtreeit

    Joined:
    Oct 12, 2016
    Posts:
    1
    I am using WWWForm for HTTP GET and POST. In My Game URL credentials are added, and data GET and POST as Json format. I can get the details using credentials on Browser, But I cannot access details using WWWForm. First I was facing 401 Unauthorised problem. Then I added header like

    Dictionary<string, string> headers = form.headers;
    headers["content-type"] = "application/json";
    headers["Authorization"] = "Basic " + System.Convert.ToBase64String(Encoding.Default.GetBytes(username+":"+password));

    After that i don't have getting 401 Unauthorised problem . Know I have getting 400 Bad RequestHere is my Code.

    IEnumerator ConnectToServer2(string username,string password)
    {

    WWWForm form = new WWWForm();

    Dictionary<string, string> headers = form.headers;
    headers["content-type"] = "application/json";
    headers["Authorization"] = "Basic " + System.Convert.ToBase64String(Encoding.Default.GetBytes(username+":"+password));

    // form.AddField( "Book_Name", "Night%20at%20call%20centre" );
    form.AddField( "Book_Name", "Night at call centre" );
    form.AddField( "Book_Part", "First" );
    form.AddField( "Sequence", "1" );
    // form.AddField( "username", username );
    // form.AddField( "password", password );

    byte[] rawData = form.data;



    // Post a request to an URL with our custom headers
    WWW www = new WWW(bookViewUrl, rawData, headers);
    yield return www;

    if (www.error != null) {
    Debug.Log (www.error);
    } else {
    Debug.Log (www.text);
    }
    //.. process results from WWW request here...
    }

    Thank you in advance.
     
  7. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    Switch to UnityWebRequest, it supports PUT as well as custom HTTP verbs.
     
  8. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    Why are you using raw data here instead of just passing WWWForm to WWW constructor?