コード例 #1
0
        /// <summary>
        /// Sends the request using the data entered on the Form, and updates the responseBox text field
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void goButton_Click(object sender, EventArgs e)
        {
            //declare the Request, Response and url
            HttpWebRequest webRequest;
            WebResponse webResponse = null; //build this outside the scope of the try block
            string url = urlTextBox.Text;

            try
            {
                //construct the web request from the URL
                webRequest = (HttpWebRequest)WebRequest.Create(url);

                //decide whether it's a GET or POST request
                if (getRadio.Checked)
                {
                    webRequest.Method = "GET";
                }
                else
                {
                    webRequest.Method = "POST";
                }

                //if it's a POST request, we also need to add the POST query data
                if (postRadio.Checked)
                {
                    webRequest.ContentType = "application/x-www-form-urlencoded";
                    webRequest.ContentLength = querystringtextbox.Text.Length;

                    StreamWriter stOut = new StreamWriter(webRequest.GetRequestStream(), System.Text.Encoding.ASCII);
                    stOut.Write(querystringtextbox.Text);
                    stOut.Close();
                }

                //send the request and add the url to the history
                webRequest.KeepAlive = false;
                webResponse = webRequest.GetResponse(); //Assign to webResponse
                history.Add(url);
                Properties.Settings.Default.Save();
            }
            catch (Exception ex)
            {
                responseBox.Text = ex.Message + "\nURL:" + url; //put the error message in the response text box
            }
            finally
            {
                if (webResponse != null) //if you got as far as retrieving a web response that wasn't null, display it in the response text
                {
                    StreamReader sr = new StreamReader(webResponse.GetResponseStream());
                    string strRead = sr.ReadToEnd();

                    if (jsonCheckbox.Checked) //if the Json checkbox was checked, format it as JSON using the JsonFormatter by
                    {
                        JsonFormatter formatter = new JsonFormatter();
                        responseBox.Text = formatter.PrettyPrint(strRead);
                    }
                    else //otherwise if the json checkbox isn't checked, just put the plaintext into the response box
                    {
                        responseBox.Text = strRead;
                    }

                    webResponse.Close(); //Close webresponse connection
                }
                webResponse = null;
                webRequest = null;
            }
        }
コード例 #2
0
        /// <summary>
        /// Sends the request using the data entered on the Form, and updates the responseBox text field
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void goButton_Click(object sender, EventArgs e)
        {
            //declare the Request, Response and url
            HttpWebRequest webRequest;
            WebResponse    webResponse = null; //build this outside the scope of the try block
            string         url         = urlTextBox.Text;

            try
            {
                //construct the web request from the URL
                webRequest = (HttpWebRequest)WebRequest.Create(url);

                //decide whether it's a GET or POST request
                if (getRadio.Checked)
                {
                    webRequest.Method = "GET";
                }
                else
                {
                    webRequest.Method = "POST";
                }

                //if it's a POST request, we also need to add the POST query data
                if (postRadio.Checked)
                {
                    webRequest.ContentType   = "application/x-www-form-urlencoded";
                    webRequest.ContentLength = querystringtextbox.Text.Length;

                    StreamWriter stOut = new StreamWriter(webRequest.GetRequestStream(), System.Text.Encoding.ASCII);
                    stOut.Write(querystringtextbox.Text);
                    stOut.Close();
                }

                //send the request and add the url to the history
                webRequest.KeepAlive = false;
                webResponse          = webRequest.GetResponse(); //Assign to webResponse
                history.Add(url);
                Properties.Settings.Default.Save();
            }
            catch (Exception ex)
            {
                responseBox.Text = ex.Message + "\nURL:" + url; //put the error message in the response text box
            }
            finally
            {
                if (webResponse != null) //if you got as far as retrieving a web response that wasn't null, display it in the response text
                {
                    StreamReader sr      = new StreamReader(webResponse.GetResponseStream());
                    string       strRead = sr.ReadToEnd();

                    if (jsonCheckbox.Checked) //if the Json checkbox was checked, format it as JSON using the JsonFormatter by
                    {
                        JsonFormatter formatter = new JsonFormatter();
                        responseBox.Text = formatter.PrettyPrint(strRead);
                    }
                    else //otherwise if the json checkbox isn't checked, just put the plaintext into the response box
                    {
                        responseBox.Text = strRead;
                    }

                    webResponse.Close(); //Close webresponse connection
                }
                webResponse = null;
                webRequest  = null;
            }
        }