The Well-Formed Web

Exploring the limits of XML and HTTP

RESTLog Client Details

The source for RESTLogClient is contained in two files: PostEditor.cs which handles all the user-interface and ContentManager.cs which handles the web interface. The lowest level inteface in ContentManager.cs is RESTLogPublisher and it handles most of that web interface. The public members of RESTLogPublisher are:

public class RESTLogPublisher {
  .
  .
  .
  public void PublishNewItem(string filename) {
    ..
  }

  public void PublishEditedItem(
    string filename, 
    int itemID) 
  {
    ...
  }

  public void DeleteItem(int itemID) {
    ...
  }
}

The three public methods of RESTLogPublisher are DeleteItem, PublishEditedItem and PublishNewItem. These in turn use one of the two private functions SimpleRequest and Publish. Now SimpleRequest is just that, a simple HTTP request that has no content. You hand it the URL and the HTTP verb and it does the work.

Publish performs HTTP requests that send content in the request, so it needs to be passed the verb, the URI, and the name of the file that contains the content to be sent. Note that because of the limited repertoire of RESTLog that many things in Publish are hard-coded, for example the timeout is set to 30 seconds and the content type is always 'text/xml'. The comprehensive HTTP support in the .Net framework makes this a fairly simple, if verbose, exercise.

public class RESTLogPublisher {
  .
  .
  .
  private void Publish(
    string filename, 
    string verb, 
    string url) 
  {
    WebRequest req = WebRequest.Create(url);
    req.ContentType = "text/xml";
    req.Credentials = new NetworkCredential(
       config_.user_, config_.password_
    );
    FileStream inFile = new FileStream(
       filename, FileMode.Open
    );
    int fileSize = (int)inFile.Length;
    byte[] data = new byte[fileSize];
    inFile.Read(data, 0, fileSize);
    inFile.Close();
    req.ContentLength = fileSize;
    req.Method = verb;
    req.Timeout = 30000;
    Stream upstream = req.GetRequestStream();
    upstream.Write(data, 0, fileSize);
    upstream.Close();
    WebResponse resp = req.GetResponse();
    resp.Close();
  }

  private void SimpleRequest(
    string verb, 
    string url) 
  {
    WebRequest req = WebRequest.Create(url);
    req.Credentials = new NetworkCredential(
      config_.user_, 
      config_.password_
    );
    req.Method = verb;
    req.Timeout = 30000;
    WebResponse resp = req.GetResponse();
    resp.Close();
  }
}

Next I'll build a list of new features to add to RESTLog. Over the next few weeks I'll add them one at a time to the server and client.

2002-11-26 21:35 Comments (0)