예제 #1
0
        protected void callApi_Click(object sender, EventArgs e)
        {
            string uri = requestUri.Text.Trim();//Request.Form["uri"];

            String[] keys = (Request.Form["key[]"] ?? "").Split(','), values = (Request.Form["value[]"] ?? "").Split(',');
            List<KeyValuePair<String, String>> param = new List<KeyValuePair<String, String>>();
            for (int i = 0; i < keys.Length; i++)
            {
                param.Add(new KeyValuePair<string, string>(keys[i], values[i]));
            }

            String[] headerKeys = (Request.Form["headerKey[]"] ?? "").Split(','), headerValues = (Request.Form["headerValue[]"] ?? "").Split(',');
            List<KeyValuePair<String, String>> headers = new List<KeyValuePair<String, String>>();
            for (int i = 0; i < headerKeys.Length; i++)
            {
                headers.Add(new KeyValuePair<string, string>(headerKeys[i], headerValues[i]));
            }

            ApiCallHandler handler = new ApiCallHandler();
            string res = "";
            try {
                switch (method.Text) {
                    case "post":
                        res = handler.postData(uri, param, headers);
                        break;

                    case "get":
                        String query = string.Join("&", param.Select(kvp => string.Format("{0}={1}", kvp.Key, kvp.Value)));
                        res = handler.getData(uri + '?' + query, headers);
                        break;

                    case "put":
                        res = handler.putData(uri, param, headers);
                        break;

                    case "delete":
                        res = handler.deleteData(uri, param, headers);
                        break;
                }

                result.Text = JValue.Parse(res).ToString(Formatting.Indented);
                //resultStatus.InnerText =
                resultSize.InnerText = result.Text.Length + " b";
            }
            catch (Exception ex) { result.Text = ex.Message; result.CssClass = "form-control has-error"; }

            //System.Diagnostics.Debug.WriteLine("uri=" + uri);
            //System.Diagnostics.Debug.WriteLine("query=" + query);
            //System.Diagnostics.Debug.WriteLine("method=" + method);
        }
        private void button_Click(object sender, RoutedEventArgs e)
        {
            ApiCallHandler handler = new ApiCallHandler();
            String address = txtURL.Text.ToString();
            clearStatus();

            try
            {
                if (rbGET.IsChecked == true)
                {

                    txtResponse.Text = JValue.Parse(handler.getData(getParams(), httpHeaders())).ToString(Formatting.Indented);
                }

                else if (rbPOST.IsChecked == true)
                {

                    txtResponse.Text = handler.postData(address, postParams(),httpHeaders());
                }

                else if (rbPUT.IsChecked == true)
                {

                    txtResponse.Text = handler.putData(address, postParams(),httpHeaders());
                }

                else if (rbDELETE.IsChecked == true)
                {
                    txtResponse.Text = handler.deleteData(address, postParams(),httpHeaders());
                }

            }
            catch (Exception ex)
            {
                changeStatus(ex);
            }
        }