Question about clearing the cache ??

3364
6
06-20-2010 09:04 PM
NoureddineEl-zaatari
New Contributor
Hello,

i was wondering if we can clear the cache through code, on demand ??? is that possible ??

thank you
0 Kudos
6 Replies
VasylMelnychuk
New Contributor
You may emulate POST call to rest admin.

URL: http://[server]/ArcGIS/rest/admin/login
POST parameters:
username - admin user name
password - admin password
redirect - http://[server]/ArcGIS/rest/admin/cache/clear (this will redirect to clear url automaticaly)

You may try someting like this:
using (System.Net.WebClient client = new System.Net.WebClient())
{
    client.Headers["Content-type"] = "application/x-www-form-urlencoded";
    client.Encoding = System.Text.Encoding.UTF8;
    var collection = new System.Collections.Specialized.NameValueCollection();
    collection.Add("username", adminUserName);
    collection.Add("password", adminPassword);
    collection.Add("redirect", http://[server]/ArcGIS/rest/admin/cache/clear);
    var response = client.UploadValues("http://[server]/ArcGIS/rest/admin/login", "POST", collection);
    // Process response
}
0 Kudos
RaviNarayanan
Esri Contributor
At 10.0 REST API will support clearing the cache using tokens. Please see this link below for more details.
http://help.arcgis.com/EN/arcgisserver/10.0/apis/rest/generateadmintoken.html
0 Kudos
NoureddineEl-zaatari
New Contributor
vasmel,

i tried your code,, didnt clear the cache ?? any idea ?? i got no errors, response variable comes as an array of bytes


thanks
0 Kudos
VasylMelnychuk
New Contributor
xnoor,

I have forgotten that WebClient is not supporting cookies (but it is required).

Override WebClient class to support cookies
        public class CookieAwareWebClient : WebClient
        {
            private CookieContainer m_container = new CookieContainer();
            protected override WebRequest GetWebRequest(Uri address)
            {
                WebRequest request = base.GetWebRequest(address);
                if (request is HttpWebRequest)
                    (request as HttpWebRequest).CookieContainer = m_container;
                return request;
            }
        }


Also you may convert response bytes to string:

            using (CookieAwareWebClient client = new CookieAwareWebClient())
            {
                client.Headers["Content-type"] = "application/x-www-form-urlencoded";
                client.Encoding = System.Text.Encoding.UTF8;
                var collection = new System.Collections.Specialized.NameValueCollection();
                collection.Add("username", adminUserName);
                collection.Add("password", adminPassword);
                collection.Add("redirect", "http://[server]/ArcGIS/rest/admin/cache/clear");
                var response = client.UploadValues("http://[server]/ArcGIS/rest/admin/login", collection);
                MemoryStream stream = new MemoryStream(response);
                StreamReader reader = new StreamReader(stream);
                String responseString = reader.ReadToEnd();               
            }


I have tried this... it works for me...

Regards,
Vasyl Melnychuk
0 Kudos
Ranga_Tolapi
Occasional Contributor III
Can any one give me the java equivalent code to achieve this.
0 Kudos
Ranga_Tolapi
Occasional Contributor III
This java code works...

import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

private static void clearRestCache(String server, String username, String password) {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
         String loginUrl = "http://" + server + ":8399/arcgis/rest/admin/login";
         String cacheClearUrl = "http://" + server + ":8399/arcgis/rest/admin/cache/clear";
            HttpGet httpget = new HttpGet(loginUrl + "?username=" + username + "&password=" + password + "&redirect=" + cacheClearUrl);
            httpclient.execute(httpget);
        } catch (Exception e) {
         e.printStackTrace();
        } finally {
            httpclient.getConnectionManager().shutdown();
        }
}
0 Kudos