Home > Articles > Programming > ASP .NET

  • Print
  • + Share This
Like this article? We recommend Handling the HTTP Methods

Handling the HTTP Methods

With that background in place, let's look at handling the various HTTP methods from both the Silverlight and Python sides of the conversation. We'll also cover a fair share of other common items in GET that frame how the remaining messages work.

HTTP GET

A GET is a request for a resource. Because it has no content in the request body, a GET request is incredibly simple. Upon load, the Silverlight control sends out a request to the originating server, asking for the list of images for the current logged-in user. In Silverlight, everything that may take time has to be asynchronous, which keeps the user interface (UI) responsive and users happy. (It also keeps people from saying, "Silverlight is slow.")

When our web form came up, it added a handler to the Loaded event to call LoadImages(). In turn, LoadImages executes an HTTP GET against the ImageListService URI and returns a list of images. Let's step through this activity, looking at each piece as it executes.

void LoadImages()
{
    try
    {
        WebRequest request =
           WebRequest.Create(ImageListService);
        request.BeginGetResponse(ImagesResponse, request);
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.ToString());
    }
}

As soon as request.BeginGetResponse() is executed, the message goes from the Silverlight application to GAE. When that response returns (or times out), the ImagesResponse method is called with a reference to the original request.

Now we're over in GAE land. Here, a pair of data types gives us access to all of our data. We have an owner type, named Owner, that keeps track of the owner name. The type derives from google.appengine.ext.db.Model. All Model types can contain values, and they all have a globally unique identifier named key. The key comes from the base class:

class Owner(db.Model):
    name=db.StringProperty()

An Owner acts as the parent for another custom type, ImageFile. The Python ImageFile object, which stores data about images, knows the following:

  • The Owner
  • Image metadata (caption and description)
  • Visibility of the image (public or private)
  • The image itself

It has the following declaration:

class ImageFile(db.Model):
    owner = db.ReferenceProperty(Owner)
    caption = db.StringProperty()
    description = db.TextProperty()
    image = db.BlobProperty()
    date = db.DateTimeProperty(auto_now_add=True)
    public = db.BooleanProperty()

When the HTTP GET request comes in, asking for a list of images, the ImageList class responds through its GET method:

class ImageList(webapp.RequestHandler):
    def get(self):
        username = ""
        # Get the current user
        owner = getCurrentUserOwner()
        if (not owner is None):
            # The user is known. Fetch the first 1000 photos
            images = db.GqlQuery(
                      "SELECT * FROM ImageFile WHERE owner=:1",
                      owner).fetch(1000, 0)
            imageList = []

            # Create temp objects that only
            # contain the info we need to be sent to
            # the caller.
            for image in images:
                key = image.key()
                temp = {'key': str(key),
                        'caption': str(image.caption),
                        'description':str(image.description),
                        'public': str(image.public)}
                imageList.append(temp)

            # Write the objects to a string and send them on
            # back
            writer = JsonWriter()
            imageResponse = writer.write(imageList)
            self.response.headers['Content-Type'] = \
               "application/json"
            self.response.out.write(imageResponse)

