I am posting data and files from HttpClient, it working properly when i used localhost url of the Web API, but it is returning 400 when i use hosted url.
public IActionResult AddABCDocument([FromForm] long DocumentTypeID, string DocumentName, string FileName, string DocumentNumber, long StateID, string IssueDate, string ExpirationDate, List<IFormFile>? Files)
{
if (ModelState.IsValid)
{
managedmodel modeldoc = new managedmodel ();
modeldoc.UserID = Convert.ToString(TempData["ProviderId"]).ToUpper();
modeldoc.DocumentTypeID = DocumentTypeID;
modeldoc.DocumentName = DocumentName;
modeldoc.DocumentNumber = DocumentNumber;
modeldoc.StateID = StateID;
modeldoc.IssueDate = dtstartdate;
modeldoc.ExpirationDate = dteradate;
modeldoc.Files = Files;
modeldoc.FileName = FileName;
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", HttpContext.Request.Cookies["Token"].ToString());
byte[] data;
foreach (var file in Files)
{
if (file.Length > 0)
{
using (var ms = new MemoryStream())
{
file.CopyTo(ms);
data = ms.ToArray();
ByteArrayContent bytes = new ByteArrayContent(data);
MultipartFormDataContent multiContent = new MultipartFormDataContent();
multiContent.Add(bytes, "Files", modeldoc.FileName);
multiContent.Add(new StringContent(modeldoc.DocumentTypeID.ToString()), "DocumentTypeID");
multiContent.Add(new StringContent(modeldoc.DocumentName.ToString()), "DocumentName");
multiContent.Add(new StringContent(modeldoc.DocumentNumber.ToString()), "DocumentNumber");
multiContent.Add(new StringContent(modeldoc.StateID.ToString()), "StateID");
multiContent.Add(new StringContent(modeldoc.ExpirationDate.ToString()), "ExpirationDate");
multiContent.Add(new StringContent(modeldoc.IssueDate.ToString()), "IssueDate");
multiContent.Add(new StringContent(modeldoc.UserID.ToString()), "UserID");
var Response = client.PostAsync(new Uri("http://localhost:5179/api/abc/docadd"), multiContent).Result;(working)
var Response = client.PostAsync(new Uri("http://9.9.82.188:3033/api/abc/docadd"), multiContent).Result;(not working)
}
}
}
//return Json(Response.message);
if (Response.StatusCode==200)
{
TempData["ProviderId"] = modeldoc.UserID;
return Json(Response.StatusCode);
}
else
{
ModelState.AddModelError("CustomError", Response.ToString());
return Json(Response);
}
}
else
{
return View();
}
}