Code examples

21 views 0

Creating client

        // first we create web client
        Uri requestUri = 
          new Uri("http://192.168.1.1:10201/file/%2F01%2F100001%2Fexample.com%2Findex.html");

        // hack to force canonical path and query
        string paq = uri.PathAndQuery; // need to access PathAndQuery
        FieldInfo flagsFieldInfo = typeof(Uri)
        .GetField("m_Flags", BindingFlags.Instance | BindingFlags.NonPublic);
        ulong flags = (ulong)flagsFieldInfo.GetValue(uri);
        flags &= ~((ulong)0x30); // Flags.PathNotCanonical|Flags.QueryNotCanonical
        flagsFieldInfo.SetValue(uri, flags);

        WebRequest client = WebRequest.Create(requestUri);

        // add authentication
        string credential = String.Format("{0}:{1}", "username", "p@ssword");
        client.Headers.Add("Authorization", "Basic "
        + Convert.ToBase64String(Encoding.ASCII.GetBytes(credential)));

Calling agent operations using .NET client

        HttpWebRequest restclient = (HttpWebRequest)client;
        restclient.Method = method;
        restclient.Accept = AcceptType;
        
        // now we execute action
        try
        {
        HttpWebResponse response = (HttpWebResponse)restclient.GetResponse();
        if ((int)response.StatusCode < 200 || (int)response.StatusCode > 299)
              {
                  throw new Exception("Bad status code: " + response.StatusCode);
              }

              return response.GetResponseStream();
          }
          catch (WebException e)
          {
              HttpWebResponse response = (HttpWebResponse)e.Response;

              if (response != null)
              {
                  using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                  {
                      // handle error
                      string json = reader.ReadToEnd();
                      reader.Close();
                      response.Close();
                  }
              }

              throw;
        }
        

Was this helpful?