ArcGIS API for Silverlight - Library Reference
DownloadStringAsync Method
See Also  Example Send comments on this topic
ESRI.ArcGIS.Client Namespace > ArcGISWebClient Class : DownloadStringAsync Method

uri
A System.Uri containing the URI to download.
parameters
The collection of query key/value pairs associated with the request.
httpMethod
The HTTP method to use.
userState
The user-specified identifier for the asynchronous task.
Downloads asynchronously the resource specified as a System.Uri.

Syntax

Visual Basic (Declaration) 
Public Sub DownloadStringAsync( _
   ByVal uri As Uri, _
   Optional ByVal parameters As IDictionary(Of String,String), _
   Optional ByVal httpMethod As ArcGISWebClient.HttpMethods, _
   Optional ByVal userState As Object _
) 
C# 
public void DownloadStringAsync( 
   Uri uri,
   IDictionary<string,string> parameters,
   ArcGISWebClient.HttpMethods httpMethod,
   object userState
)

Remarks

Use the ArcGISWebClient.DownloadStringAsync Method / ArcGISWebClient.DownloadStringCompleted Event to process (read/write) text such as: JSON, XAML, XML, and HTML.

Use the ArcGISWebClient.OpenReadAsync Method / ArcGISWebClient.OpenReadCompleted Event to process (read/write) binary streams of data such as: images, files (.doc, .pdf, .exe), music, movies, etc.

Parameters

uri
A System.Uri containing the URI to download.
parameters
The collection of query key/value pairs associated with the request.
httpMethod
The HTTP method to use.
userState
The user-specified identifier for the asynchronous task.

Example

How to use:

Click the 'DownloadStringAsync' Button to start an ArcGISWebClient operation. Status information will be displayed. The ArcGISWebClient operation could take several minutes to complete before it displays the JSON response string in the TextBox. Repeat clicking the 'DownloadStringAsync' button but before the ArcGISWebClient completes, click the 'CancelAsync' button to kill the operation.

