简体   繁体   English

在请求正文中发送参数和文件,带有源请求 Header C#

[英]Send parameters & file in Request Body with Origin Request Header C#

I am trying to send parameters & file in Request Body with Origin Request Header C# Console / Asp.Net (Web Form) Web Application.我正在尝试使用原始请求 Header C# 控制台/Asp.Net(Web 表单)Web 应用程序在请求正文中发送参数和文件。 I have looked around many examples but I didn't get proper solutions.我查看了许多示例,但没有得到适当的解决方案。

I've tried with the following code which is almost working.我已经尝试使用以下几乎可以正常工作的代码。 The only issue with couldn't get any solution to send the file in Request Body .唯一的问题是无法在 Request Body 中发送文件

 public class requestObj
        {
            public string langType { get; set; }
        }

protected void CreateUser_Click(object sender, EventArgs e)
        {
            try
            {           
                var requestObj = new requestObj
                {
                    langType = "aaa"
                };

                var client = new RestSharp.RestClient(url);
                client.Timeout = -1;
                var request = new RestSharp.RestRequest(RestSharp.Method.POST);
                request.AddHeader("Origin", "http://localhost:8080");
                request.AddJsonBody(requestObj);
                var response = client.Execute(request);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Thanks,谢谢,

I have made it working following ways,我已经使它按照以下方式工作,

string url = requestUrl;
string filePath = @"F:\text.txt";


 var client = new RestSharp.RestClient(url);
                    client.Timeout = -1;
                    var request = new RestRequest(Method.POST);
                    request.AddHeader("Origin", "http://localhost:2020");
                    request.AddParameter("parameter", "parameterValue");
                    request.AddFile("file", filePath, "text/plain"); //file parameter.
                    IRestResponse response = client.Execute(request);
                    Console.WriteLine(response.Content);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM