I've read dozen of posts on this subject, most of them rely on deprecated Android APIs and I finally tried to use this but without success:
So I get the csrftoken like that:
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setUseCaches(false);
urlConnection.setRequestMethod("GET");
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
String COOKIES_HEADER = "Set-Cookie";
site.setCookieManager(new java.net.CookieManager());
Map<String, List<String>> headerFields = urlConnection.getHeaderFields();
List<String> cookiesHeader = headerFields.get(COOKIES_HEADER);
if(cookiesHeader != null)
{
for (String cookie : cookiesHeader)
{
if (cookie.startsWith("csrfToken")) {
site.getCookieManager().getCookieStore().add(null, HttpCookie.parse(cookie).get(0));
}
}
}
urlConnection.disconnect();
} else {
urlConnection.disconnect();
return null;
}
I also tried to get all informations from the header but it doesn't change the things.
And then, during posts requests I insert the token like that:
urlConnection = (HttpURLConnection) url.openConnection();
if(site.getCookieManager().getCookieStore().getCookies().size() > 0)
{
urlConnection.setRequestProperty("Cookie",
TextUtils.join(";", site.getCookieManager().getCookieStore().getCookies()));
}
if ((params != null) && !params.isEmpty()) {
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
urlConnection.setRequestProperty("Accept-Charset", "UTF-8");
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + "UTF-8");
OutputStream output = urlConnection.getOutputStream();
output.write(params.getBytes("UTF-8"));
output.close();
}
is = urlConnection.getInputStream();
So if I look at urlconnection datas, I can see:
requestHeaders
nameAndValues
0 = "Cookie"
1 = "csrkToken=5f62......973"
2 = "Accept-Charset"
3 = "UTF-8"
4 = "Content-Type"
5 = "application/x-www-form-urlencoded;charset=UTF-8"
But when I execute urlConnection.getInputStream()
, I get the following exception:
java.io.FileNotFoundException: http://my.example.com/mywebservice
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:238)
at com.ndguide.ndguide.JSONParser.getJSONFromUrl(JSONParser.java:93)
at com.ndguide.ndguide.MainActivity.sendRegistrationIdToBackend(MainActivity.java:1562)
at com.ndguide.ndguide.MainActivity.access$600(MainActivity.java:82)
at com.ndguide.ndguide.MainActivity$2.doInBackground(MainActivity.java:1160)
at com.ndguide.ndguide.MainActivity$2.doInBackground(MainActivity.java:1122)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
May I precise that if I don't load the Csrf component on the server side, everything goes fine.
I also tried to add the following lines at the begining of my app as I read here, but it cause the same exception:
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
So what's wrong with my headers?