コード例 #1
0
ファイル: MinPost.cs プロジェクト: intotf/net_learning_demo
        /// <summary>
        /// Post提交数据
        /// </summary>
        /// <param name="model">提交模型</param>
        private void SubmitPostHttp(ParameterModel model)
        {
            var clientResult = DateTime.Now.ToDateTimeString() + "\r\n";

            using (var client = new HttpClient())
            {
                try
                {   //添加请求头
                    client.Headers = client.AddWebHeaders(model.HeadersDic);

                    var langType = Encoding.GetEncoding(model.langType.GetFieldDisplay());
                    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 += client.HttpPost(model.postUrl.AbsoluteUri, form, langType);
                    }
                    else
                    {
                        clientResult += client.HttpPost(model.postUrl.AbsoluteUri, model.postBody, langType);
                    }
                }
                catch (Exception ex)
                {
                    clientResult += ex.Message;
                    setStatusTxt("异常:" + ex.Message);
                }
                tbAppendText(this.tbResult, clientResult + "\r\n");
            }
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: intotf/net_learning_demo
        /// <summary>
        /// Post提交数据
        /// </summary>
        /// <param name="url">提交地址</param>
        /// <param name="body">提交内容</param>
        /// <param name="langType">提交编码</param>
        /// <param name="fileList">文件列表</param>
        private void SubmitPostHttp(string url, string body, Encoding langType, List <string> fileList)
        {
            var clientResult = DateTime.Now.ToDateTimeString() + "\r\n";

            using (var client = new HttpClient())
            {
                try
                {
                    if (fileList.Count() > 0)
                    {
                        var form = new HttpClient.MultipartForm();
                        foreach (var file in fileList)
                        {
                            string fileName = file.Substring(file.LastIndexOf('\\') + 1);
                            form.AddFile(fileName, file);
                            var parArr = body.Split('&');
                            foreach (var item in parArr)
                            {
                                if (!item.IsNullOrEmpty())
                                {
                                    var items = item.Split('=');
                                    form.Add(items[0], items[1]);
                                }
                            }
                        }
                        clientResult += client.HttpPost(url, form, langType);
                    }
                    else
                    {
                        clientResult += client.HttpPost(url, body, langType);
                    }
                }
                catch (Exception ex)
                {
                    clientResult += ex.Message;
                    setStatusTxt("异常:" + ex.Message);
                }
                tbAppendText(this.tbResult, clientResult + "\r\n");
            }
        }