Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
[This document supports a preliminary release of a software product that may be changed substantially prior to final commercial release. This document is provided for informational purposes only.]
As described in Specifying Queries Using REST or the Browser a GET request using REST interface can be sent to retrieve a single entity (authority, container or an entity) or submit a query to retrieve an entity set. The following C# sample illustrates both cases:
- The sample then creates WebRequest by passing a string that has a specific container URI followed by a query to retrieve all the entities in the container. The response in the case is an entity set.
- The response,
<Entity>or<EntitySet>can be processed as needed. For illustration the example uploads the result inXmlDocument.
using System;
using System.Text;
using System.Net;
using System.Xml;
using System.IO;
using System.Xml.Linq;
namespace QueryUsingREST
{
class Program
{
// Provide your data here
private static string userName;
private static string password;
private const string authorityId = "<YourExistingAuthorityId>";
private const string containerId = "<YourExistingContainerId>"; // that has the entity you want to retrieve
private const string entityId = "<YourExistingEntityId>";
private const string HttpGetMethod = "GET";
private const string ssdsContentType = "application/x-ssds+xml";
static void Main(string[] args)
{
Console.Write("Username: ");
userName = Console.ReadLine();
Console.Write("Password: ");
password = ReadPassword();
string entityUri = String.Format("https://{0}.data.database.windows.net/v1/{1}/{2}", authorityId, containerId, entityId);
string query = String.Format("https://{0}.data.database.windows.net/v1/{1}?q='from e in entities select e'", authorityId, containerId);
try
{
// GET entity
string data1 = GetResult(entityUri);
// process the single entity returned. For example, you may load it in XmlDocument
XmlDocument entityXml = new XmlDocument();
entityXml.LoadXml(data1);
// process the XmlDocument as you wish.
// Query contianer
string data2 = GetResult(query);
// process the single entity returned. For example, you may load it in XmlDocument
XmlDocument entitySetXml = new XmlDocument();
entitySetXml.LoadXml(data2);
// process the XmlDocument as you wish.
}
catch (WebException ex)
{
using (HttpWebResponse response = ex.Response as HttpWebResponse)
{
if (response != null)
{
string errorMsg = ReadResponse(response);
Console.WriteLine("Error:{0}", errorMsg);
Console.WriteLine("Unexpected status code returned: {0}", response.StatusCode);
}
else
{
// Process DNS name resolution error
Console.WriteLine(ex.Message);
}
}
}
}
private static string GetResult(string entityUri)
{
string data = null;
if (string.IsNullOrEmpty(entityUri))
{
throw new ArgumentOutOfRangeException("entityUri");
}
// retrieve
WebRequest request = HttpWebRequest.Create(entityUri);
request.Method = HttpGetMethod;
//request.ContentLength = 0;
request.ContentType = ssdsContentType;
request.Credentials = new NetworkCredential(userName, password);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
data = ReadResponse(response);
if (response.StatusCode != HttpStatusCode.OK)
{
Console.WriteLine("Error: {0}", data);
Console.WriteLine("Unexpected status code returned: {0}", response.StatusCode);
}
}
return data;
}
public static string ReadResponse(HttpWebResponse response)
{
// Begin by validating our inbound parameters.
//
if (response == null)
{
throw new ArgumentNullException("response", "Value cannot be null");
}
// Then, open up a reader to the response and read the contents to a string
// and return that to the caller.
//
string responseBody = "";
using (Stream rspStm = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(rspStm))
{
responseBody = reader.ReadToEnd();
}
}
return responseBody;
}
private static string ReadPassword()
{
StringBuilder retval = new StringBuilder();
ConsoleKeyInfo keyInfo;
while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Enter)
{
Console.Write("*");
retval.Append(keyInfo.KeyChar);
}
Console.WriteLine();
return retval.ToString();
}
}
}
See Also
Concepts
Specifying Queries Using REST or the Browser
Querying SQL Data Services