HttpRequest Android Problem

HttpRequest Android Problem
0

#1

Hi,
I’ve a problem with HttpRequest in Android.
I’m connecting to a mysql database through http, in my App.
I’m using POST and GET methods for login, get info, etc.

In iOS everything works fine.
However, in Android, with the same code that works in iOS, it doesn’t work.
When I try to get info this is the response result:

ResponseCode: 401
ResponseHeader: HTTP/1.1 401 Unauthorized \n Cache-Control: no-store
ResponseData:

Is a rare behavior because the code and the server is the same, and i repeat, in iOS works ok.
Maybe should I use a different header for send request? this is my header:

    vector<string> header;
    header.emplace_back("User-Agent: MyApp/1.0");
    header.emplace_back("Content-Type: application/x-www-form-urlencoded");
    httpRequest->setHeaders(header);

Also I read info about CookieStore in Android. But i’m not sure what’s the problem.

//cc android expert… @mars3142 :slight_smile:


#2

How to you authenticate to the mysql server?

Did you check the traffic, eg with CharlesProxy? This is the tool, I use to check my traffic and compare header/body in Android and iOS. To test my REST-Calls I use Postman. So I’m sure, that my calls will work.


#3

If you are using https you have problems with old Android devices after 30/jun/2018. Change to http and try.


#4

Yes, i’m using https and i’m testing in Android 6 (old).
Do you think that the problem is that? But if i don’t use https it would be very insecure. What’s the solution using https?


#5

I’m talking about Android 4.?. So your apps will have problem running in these old devices. I suggest you to change to http just to see if it works, if so, you found the problem and looks for a solution.

https://developer.android.com/reference/javax/net/ssl/SSLSocket


#6

@Rogerup Nope, I confirm that https isn’t the problem. I tested with http and I’ve the same problem.


#7

Testing with Postman, i detected a difference between the Headers.
In iOS I see Set-Cookie , and in Android, not. Maybe is a problem with php sessions in the server.

In cocos2d exist HttpCookie, maybe it can help me? anyway, i have no idea how to use that…


#8

I found the problem. I share here for (maybe) help to someone else.
The problem is with the Cookies in Http.

When I log in to the API, the http response header contains a new Cookie value:
Set-Cookie: value

I should to use that ‘value’ always that I would want to use GET requests.
For all GET requests, in the header, I need add Cookie: value
And now everything works fine.

// thanks @mars3142 for share Postman tool, it helped me to discover the problem :wink: