To start using REST with ArcWeb Services, you need an authentication token to verify you are authorized to access ArcWeb Services. There are two ways to request an authentication token—using getToken or using getUserID. See Authentication for the differences between UserIDs and tokens.
WARNING: If a token from a getUserID request is hacked and hijacked, it can be used to consume your ArcWeb credits. This can be stopped by changing your password, which changes your UserID, invalidating the previous one, or you can disable the UserID feature on the ArcWeb UserID account page.
There are several ways to obtain a REST authentication token:
NOTE: See Samples—terms and conditions for usage permissions.
This sample demonstrates how to obtain a REST token for ArcWeb Services using C# .NET. The sample includes code for obtaining a REST token and a REST UserID.
NOTE: Before running this application, make sure the UserID feature is enabled on the ArcWeb UserID account page.
NOTE: If you run this application locally, make sure the URL contains the machine name instead of the local host. This is required to generate the ArcWeb token containing the IP address of the requesting client, your machine. For example:
http://<hostmachine>:<port>/<application context path>/<C# page name>.aspx
The following lines in WebForm1_aspx_cs.htm are used in a request for a REST token to obtain a token. Replace [ArcWeb username] and [ArcWeb password] with your ArcWeb Services user name and password:
string username="[ArcWeb username]"; //ArcWeb user name
string password="[ArcWeb password]"; //ArcWeb password
Use the following lines to get the client IP address. This IP address is used for creating the token so that the token can be used for additional map requests originating only from this IP address. If this token is used from a different IP address, then an invalid token error message will be received:
string ipAdd = "";//ip address of the requesting client
ipAdd = Request.UserHostAddress; //ip of the client sending request
The following line creates an ArcWeb REST URL to generate an ArcWeb REST token:
string urlToGetToken = "https://www.arcwebservices.com/services/v2006_1/restmap?actn=getToken&usr="+ username + "&pswd=" + password + "&ip=" +ipAdd;
The following code reads the XML response for an ArcWeb REST token:
XmlTextReader reader = new XmlTextReader(urlToGetToken);
while ( reader.Read() )
{
reader.MoveToContent();
if (reader.Value != "")
{
token = reader.Value;
}
}
The token variable contains the generated token at the end of the while loop.
This sample demonstrates how to obtain a REST token for ArcWeb Services using Java. When a valid HTTP GET or HTTP POST ArcWeb REST request is sent for an ArcWeb UserID or token, the ArcWeb server generates the UserID or token using the credentials supplied in the request in the XML format. This sample shows how to send the request to the ArcWeb server and parse the XML response containing the requested token.
username: ArcWeb Username
password: ArcWeb Password
NOTE: Before running this application, make sure the UserID feature is enabled on the ArcWeb UserID account page.
http://<host>:<port>/<path to jsp>/cmrHTTPHeader.jsp
NOTE: If you run this application locally, make sure the URL contains the machine name instead of the local host. This is required to generate the ArcWeb token containing the IP address of the requesting client, which is used to generate the token so that this token is valid for all subsequent map requests that originate from this requesting client. An Invalid Token error will be thrown if this token is used from a different client machine. Note that this is not the case for ArcWeb UserID.
In the following lines in getTokenAndUserID.jsp, replace [ArcWeb username] and [ArcWeb password] with your ArcWeb Services user name and password:
String username="[ArcWeb username]"; //ArcWeb password
String password="[ArcWeb password]";//ArcWeb user name
The following line gets the IP address of the client:
String ipAddress = request.getRemoteAddr();
The following code creates a URL to send a request to ArcWeb REST services to generate a token. Perform URL encoding to prevent special characters from breaking the code.
String token= "";
String urlString = "https://www.arcwebservices.com/services/v2006_1/restmap?actn=getToken&usr="+
URLEncoder.encode(username, "UTF-8")+
"&pswd="+URLEncoder.encode(password,"UTF-8")+
"&ip="+URLEncoder.encode(ipAddress, "UTF-8");
The following code opens a URL connection and prevents cached data from being used:
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
connection.setUseCaches(false);
Create an instance of variable to store the XML response. Set properties to prevent XML validation and namespace usage, and ignore white spaces and comments.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// prevent validation of the XML
dbf.setValidating(false);
// Prevent namespace usage
dbf.setNamespaceAware(false);
//ignore white-space
dbf.setIgnoringElementContentWhitespace(true);
// To ignore comments
dbf.setIgnoringComments(true);
The following code reads the XML response and parses it. An exception occurs when there is an error.
try {
// reading the XML file
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc=null;
try{
doc = db.parse(connection.getInputStream());
}catch (Exception e) {
throw e;
}
The following code gets the content of the <TOKEN> tag from the XML response. If the response does not contain a <TOKEN> tag, then it generates an error.
Element elmt = doc.getDocumentElement();
String tag = elmt.getTagName();
if (!tag.equals("TOKEN")){
throw new Exception("Could not get token");
}
token = elmt.getFirstChild().getNodeValue();
} catch(ParserConfigurationException e){}
return token;
}
}
This sample demonstrates how to obtain a REST token for ArcWeb Services using the ProxyServlet method.
The ProxyServlet method:
In this method, the client makes the REST request to the ProxyServlet without using the tkn parameter. The ProxyServlet generates the token transparently to the end user, appends it to the parameters received from client, and makes a call to the ArcWeb Server. The ProxyServlet collects the response and passes the request to the client.
How to obtain a REST token using the ProxyServlet method using Apache with Tomcat
The following steps are for Apache with Tomcat Web server in the Windows operating environment:
<TOMCAT_HOME>
>webapps
>restproxyservlet
>META-INF
>context.xml
>MANIFEST.MF
>WEB-INF
>web.xml
>Classes
>com
>esri
>web
>proxy
>restProxy.java
>lib
>commons-logging-1.0.4.jar
>commons-io-1.1.jar
%CLASSPATH%;%<TOMCAT_HOME>%\webapps\restproxyservlet\WEB-INF\lib\commons-logging-1.0.4.jar;%<TOMCAT_HOME>%\webapps\restproxyservlet\WEB-INF\lib\commons-io-1.1.jar;
For example:
c:\<TOMCAT_HOME>\webapps\restproxyservlet\WEB-INF\classes\com\esri\web\proxy>javac
restProxy.java
This example displays a U.S. map in SVG format.
Visit the Feedback page to give comments or suggestions about the ArcWeb Developer's Guide.