后台数据接口:
[HttpPost]
public Response<List<Schedule>> LoadSchedule([FromForm]QueryScheduleOneListReq query)
{
var result = new Response<List<Schedule>>();
try
{
result.Result = _scheduleApp.Load(query);
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.InnerException?.Message ?? ex.Message;
}
return result;
}
前台请求格式:
public static async Task<string> PostAsync(string url,string data,Dictionary<string,string> header=null,bool Gzip = false)
{
try
{
HttpClient client = new HttpClient(new HttpClientHandler() { UseCookies = false });
HttpContent content = new StringContent(data);
content.Headers.ContentType=new System.Net.Http.Headers.MediaTypeHeaderValue("multipart/form-data");
if (header != null)
{
client.DefaultRequestHeaders.Clear();
foreach (var item in header)
{
client.DefaultRequestHeaders.Add(item.Key, item.Value);
}
}
HttpResponseMessage response = await client.PostAsync(url, content);
response.EnsureSuccessStatusCode();
string responseBody = "";
if (Gzip)
{
GZipInputStream inputStream = new GZipInputStream(await response.Content.ReadAsStreamAsync());
responseBody = new StreamReader(inputStream).ReadToEnd();
}
else
{
responseBody = await response.Content.ReadAsStringAsync();
}
return responseBody;
}
catch (Exception ex)
{
throw;
}
return null;
}
第一个问题请求的时候会抛出异常,把 content.Headers.ContentType=new System.Net.Http.Headers.MediaTypeHeaderValue("multipart/form-data");这行代码去掉,异常就没有了。
第二个问题就是前台的数据提交不上去