Exemple #1
0
        private async void btnGO_Click(object sender, EventArgs e)
        {
            txtResponse.ReadOnly           = false;
            txtResponse.Lexer              = Lexer.Container;
            txtResponse.Text               = string.Empty;
            txtResponse.ReadOnly           = true;
            gridResponseHeaders.DataSource = new BindingList <Header>();
            var reqResp = new RequestResponse();

            reqResp.URL    = txtURL.Text;
            reqResp.Method = cboMethod.Text;
            if (reqResp.Method.ToLower() != "get")
            {
                reqResp.RequestBody = txtRequestBody.Text;
            }
            reqResp.RequestDate           = DateTime.Now;
            reqResp.StatusCode            = -1;
            reqResp.UseDefaultCredentials = chkUseDefaultCredentials.Checked;
            (gridRequestsResponses.DataSource as BindingList <RequestResponse>).Insert(0, reqResp);
            gridRequestsResponses.ClearSelection();
            gridRequestsResponses.Rows[0].Selected = true;
            HttpWebResponse response  = null;
            Exception       exception = null;
            DateTime?       start     = null;

            progMain.Style = ProgressBarStyle.Marquee;
            try
            {
                var req = WebRequest.Create(txtURL.Text) as HttpWebRequest;
                req.UserAgent             = "PaJaMa WebRequestor";
                req.Accept                = "*/*";
                req.KeepAlive             = true;
                req.Method                = cboMethod.Text;
                req.UseDefaultCredentials = chkUseDefaultCredentials.Checked;
                foreach (DataGridViewRow hdr in gridRequestHeaders.Rows)
                {
                    if (hdr.DataBoundItem == null)
                    {
                        continue;
                    }
                    var header = (hdr.DataBoundItem as Header).Clone();
                    reqResp.RequestHeaders.Add(header);
                    if (header.Name.ToLower() == "content-type")
                    {
                        req.ContentType = header.Value;
                    }
                    else
                    {
                        req.Headers.Add(header.Name, header.Value);
                    }
                }
                start = DateTime.Now;
                if (cboMethod.Text != "GET" && !string.IsNullOrEmpty(txtRequestBody.Text))
                {
                    var stream = req.GetRequestStream();
                    var bytes  = Common.Common.GetBytesFromString(txtRequestBody.Text);
                    stream.Write(bytes, 0, bytes.Length);
                    stream.Close();
                }
                response = (HttpWebResponse)await req.GetResponseAsync();
            }
            catch (WebException we)
            {
                response = (HttpWebResponse)we.Response;
                if (response == null)
                {
                    exception = we;
                }
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            progMain.Style = ProgressBarStyle.Continuous;

            if (start != null)
            {
                reqResp.Duration = (int)Math.Round((DateTime.Now - start.Value).TotalMilliseconds);
            }

            if (exception != null)
            {
                reqResp.StatusCode = 0;
                writeResponse(reqResp, exception.Message);
            }
            else if (response != null)
            {
                reqResp.StatusCode = (int)response.StatusCode;
                writeResponse(reqResp, Common.Common.GetStringFromStream(response.GetResponseStream()).Trim('\0').Trim());
                foreach (var k in response.Headers.AllKeys)
                {
                    reqResp.ResponseHeaders.Add(new Header()
                    {
                        Name = k, Value = response.Headers[k]
                    });
                }

                gridResponseHeaders.DataSource = reqResp.ResponseHeaders;
            }

            gridRequestsResponses.Invalidate();
            Workspace.RequestResponses.Insert(0, reqResp);
            File.WriteAllText(WorkspacePath + ".json", JsonConvert.SerializeObject(Workspace));
            _current = reqResp.Clone();
            duplicate(_current);
            populateOutputControl(_current);

            tabRequestResponse.SelectedTab = pageResponse;
            setTabText();
        }