private void OnViewResponseHeadersButtonClick(NEventArgs args) { // get the response form the button tag (see UpdateRequestListBoxItem) and display its headers object[] array = (object[])args.TargetNode.Tag; NHttpRequest request = (NHttpRequest)array[0]; NHttpResponse response = (NHttpResponse)array[1]; // create a top level window, setup as a dialog NTopLevelWindow window = NApplication.CreateTopLevelWindow(); window.SetupDialogWindow(request.Uri.ToString(), true); // create a list box for the headers NListBox listBox = new NListBox(); window.Content = listBox; // fill with header fields INIterator <NHttpHeaderField> it = response.HeaderFields.GetIterator(); while (it.MoveNext()) { listBox.Items.Add(new NListBoxItem(it.Current.ToString())); } // open the window window.Open(); }
/// <summary> /// Called when a request has been completed. Updates the item for the request. /// </summary> /// <param name="request"></param> private void UpdateRequestListBoxItem(NHttpRequest request, NHttpResponse response) { // first clear the boder of all items for (int i = 0; i < m_RequestsListBox.Items.Count; i++) { m_RequestsListBox.Items[i].Border = null; } // highlight the completed item in red NListBoxItem item = m_Request2ListBoxItem[request]; item.Border = NBorder.CreateFilledBorder(NColor.LightCoral); // update the group box header NGroupBox groupBox = (NGroupBox)item.Content; NLabel headerLabel = (NLabel)groupBox.Header.Content; headerLabel.Text += " Response Status: " + response.Status.ToString() + ", Received In: " + (response.ReceivedAt - request.SentAt).TotalSeconds.ToString() + " seconds"; // Disable the Abort button (the first button of the item (first descentant of type button)) NButton abortButton = (NButton)item.GetFirstDescendant(NIsFilter <NNode, NButton> .Instance); abortButton.Enabled = false; // Enable the Headers Button (the last button of the item) NButton headersButton = (NButton)item.GetLastDescendant(NIsFilter <NNode, NButton> .Instance); headersButton.Tag = new object[] { request, response }; headersButton.Enabled = true; }
/// <summary> /// Called by a NHttpRequest when it has been completed. /// </summary> /// <param name="response"></param> private void OnRequestCompleted(NUriRequest request, NUriResponse response) { NHttpRequest httpRequest = (NHttpRequest)request; NHttpResponse httpResponse = (NHttpResponse)response; // update the list box item UpdateRequestListBoxItem(httpRequest, httpResponse); // update the response content holder switch (response.Status) { case ENAsyncResponseStatus.Aborted: // request has been aborted by the user -> do nothing. break; case ENAsyncResponseStatus.Failed: // request has failed -> fill content with an error message m_ResponseContentHolder.Content = new NLabel("Request for URI: " + request.Uri.ToString() + " failed. Error was: " + response.ErrorException.ToString()); break; case ENAsyncResponseStatus.Succeeded: // request succeded -> fill content with the response content try { // get the Content-Type Http Header field, and split it to portions // NOTE: the Content-Type is a multi value field. Values are seperated with the ';' char string contentType = httpResponse.HeaderFields[NHttpHeaderFieldName.ContentType]; string[] contentTypes = contentType.Split(new char[] { ';' }); // normalize content type values (trim and make lower case) for (int i = 0; i < contentTypes.Length; i++) { contentTypes[i] = contentTypes[i].Trim(); contentTypes[i] = contentTypes[i].ToLower(); } // the first part of the content type is the mime type of the content switch (contentTypes[0]) { case "image/png": case "image/jpeg": case "image/bmp": NImage image = new NImage(new NBytesImageSource(response.Data)); NImageBox imageBox = new NImageBox(image); m_ResponseContentHolder.Content = new NScrollContent(imageBox); break; case "text/html": case "application/json": string charSet = (contentTypes.Length >= 1? contentTypes[1]: "charset=utf-8"); string html = ""; switch (charSet) { case "charset=utf-8": html = Nevron.Nov.Text.NEncoding.UTF8.GetString(response.Data); break; default: html = Nevron.Nov.Text.NEncoding.UTF8.GetString(response.Data); break; } NTextBox textBox = new NTextBox(); textBox.Text = html; m_ResponseContentHolder.Content = textBox; break; default: break; } } catch (Exception ex) { m_ResponseContentHolder.Content = new NLabel("Request for URI: " + request.Uri.ToString() + " decoding failed. Error was: " + ex.Message.ToString()); } break; } }