Search Unity

Sending ssl in UnityWebRequest

Discussion in 'Scripting' started by gfellisch, Oct 9, 2019.

  1. gfellisch

    gfellisch

    Joined:
    Sep 2, 2019
    Posts:
    15
    I want to use UnityWebRequest to send a Form Data parameter "ssl" including an ssl string (see picture below).

    ssl in Form Data.png

    I already tried the two methods below. But it did not work.
    Sending a simple payload did reach the server however.
    Does formData change the header of UnityWebRequest? Or am I overlooking something else?

    Code (CSharp):
    1. IEnumerator Method(string url, string ssl){
    2.         List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
    3.         formData.Add(new MultipartFormDataSection("ssl=" + ssl);
    4.  
    5.         UnityWebRequest www;
    6.         www = UnityWebRequest.Post(url, formData);
    7.  
    8.         yield return www.SendWebRequest();
    9.  
    10.         if (www.isNetworkError || www.isHttpError)
    11.         {
    12.             Debug.Log(www.error);
    13.         }
    14.         else
    15.         {
    16.             userData = www.downloadHandler.text;
    17.         }
    18. }
    and

    Code (CSharp):
    1. IEnumerator Method(string url, string ssl){
    2.         List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
    3.  
    4.         formData.Add(new MultipartFormDataSection("ssl=" +
    5. ssl.Replace("+","%2B").Replace("=","%3D").Replace("/","%2F")));
    6.  
    7.         UnityWebRequest www;
    8.         www = UnityWebRequest.Post(url, formData);
    9.  
    10.         yield return www.SendWebRequest();
    11.  
    12.         if (www.isNetworkError || www.isHttpError)
    13.         {
    14.             Debug.Log(www.error);
    15.         }
    16.         else
    17.         {
    18.             userData = www.downloadHandler.text;
    19.         }
    20. }
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,279
    You are sending a raw section of multipart form.
    I bevieve what you want is a simple form with ssl filed in it?
    In such case you should use WWWForm.
    If you do indeed want a multipart form, then you probably should use MultipartFormDataSection contructor with name argument besides payload, rather than concatenating "ssl=" manually.
     
  3. gfellisch

    gfellisch

    Joined:
    Sep 2, 2019
    Posts:
    15
    Thank you. That's a good tip.
    Now I am using WWWForm like this:
    Code (CSharp):
    1. IEnumerator Method(string url, string ssl){
    2.         WWWForm form = new WWWForm();
    3.         form.AddField("ssl", ssl);
    4.  
    5.         UnityWebRequest www;
    6.         www = UnityWebRequest.Post(url, form);
    7.         yield return www.SendWebRequest();
    8.         if (www.isNetworkError || www.isHttpError)
    9.         {
    10.             Debug.Log(www.error);
    11.         }
    12.         else
    13.         {
    14.             userData = www.downloadHandler.text;
    15.         }
    16. }
    But I am still not able to login (changed to random data in the picture) and recieve a bad Request.
    Could it be connected to "__proto__"? I have no clue what that is.
    proto.png
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,279
    I have no clue what you are trying to achieve. I can only tell, how to transfer the data from client to server.
    If you know have an example request that succeeds, I can help to replicate that in Unity.
    But beyond that you are on your own. I have zero idea about that (server side?) code you are showing, nor I have any clue what the value of that ssl field is nor where you got it from, nor whether it is valid etc...
     
  5. gfellisch

    gfellisch

    Joined:
    Sep 2, 2019
    Posts:
    15
    I really apriciate your that you are trying to help me. :D
    An actual successful request I am trying to replicate is this:

    Request.png

    Does this help? Otherwise, thank you for your effort. :)

    The hex numbers inside the url are generated as well as the ssl. I already successfuly replicated that.
    So the last step I have to take is to send the generated ssl to the generated url.
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,279
    Have you tried using http debugging proxy like Fiddler or Charles to intercept and view the raw request? When you have the successful request, that's what you should do and compare.
    One item that catches my eye is the query in url. Maybe not related, but somewhat strange for a POST request.
     
    gfellisch likes this.
  7. gfellisch

    gfellisch

    Joined:
    Sep 2, 2019
    Posts:
    15
    Using Fiddler is a good tip. I never used it befor. But I still don't know what I am exactly looking for. The two raw requests seem pretty different to me:


    Unity:
    POST https://item.engineering/dqart/91bf16b6f0e8573e4eddf1a265b781e9403:5e62d6a669ba526d772f3c63ff7764da/u/l/?version=alpha4 HTTP/1.1
    Host: item.engineering
    User-Agent: UnityPlayer/2019.2.3f1 (UnityWebRequest/1.0, libcurl/7.52.0-DEV)
    Accept: */*
    Accept-Encoding: identity
    Content-Type: application/x-www-form-urlencoded
    X-Unity-Version: 2019.2.3f1
    Content-Length: 382

    ssl=OH%2bbbJrfJ6U1b0mYn9Z5MZTH4SIpIS%2btt9xpcZWzN7o8fcHK5GFCbFVogPaBzKwPkDdwgkSYE90BzjTVhR0O%2byy7uAZt3dlsKJ%2bTRKO8KP%2f2ICwEFUMe1kgmkErSP4BvLm8filqt%2bmSLwX51wz0r3rFj1lmJKNOp3168CcFx6yj7zrjmNDwubqyN28S2v%2bmVmGCIyu671%2b84QcYfkjJMQR1J%2b7ySiDFrAJLce3Z3UhQpYqESdPqDayB64R6%2fou58b20CPXEUyE4WI8mWKc%2bynj8g0VJV8a%2bVxkwIYQW6rrDdj59Q0z14Vn43TqtnhyWcIwd%2ffpkJjJPekC%2fiH%2fgZPQ%3d%3d



    Web browser:
    POST https://item.engineering/dqart/76385dd43bb2c8c74e3a37fd1b1f529d:0328c5e798876a06df5d6b869550241c/u/l/?version=alpha4 HTTP/1.1
    Host: item.engineering
    Connection: keep-alive
    Content-Length: 364
    Accept: */*
    Origin: https://item.engineering
    User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36
    Sec-Fetch-Mode: cors
    Content-Type: application/x-www-form-urlencoded; charset=UTF-8
    Sec-Fetch-Site: same-origin
    Referer: https://item.engineering/DEde/tools/engineeringtool/
    Accept-Encoding: gzip, deflate, br
    Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
    Cookie: _gcl_au=1.1.794253783.1567584723; _ga=GA1.2.1886642906.1567584716; _gac_UA-66605572-1=1.1567584731.Cj0KCQjwwb3rBRDrARIsALR3XeY9xQbwOWUUkVVRgcXa4EMIhL4US6HqI-NNqdmtPoeBylTBNbxO2Z8aAgF_EALw_wcB; _gac_UA-66605572-2=1.1567584716.Cj0KCQjwwb3rBRDrARIsALR3XeY9xQbwOWUUkVVRgcXa4EMIhL4US6HqI-NNqdmtPoeBylTBNbxO2Z8aAgF_EALw_wcB; _gid=GA1.2.1187402807.1571035634; _gat_UA-66605572-2=1; _gat_UA-66605572-39=1; _gat_UA-66605572-1=1

    ssl=DGbjiQ2RgWGhYRUWkfMNloXmg9doSXD6dyT4HAdwUTnNMSzGmsduWlkq%2Fldjts%2BR9hFxSf9nKF1PxhLcCVA289%2BX2VOd3CBRLu%2FCud2TYDVNFxRT9TcFa4ngfofpDehXMk4zoQucDjlkPhNEb1W3yDxkROn1AZV1OyqoNi1vIrWzPYORtRgTkGzojxbt1OyeKA06LVYfNjW2yok0fZvE339EudfHmzN01jkhCzugHnBKl2sfZwtRgK0LQD3WiHz2egvt60l6sHFgGXcts6Jc0ue2loWPcEJ2g0jn9W5x3T4sV4xG0LLot62a6VWLqx%2FaDMLP%2B9lCCUKaW9IcYZECDQ%3D%3D
     
  8. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,279
    Most differences are the headers, most of them I don't expect to make a difference, except one: Cookie.
    Does the site require to login/authenticate? Cookies are often used with that, I I see none in Unity.
    Also, both URL and ssl parameter are different (ssl differs not only in value, but also in length, perhaps something to look into?). Maybe they are supposed to, but in general you should be aiming to replicate things as close as possible.
     
    gfellisch likes this.
  9. gfellisch

    gfellisch

    Joined:
    Sep 2, 2019
    Posts:
    15
    Okay, I made it. :D
    The problem was the Content-Type. All I had to add was
    www.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");


    Thank you very much, Aurimas-Cernius!