Search Unity

Why can't get responseHeaders by WWW in ios ?

Discussion in 'iOS and tvOS' started by tuandq77, Apr 26, 2016.

  1. tuandq77

    tuandq77

    Joined:
    Nov 18, 2015
    Posts:
    6
    Hi everyone !
    I need get response code from my request. My code work in Unity. But when i build to Xcode. It can't work.
    Pls help me fix it :(
    Thank you so much

    Code (CSharp):
    1. public  int getResponseCode(WWW request) {
    2.         int ret = 0;
    3.         if (request.responseHeaders == null) {
    4.             Debug.LogError("no response headers.");
    5.         }
    6.         else {
    7.             if (!request.responseHeaders.ContainsKey("STATUS")) {
    8.                 Debug.LogError("response headers has no STATUS.");
    9.             }
    10.             else {
    11.                 ret = parseResponseCode(request.responseHeaders["STATUS"]);
    12.             }
    13.         }
    14.         Debug.Log ("RESPONSE CODE : " +ret);
    15.         return ret;
    16.     }
    17.    
    18.     public  int parseResponseCode(string statusLine) {
    19.         int ret = 0;
    20.        
    21.         string[] components = statusLine.Split(' ');
    22.         if (components.Length < 3) {
    23.             Debug.LogError("invalid response status: " + statusLine);
    24.         }
    25.         else {
    26.             if (!int.TryParse(components[1], out ret)) {
    27.                 Debug.LogError("invalid response code: " + components[1]);
    28.             }
    29.         }
    30.        
    31.         return ret;
    32.     }
    Code (CSharp):
    1. public IEnumerator GetEndLinks(string url_redirect)
    2.     {
    3.             WWW request = new WWW (url_redirect);
    4.             yield return request;
    5.  
    6.          int CODE = getResponseCode (request);   //=> Not work when build to xcode and run with iphone
    7.              Debug.Log("v-------------new--------------v");
    8.             foreach(var cKey in request.responseHeaders.Keys)
    9.             {
    10.              Debug.Log(cKey + " : " + request.responseHeaders[cKey]);
    11.            }
    12.  
    13.            if (CODE == 301 || CODE == 302 || CODE == 303 || CODE == 304 || CODE == 305 || CODE == 306 || CODE == 307)
    14.                {
    15.                   string redirection = request.responseHeaders ["LOCATION"];
    16.                    Debug.Log (redirection);
    17.                    //Open redirect links
    18.                    Application.OpenURL (redirection);
    19.                }
    20.     }