简体   繁体   中英

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. 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 .

 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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