public static string RequestGet(RestInfo restInfo) { string result = ""; if (restInfo == null) { return(result); } string path = string.Format("{0}?{1}", restInfo.Path, restInfo.GetQuery()); if (string.IsNullOrEmpty(restInfo.Param)) { path = string.Format("{0}", restInfo.Path); } HttpWebRequest request = (HttpWebRequest)WebRequest.Create(restInfo.Path); request.Method = "GET"; request.Accept = "*/*"; request.ContentType = "application/x-www-form-urlencoded"; // response WebResponse response = request.GetResponse(); StreamReader streamReader = new StreamReader(response.GetResponseStream()); return(result); }
private void btnAdd_Click(object sender, EventArgs e) { string path = textBoxNewUrl.Text; if (string.IsNullOrEmpty(path)) { MessageBox.Show("Can't be Empty!"); return; } // add RestInfo restInfo = new RestInfo(); restInfo.Path = path; string method = comboNewMethod.SelectedItem.ToString(); restInfo.SetRequestMethod(method); _arrListRestInfo.Add(restInfo); // init textbox MessageBox.Show("Url was added!"); textBoxNewUrl.Text = ""; // refresh refreshUrls(); }
public string SendRequest(RestInfo restInfo) { string result = ""; if (restInfo == null) { return(result); } if (restInfo.RequestMethod == DEF_REQUEST_METHOD.REQUEST_GET) { try { Task <string> task = RequestGetAsync(restInfo); task.Wait(); return(task.Result); } catch (Exception e) { return(e.Message); } } else { try { Task <string> task = RequestPostAsync(restInfo); task.Wait(); return(task.Result); } catch (Exception e) { return(e.Message); } } }
private void buttonSend_Click(object sender, EventArgs e) { string path = textboxUrl.Text; string method = comboMethod.SelectedItem.ToString(); if (string.IsNullOrEmpty(path)) { MessageBox.Show("Can't be empty"); return; } if (_curRestInfo == null) { _curRestInfo = new RestInfo(); } // send request _curRestInfo.Path = path; _curRestInfo.SetRequestMethod(method); string param = KeyInfo.GetJson(_arrListKeys); _curRestInfo.Param = param; string result = RequestFunc.GetInstance().SendRequest(_curRestInfo); // set result textBoxResult.Text = result; _curRestInfo.Result = result; }
private void Test() { RestInfo restInfo = new RestInfo(); restInfo.Param = "access_token=6310970b75cb92744607755654b774db3e7b9bac"; restInfo.Path = "https://api.particle.io/v1/devices"; restInfo.RequestMethod = DEF_REQUEST_METHOD.REQUEST_POST; }
private async Task <string> RequestGetAsync(RestInfo restInfo) { string path = string.Format("{0}?{1}", restInfo.Path, restInfo.GetQuery()); if (string.IsNullOrEmpty(restInfo.Param)) { path = string.Format("{0}", restInfo.Path); } var response = await _httpClient.GetStringAsync(path); //JContainer jsonResult = JObject.Parse(response); return(response); }
private async Task <string> RequestPostAsync(RestInfo restInfo) { var requestMessage = new HttpRequestMessage(HttpMethod.Post, restInfo.Path); var data = restInfo.GetFormUrlEncodedContent(); if (data != null) { requestMessage.Content = new FormUrlEncodedContent(data); } var response = await _httpClient.SendAsync(requestMessage).ConfigureAwait(false); var responseString = await response.Content.ReadAsStringAsync(); //JContainer jsonResult = JObject.Parse(responseString); return(responseString); }
public static string RequestPost1(RestInfo restInfo) { string result = ""; if (restInfo == null) { return(result); } string path = string.Format("{0}", restInfo.Path); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(restInfo.Path); //CookieContainer cookieContainer = new CookieContainer(); //request.CookieContainer = cookieContainer; request.UserAgent = _userAgent; request.Method = "POST"; request.Accept = "*/*"; request.ContentType = "application/x-www-form-urlencoded"; StreamWriter requestWriter = new StreamWriter(request.GetRequestStream()); requestWriter.Write(restInfo.GetQuery()); requestWriter.Close(); // response try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader streamReader = new StreamReader(response.GetResponseStream()); result = streamReader.ReadToEnd(); } catch (WebException ex) { using (var stream = ex.Response.GetResponseStream()) using (var reader = new StreamReader(stream)) { result = reader.ReadToEnd(); } } catch (Exception e) { result = "network error"; } return(result); }
private void comboSelectUrl_SelectedIndexChanged(object sender, EventArgs e) { int index = comboSelectUrl.SelectedIndex; if (index < 0) { return; } _curRestInfo = _arrListRestInfo[index]; if (_curRestInfo == null) { return; } // set by selected url textboxUrl.Text = _curRestInfo.Path; if (_curRestInfo.RequestMethod == DEF_REQUEST_METHOD.REQUEST_GET) { comboMethod.SelectedItem = RequestFunc.DEF_METHOD_GET; } else { comboMethod.SelectedItem = RequestFunc.DEF_METHOD_POST; } // set param BindingList <KeyInfo> arrListKeys = KeyInfo.GetBindingList(_curRestInfo.Param); _arrListKeys.Clear(); if (arrListKeys != null) { foreach (KeyInfo keyInfo in arrListKeys) { _arrListKeys.Add(keyInfo); } } // set result textBoxResult.Text = _curRestInfo.Result; }