简体   繁体   English

c#重定向post请求

[英]c# redirect post request

Trying to make a post request from one application to another using the same Form Data parameters that the first one received.尝试使用第一个应用程序收到的相同表单数据参数从一个应用程序向另一个应用程序发出发布请求。 both application controller currently have same method:两个应用程序控制器目前具有相同的方法:

public async Task<ActionResult> TestSet()
    {
        var inputString = Request.Form["inputString"];
        var inputFile = Request.Files[0];
        var resultString = await _service.Set(inputString, inputFile.FileName, inputFile.ContentType, inputFile.InputStream);
        return new MyJsonResult(new
        {
            fileName = resultString
        });
    }

which return json string:返回json字符串:

{"fileName": "someFileName.png"}

Trying to make the first method to be something like this试图使第一种方法成为这样的

public async Task<ActionResult> TestSet()
    {
        var inputString = Request.Form["inputString"];
        var inputFile = Request.Files[0];
        if (!string.IsNullOrEmpty(_redirectUrl))
        {
            using (var client = new HttpClient())
            {
                HttpContent content = GetContentSomehow(this.Request); // this i have an issue with
                var response = await client.PostAsync(_redirectUrl, content);
                var responseString = await response.Content.ReadAsStringAsync();
                return new MyJsonResult(responseString);
            }
        }
        var resultString = await _service.Set(inputString, inputFile.FileName, inputFile.ContentType, inputFile.InputStream);
        return new MyJsonResult(new
        {
            fileName = resultString
        });
    }

This could help to get ByteArrayContent for File only.可能有助于仅获取 File 的 ByteArrayContent。

And this would probably work to get the non-file parameters into StringContent, but how to get both of them into single Content?这可能可以将非文件参数放入 StringContent,但是如何将它们都放入单个 Content 中?

var jsonString = JsonConvert.SerializeObject(Request.Form.ToDictionary());
var content = new StringContent(jsonString, Encoding.UTF8, "application/json");

Solved this issue by cominig StringContent and StreamContent into MultipartFormDataContent通过将 StringContent 和 StreamContent 合并到 MultipartFormDataContent 中解决了这个问题

public static MultipartFormDataContent GetMultipartFormData(HttpRequestBase req)
    {
        var formData = new MultipartFormDataContent();
        //formData.Headers.ContentType.MediaType = "multipart/form-data";
        foreach (var row in req.Form.ToDictionary())
        {
            formData.Add(new StringContent(row.Value), row.Key);
        }

        var file = req.Files[0];
        StreamContent fileStreamContent = new StreamContent(file.InputStream);
        fileStreamContent.Headers.ContentType = new MediaTypeHeaderValue(file.ContentType);
        formData.Add(fileStreamContent, file.FileName, file.FileName);

        return formData;
    }

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

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