That last line sends the response back to our Silverlight application, which is patiently waiting for the news about images. When the response arrives, the code needs to put the image information from Python into a format suitable for consumption in .NET. (I call the type ImageItem because it's smaller than and slightly different from ImageFile on the Python side.) We also need to give the object a bit of a hand so that it can explain how to read and write itself to the .NET serialization framework.

[DataContract]
public class ImageItem : INotifyPropertyChanged
{
    public static string BaseUri { get; set; }
    public ImageItem() { }

    public string ImageUrl
    {
        get
        {
            return string.Format(
                "{0}?id={1}", BaseUri, ImageId);
        }
    }

    string _caption;
    [DataMember(Name = "caption")]
    public string Caption
    {
        get { return _caption; }
        set
        {
            _caption = value;
            NotifyChange("Caption");
        }
    }


    string _description;
    [DataMember(Name="description")]
    public string Description
    {
        get { return _description; }
        set
        {
            _description = value;
            NotifyChange("Description");
        }
    }

    [DataMember(Name = "key")]
    public string ImageId { get; set; }


    bool _publicImage;
    [DataMember(Name="public")]
    public bool PublicImage
    {
        get { return _publicImage; }
        set
        {
            _publicImage = value;
            NotifyChange("PublicImage");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    void NotifyChange(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,
               new PropertyChangedEventArgs(property));
        }
    }
}

You may have noticed that the type also implements INotifyPropertyChanged. This interface allows the type to communicate changes in values back to any UI that's bound to those values. With this type in hand, we can read the returned JSON into ImageItem:

void ImagesResponse(IAsyncResult result)
{
    WebRequest request = (WebRequest)result.AsyncState;
    try
    {
        WebResponse response = request.EndGetResponse(result);
        Stream responseStream = response.GetResponseStream();

        // Get a serializer
        DataContractJsonSerializer ser = new
            DataContractJsonSerializer(typeof(ImageItem[]));

        // Read the data into an array
        ImageItem[] imageItems =
            ser.ReadObject(responseStream) as ImageItem[];

        // Update the UI
        ImageDocument = new ObservableCollection<ImageItem>();
        foreach (ImageItem item in imageItems)
        {
            ImageDocument.Add(item);
        }
        Dispatcher.BeginInvoke(delegate
        {
            lstImages.DataContext = ImageDocument;
        });
    }
    catch (WebException we)
    {
        HttpWebResponse response = (HttpWebResponse)we.Response;
        Debug.WriteLine(we.ToString());
        response.Close();
    }
    catch (Exception e)
    {
        Dispatcher.BeginInvoke(delegate
        {
            MessageBox.Show(e.ToString());
        });
    }
}

That completes the loop for an HTTP GET.

Next up: What about adding an image?

HTTP POST

When adding a new item, we need to transfer data from the client to the server. Two HTTP methods allow for content in the request body: PUT and POST.

If we're looking at pure REST, creation happens with a POST and updates with a PUT. In practice, PUT and POST functionality typically are merged, for a couple of reasons:

  • HTML 4.01 forms limit <form> tags to two method attribute values: GET and POST.
  • Many developers prefer to have code that appears as a Save and handles the create/update logic at a lower level.

Our application uses POST in two places: when adding a new image, and when updating the image metadata. When uploading a binary image, the user clicks the Add Images button in the UI. After the user selects a file from the local machine, the Silverlight UI loads the image and sends it off to the server. The code reads the file from the OpenFileDialog at this point. The application can't just save the filename to send the file later; the user gave permission to read the file by selecting it via the OpenFileDialog, so we read the file and store its contents in memory:

private void AddImages_Click(object sender, RoutedEventArgs e)
{
    // Let the user pick a JPEG or PNG to send.
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Multiselect = false;
    ofd.Filter = "JPEG Files|*.jpeg;*.jpg|PNG Files|*.png";
    bool? result = ofd.ShowDialog();
    if (result.HasValue && result.Value)
    {
        // Read the file.
        Stream stream = ofd.File.OpenRead();
        byte[] bytes = new byte[ofd.File.Length];
        stream.Read(bytes, 0, bytes.Length);
        stream.Close();

        // Send the file to the application.
        WebRequest request =
           WebRequest.Create(ImageListService);
        request.Method = "POST";
        request.ContentType = "image";
        request.BeginGetRequestStream(
            GetAddImageRequestStreamCallback,
            new object[] { request, bytes });
    }
}

When the callback returns, we have access to the request stream and can send the file to the server. The application grabs the request stream and writes the image to the stream.

After writing the image, we Close() the stream:

void GetAddImageRequestStreamCallback(IAsyncResult result)
{
    object[] stateVariables = (object[])result.AsyncState;
    WebRequest request = (WebRequest)stateVariables[0];
    byte[] image = (byte[])stateVariables[1];
    try
    {
        Stream stream = request.EndGetRequestStream(result);
        stream.Write(image, 0, image.Length);
        stream.Close();
        request.BeginGetResponse(AddResponse, request);
    }
    catch (WebException we)
    {
        HttpWebResponse response = (HttpWebResponse)we.Response;
        Debug.WriteLine(we.ToString());
        response.Close();
    }
}

This message to add an image then goes over to the ImageList endpoint in our GAE application. There, we read in the image, resize it as a 300 [ts] 300 image, and store it to the data store. If the bytes sent up don't represent a JPEG or PNG image, the code below fails and nothing is stored; the logic in the web application returns an error indicating what went wrong.

In this case, the error will only make sense to those who also develop for the website. Because we don't intend to share this endpoint with other consumers, that limitation is perfectly okay. (If you expose your endpoints for consumption by others, you should catch any exceptions and provide meaningful diagnostics about what went wrong.)

def post(self):
    currentUser = getCurrentUserOwner()
    if currentUser is None:
        return

    image = ImageFile()
    uploaded = self.request.body
    image.image = db.Blob(images.resize(uploaded, 300, 300))
    image.owner = currentUser
    image.public = False
    image.put()

When the response returns, we reload the images and update the display:

void AddResponse(IAsyncResult result)
{
    WebRequest request = (WebRequest)result.AsyncState;
    try
    {
        WebResponse response = request.EndGetResponse(result);
        LoadImages();
    }
    catch (WebException we)
    {
        HttpWebResponse response = (HttpWebResponse)we.Response;
        Debug.WriteLine(we.ToString());
        response.Close();
    }
}

Once the user adds the image, she might want to update the image metadata. When the selected image changes, the application sends any changes back to the server. The GAE application only accepts JSON serialized data, so our UI needs to send out the data in JSON format. The endpoint for updates is an individual image endpoint. We bind the selected item to the details view, to make getting and setting properties easy:

private void ListBox_SelectionChanged(object sender,
    SelectionChangedEventArgs e)
{
    if (grdDetails.DataContext != null && chkPublic.IsEnabled)
    {
        WebRequest request = WebRequest.Create(ImageService);
        request.Method = "POST";
        request.ContentType = "application/json";
        ImageItem imageItem = (ImageItem)grdDetails.DataContext;
        request.BeginGetRequestStream(
            GetUpdateImageRequestStreamCallback,
            new object[] { request, imageItem });
    }
    grdDetails.DataContext = ((ListBox)sender).SelectedItem;
}

A few cycles later, the application executes our callback so that we can send the ImageItem to the GAE side. We use a DataContractJsonSerializer to write the object to the request body:

void GetUpdateImageRequestStreamCallback(IAsyncResult result)
{
    try
    {
        object[] stateInfo = (object[])(result.AsyncState);
        WebRequest request = (WebRequest)(stateInfo[0]);
        ImageItem imageItem = (ImageItem)(stateInfo[1]);
        Stream stream = request.EndGetRequestStream(result);
        DataContractJsonSerializer ser = new
            DataContractJsonSerializer(typeof(ImageItem));
        ser.WriteObject(stream, imageItem);
        stream.Close();
        request.BeginGetResponse(UpdateResponse, request);
    }
    catch (Exception ex)
    {
        Dispatcher.BeginInvoke(delegate
        {
            MessageBox.Show(ex.ToString());
        });
    }
}

The GAE side picks up this message and reads it into an existing ImageFile instance, and we load the item from the database. If the item exists, and the image's owner matches the current user, the update happens:

def post(self):
   reader = JsonReader()
   currentUser = getCurrentUserOwner()
   tempImage = reader.read(self.request.body)
   if not currentUser is None:
       requestedImage = db.get(tempImage['key'])
       if requestedImage.owner.key() == currentUser.key():
           requestedImage.caption = tempImage['caption']
           requestedImage.description = tempImage['description']
           requestedImage.public = tempImage['public']
           requestedImage.put()

An empty response comes back from the update, and we move on.

void UpdateResponse(IAsyncResult result)
{
    WebRequest request = (WebRequest)result.AsyncState;
    try
    {
        WebResponse response = request.EndGetResponse(result);
        response.Close();
    }
    catch (WebException we)
    {
        HttpWebResponse response = (HttpWebResponse)we.Response;
        Debug.WriteLine(we.ToString());
        response.Close();
    }
}
  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020