- Home /
Unity HTTP Post WebRquest not working on some Android Devices
I am having an issue with making post requests on some Android devices,
Here is my enumerator for web request
[SerializeField] private Text _statusText; //for debugin
[SerializeField] private Text _donloadHandlerText; //for debugin
public IEnumerator GetEvent(string url, string bodyJsonString, System.Action callback)
{
print(bodyJsonString);
var request = new UnityWebRequest(url, "POST");
byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJsonString);
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
request.timeout = 10; //Setting the timeout request;
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
Debug.Log("Network Error: " + request.responseCode + request.error);
_statusText.text = "Event Call Status: " + request.responseCode + " " + request.error;//for debugin
}
else
{
Debug.Log("Status Code: " + request.responseCode);
_statusText.text = "Event Call Status: " + request.responseCode;//for debugin
//getting the body from call
Debug.Log(request.downloadHandler.text);
_donloadHandlerText.text = "Download Handler: " + request.downloadHandler.text;
var data = request.downloadHandler.text;
EventCallBack eventCallBack = EventCallBack.CreateFromJSON(data);
callback(eventCallBack);
}
}
This code works perfectly fine on Unity Editor, iOS devices. However, here is the interesting part on Andriod devices the post request works fine on wifi network. But if the user switches to cellular mobile network on some Android phone I am not able to make any post-call.
I have checked to see if the application does have access to cellular mobile network
if (Application.internetReachability == NetworkReachability.NotReachable)
{
//Change the Text
_networkStatus.text = "NetWork Status: " + "Not Reachable.";
}
//Check if the device can reach the internet via a carrier data network
else if (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)
{
_networkStatus.text = "NetWork Status: " + "Reachable via carrier data network.";
}
//Check if the device can reach the internet via a LAN
else if (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork)
{
_networkStatus.text = "NetWork Status: " + "Reachable via Local Area Network.";
}
and turned out the application does have access to the cellular mobile network. I also wanted to see what happen to post-call on Andriod devices which are affected by this problem.
I get the status call 200 which means the call success but in the body, I get a strange html fragment and the most interesting part is that I don’t actually receive any call in the backend but somehow I get responseCode 200 from the webRequest.
<!DOCTYPE html>
<html lang="en"?
<head>
<meta charest="utf-8">
Anyone has any idea that can help, please let me know. I have tried everything to find out what is happening but no look so far.
Note: I am also aware that Andiond on higher API(28+) level block http call by default I have added <application android:usesCleartextTraffic="true>
line to my Andiodmanifest. I don't think the problem is from the http call being blocked cause it works using WIFI on every Android, whatever causing this problem is related to Andriod cellular mobile network.
Your answer
Follow this Question
Related Questions
How to use the new IMultipartFormSection in a UnityWebRequest.Post 2 Answers
Try catch alternative for HTTP request 1 Answer
multipart/form-data Does WWW Class send the trailing boundary? 1 Answer
Pass Headers and Arguments in UnityWebRequest POST Method Object 1 Answer
UnityWebRequest: Out of memory 0 Answers