The XAML code in this example is used in conjunction with the code-behind (C# or VB.NET) to demonstrate the functionality.

The following screen shot corresponds to the code example in this page.

Using the ArcGISWebClient.DownloadStringAsync Method to obtain JSON string information from ArcGIS Server.

XAMLCopy Code
<Grid x:Name="LayoutRoot" Background="White">
  
  <!-- Buttons to perform the work. -->
  <Button Content="DownloadStringAsync" Height="40" Width="201" HorizontalAlignment="Left" Margin="12,135,0,0" 
          Name="Button_DownloadStringAsync" VerticalAlignment="Top" Click="Button_DownloadStringAsync_Click" />
  <Button Content="CancelAsync" Height="40" Width="201" HorizontalAlignment="Left" Margin="12,184,0,0" 
          Name="Button_CancelAsync" VerticalAlignment="Top" Click="Button_CancelAsync_Click" IsEnabled="False"/>
  
  <!-- Provide ArcGISWebClient information messages. -->
  <sdk:Label Height="19" HorizontalAlignment="Left" Margin="229,131,0,0" Name="Label_StatusInfo" 
       VerticalAlignment="Top" Width="120" Content="Status Information:"/>
  <ListBox Height="80" HorizontalAlignment="Left" Margin="229,147,0,0" Name="ListBox_StatusInformation" 
           VerticalAlignment="Top" Width="383" />
  
  <!-- Display the Response for the ArcGISWebClient operation. -->
  <TextBox Height="355" HorizontalAlignment="Left" Margin="12,233,0,0" Name="TextBox_Response" 
           VerticalAlignment="Top" Width="600" VerticalScrollBarVisibility="Auto" 
           HorizontalScrollBarVisibility="Auto" />
  
  <!-- Provide the instructions on how to use the sample code. -->
  <TextBlock Height="94" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" Width="625" 
             TextWrapping="Wrap" Text="Click the 'DownloadStringAsync' Button to start an ArcGISWebClient operation. Status information 
             will be displayed. The ArcGISWebClient operation could take several minutes to complete before it displays 
             the JSON response string in the TextBox. Repeat clicking the 'DownloadStringAsync' button but before the 
             ArcGISWebClient completes, click the 'CancelAsync' button to kill the operation." />
  
</Grid>
C#Copy Code
// Create a Global (aka. Member) variable for the ArcGISWebClient object that will be used in other  parts of the application.
private ESRI.ArcGIS.Client.ArcGISWebClient _ArcGISWebClient;
            
private void Button_DownloadStringAsync_Click(object sender, System.Windows.RoutedEventArgs e)
{
  // Clear out any existing messages.
  ListBox_StatusInformation.Items.Clear();
  TextBox_Response.Text = "";
  
  // Set the _ArcGISWebClient variable to a new instance of the ArcGISWebClient Class.
  _ArcGISWebClient = new ESRI.ArcGIS.Client.ArcGISWebClient();
  
  // Wire up an Event Handler for the ArcGISWebClient.DownloadStringCompleted Event.
  _ArcGISWebClient.DownloadStringCompleted += arcgisWebClient_DownloadStringCompleted;
  
  // Prevent the internet browser from using a cache for the ArcGISWebClient operations. Useful in this example because
  // running it two or more times would just use the internet browsers cached result basically rendering the CancelAsync 
  // Button useless.
  _ArcGISWebClient.DisableClientCaching = true;
  
  // Provide a valid Url for the ArcGISWebClient request.
  string myUrl = "http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_1990-2000_Population_Change/MapServer/1/query";
  
  // Rather than having all of the parameter arguments as part of the Url, create a parameter collection.
  System.Collections.Generic.IDictionary<string, string> myParameters = new System.Collections.Generic.Dictionary<string, string>();
  myParameters.Add("where", "POPGRW00CY > 100"); // Restrict the number of records returned.
  myParameters.Add("returnGeometry", "false"); // Do not include the geometry coordinate values.
  myParameters.Add("outFields", "*"); // Return all of the fields.
  myParameters.Add("f", "pjson"); // Get the results back in the 'pretty' JSON format.
  
  // NOTE: If you did not want to use a parameter collection you could have used a Url like the following:
  // http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_1990-2000_Population_Change/MapServer/1/query?where=POPGRW00CY+%3E+100&returnGeometry=false&outFields=*&f=pjson
  
  // Invoke the ArcGISWebClient.DownloadStringAsync Method passing in the Url, parameter collection, and null for a user token.
  _ArcGISWebClient.DownloadStringAsync(new Uri(myUrl), myParameters,ESRI.ArcGIS.Client.ArcGISWebClient.HttpMethods.Auto,null);
  
  // Update the status informational messages.
  ListBox_StatusInformation.Items.Add("The ArcGISWebClient operation was started.");
  ListBox_StatusInformation.Items.Add("(It could take a few minutes)");
  
  // Enable stopping the ArcGISWebClient operation.
  Button_CancelAsync.IsEnabled = true;
}
            
private void Button_CancelAsync_Click(object sender, System.Windows.RoutedEventArgs e)
{
  // This will cancel the ArcGISWebClient operation. 
  _ArcGISWebClient.CancelAsync();
}
            
private void arcgisWebClient_DownloadStringCompleted(object sender, ESRI.ArcGIS.Client.ArcGISWebClient.DownloadStringCompletedEventArgs e)
{
  // Get the whether the ArcGISWebClient operation was canceled.
  bool IsCanceled = e.Cancelled;
  
  // Get the JSON string result from the ArcGISWebClient operation.
  string theResult = e.Result;
  
  if (IsCanceled)
  {
    // The user canceled the ArcGISWebClient operation.
    
    // Update the status informational messages.
    ListBox_StatusInformation.Items.Add("The ArcGISWebClient operation was canceled.");
  }
  else
  {
    // The ArcGISWebClient operation completed successfully. 
    
    // Update the status informational messages.
    ListBox_StatusInformation.Items.Add("The ArcGISWebClient operation completed.");
  
    // Display the JSON string response to the user.
    TextBox_Response.Text = theResult;
  }
  
  // Disable the ability to CancelAsync Button.
  Button_CancelAsync.IsEnabled = false;
}
VB.NETCopy Code
' Create a Global (aka. Member) variable for the ArcGISWebClient object that will be used in other  parts of the application.
Private _ArcGISWebClient As ESRI.ArcGIS.Client.ArcGISWebClient
            
Private Sub Button_DownloadStringAsync_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
  
  ' Clear out any existing messages.
  ListBox_StatusInformation.Items.Clear()
  TextBox_Response.Text = ""
  
  ' Set the _ArcGISWebClient variable to a new instance of the ArcGISWebClient Class.
  _ArcGISWebClient = New ESRI.ArcGIS.Client.ArcGISWebClient
  
  ' Wire up an Event Handler for the ArcGISWebClient.DownloadStringCompleted Event.
  AddHandler _ArcGISWebClient.DownloadStringCompleted, AddressOf arcgisWebClient_DownloadStringCompleted
  
  ' Prevent the internet browser from using a cache for the ArcGISWebClient operations. Useful in this example because
  ' running it two or more times would just use the internet browsers cached result basically rendering the CancelAsync 
  ' Button useless.
  _ArcGISWebClient.DisableClientCaching = True
  
  ' Provide a valid Url for the ArcGISWebClient request.
  Dim myUrl As String = "http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_1990-2000_Population_Change/MapServer/1/query"
  
  ' Rather than having all of the parameter arguments as part of the Url, create a parameter collection.
  Dim myParameters As System.Collections.Generic.IDictionary(Of String, String) = New System.Collections.Generic.Dictionary(Of String, String)
  myParameters.Add("where", "POPGRW00CY > 100") ' Restrict the number of records returned.
  myParameters.Add("returnGeometry", "false") ' Do not include the geometry coordinate values.
  myParameters.Add("outFields", "*") ' Return all of the fields.
  myParameters.Add("f", "pjson") ' Get the results back in the 'pretty' JSON format.
  
  ' NOTE: If you did not want to use a parameter collection you could have used a Url like the following:
  ' http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_1990-2000_Population_Change/MapServer/1/query?where=POPGRW00CY+%3E+100&returnGeometry=false&outFields=*&f=pjson
  
  ' Invoke the ArcGISWebClient.DownloadStringAsync Method passing in the Url, parameter collection, and nothing for a user token.
  _ArcGISWebClient.DownloadStringAsync(New Uri(myUrl), myParameters, ESRI.ArcGIS.Client.ArcGISWebClient.HttpMethods.Auto, Nothing)
  
  ' Update the status informational messages.
  ListBox_StatusInformation.Items.Add("The ArcGISWebClient operation was started.")
  ListBox_StatusInformation.Items.Add("(It could take a few minutes)")
  
  ' Enable stopping the ArcGISWebClient operation.
  Button_CancelAsync.IsEnabled = True
  
End Sub
            
Private Sub Button_CancelAsync_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
  
  ' This will cancel the ArcGISWebClient operation. 
  _ArcGISWebClient.CancelAsync()
  
End Sub
            
Private Sub arcgisWebClient_DownloadStringCompleted(sender As Object, e As ESRI.ArcGIS.Client.ArcGISWebClient.DownloadStringCompletedEventArgs)
  
  ' Get the whether the ArcGISWebClient operation was canceled.
  Dim IsCanceled As Boolean = e.Cancelled
  
  ' Get the JSON string result from the ArcGISWebClient operation.
  Dim theResult As String = e.Result
  
  If IsCanceled Then
    
    ' The user canceled the ArcGISWebClient operation.
    
    ' Update the status informational messages.
    ListBox_StatusInformation.Items.Add("The ArcGISWebClient operation was canceled.")
    
  Else
    
    ' The ArcGISWebClient operation completed successfully. 
    
    ' Update the status informational messages.
    ListBox_StatusInformation.Items.Add("The ArcGISWebClient operation completed.")
    
    ' Display the JSON string response to the user.
    TextBox_Response.Text = theResult
    
  End If
  
  ' Disable the ability to CancelAsync Button.
  Button_CancelAsync.IsEnabled = False
  
End Sub

Requirements

Target Platforms: Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family, Windows 7

See Also

© ESRI, Inc. All Rights Reserved.