/// <summary> /// Post提交数据 /// </summary> /// <param name="model">提交模型</param> private async Task SubmitPostHttpAsync(ParameterModel model) { var clientResult = "<!--" + DateTime.Now.ToDateTimeString() + "-->" + Environment.NewLine; using (var client = new HttpClient()) { try { //添加请求头 client.Headers = client.AddWebHeaders(model.HeadersDic); var langType = Encoding.GetEncoding(model.langType.GetFieldDisplay()); if (this.ckBody.Checked) { var httpContent = new System.Net.Http.StringContent(model.postBody); httpContent.Headers.ContentType = new MediaTypeHeaderValue("text/plains"); var result = await new System.Net.Http.HttpClient().PostAsync(model.postUrl.AbsoluteUri, httpContent); var request = await result.Content.ReadAsStringAsync(); clientResult += request; } else { if (model.IsBinary || model.fileList.Count() > 0) { var form = new HttpClient.MultipartForm(); foreach (var file in model.fileList) { string fileName = file.Substring(file.LastIndexOf('\\') + 1); form.AddFile(fileName, file); } var parArr = model.postBody.Split('&'); foreach (var item in parArr) { if (!item.IsNullOrEmpty()) { var items = item.Split('='); form.Add(items[0], items[1]); } } clientResult += await client.HttpPost(model.postUrl.AbsoluteUri, form, langType); } else { clientResult += await client.HttpPost(model.postUrl.AbsoluteUri, model.postBody, langType); } } } catch (Exception ex) { clientResult += ex.Message; setStatusTxt("异常:" + ex.Message); } tbAppendText(this.tbResult, clientResult + "\r\n"); } }
/// <summary> /// 获取介面提交参数实体 /// </summary> private ParameterModel GetParameterModel() { if (!this.tbUrl.Text.IsUrl()) { MessageBox.Show("提交地址必需为一个完整的Url"); return(null); } var model = new ParameterModel(); var fileList = new List <string>(); foreach (DataGridViewRow item in this.dgvFiles.Rows) { var tempFile = item.Cells[0].Value.ToString(); if (!fileList.Contains(tempFile)) { fileList.Add(tempFile); } } //提交 Headers 参数 Dictionary <string, string> HeadersDic = new Dictionary <string, string>(); foreach (DataGridViewRow item in this.dgvParameter.Rows) { var key = item.Cells[0].Value; if (key != null) { HeadersDic.Add(key.ToString(), item.Cells[1].Value.ToString()); } } model.IsBinary = this.cbIsBinary.Checked; var langType = this.cbLangType.SelectedItem.ToOfType <SelectModel>(); var postType = this.cbPostType.SelectedItem.ToOfType <SelectModel>(); model.HeadersDic = HeadersDic; model.fileList = fileList; model.postBody = this.tbDataBody.Text; model.langType = (LanguageType)langType.Value; model.postType = (PostType)postType.Value; model.postUrl = this.tbUrl.Text.ToUri(); model.IsBodyRaw = this.ckBody.Checked; return(model); }
/// <summary> /// Get提交数据 /// </summary> /// <param name="url"></param> /// <param name="body"></param> /// <param name="langType"></param> private async Task SubmitGetHttpAsync(ParameterModel model) { var clientResult = "<!-- " + DateTime.Now.ToDateTimeString() + " -->" + Environment.NewLine; using (var client = new HttpClient()) { try { var langType = Encoding.GetEncoding(model.langType.GetFieldDisplay()); clientResult += await client.HttpGet(model.postUrl.AbsoluteUri, model.postBody, langType); } catch (Exception ex) { clientResult += ex.Message; setStatusTxt("异常:" + ex.Message); } tbAppendText(this.tbResult, clientResult + "\r\n"); } }