Sai*_*dat 3 c# asp.net-mvc dotnet-httpclient postman
有一个外部 API,我必须向它发布一些对象才能获取一些数据。
我在 Postman 中尝试过,效果很好,如下所示:
我也在用 C# 编写的 ASP.NET MVC 应用程序中进行了尝试:
public class TestingController : SurfaceController
{
[System.Web.Http.HttpGet]
public async System.Threading.Tasks.Task<ActionResult> Hit()
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Order order = new Order();
ClientDetailsModel ClientDetails = new ClientDetailsModel();
ProductDetailsModel ProductDetails = new ProductDetailsModel();
ShippingAdressModel ShippingAdress = new ShippingAdressModel();
ClientDetails.ClientName = "x";
ClientDetails.Email = "x";
ClientDetails.Note = "x";
ClientDetails.Tel = "x";
ProductDetails.ColorID = "1";
ProductDetails.Quantity = "1";
ProductDetails.SizeID = "1";
ProductDetails.ProductMatrixRecordID = "1";
ShippingAdress.City = "x";
ShippingAdress.CountryID = "1";
ShippingAdress.PostalAddress = "x";
ShippingAdress.Street = "x";
ShippingAdress.StreetAddress = "x";
order.ResponseType = "JSON";
order.Token = "P74kXRjM4W44l9qNw8u3";
order.PaymentMode = "1";
order.ProductDetails = ProductDetails;
order.ShippingAddress = ShippingAdress;
order.ClientDetails = ClientDetails;
order.CurrencyAbbreviation = "JOD";
var response = await client.PostAsJsonAsync<Order>("https://bmswebservices.itmam.com/OrderingManagement/Orders.asmx/PlaceOrder", order);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
if (!response.IsSuccessStatusCode)
{
Console.WriteLine("ERROR: Products Not Posted." + response.ReasonPhrase);
return null;
}
var cs = await response.Content.ReadAsAsync<Order>();
return Json(cs);
}
}
Run Code Online (Sandbox Code Playgroud)
我收到 HTTP 500 内部服务器错误:
注意:我对其他 API 使用了相同的方式,但与此不同的是将整个对象发送到服务器。
任何人都可以帮助如何将对象应用于具有内容类型的服务器application/x-www-form-urlencoded?
在邮递员图像中,您正在发布表单数据并获得 json 响应。
在您调用的 c# 代码中PostAsJsonAsync,它将发送 JSON 内容类型。
使用FormUrlEncodedContent并用要发送的数据填充它。
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
//...code omitted for brevity
var order = new Dictionary<string, string>();
order["ResponseType"] = "JSON";
order["Token"] = "P74kXRjM4W44l9qNw8u3";
order["PaymentMode"] = "1";
order["ProductDetails"] = JsonConvert.SerializeObject(ProductDetails);
order["ShippingAddress"] = JsonConvert.SerializeObject(ShippingAdress);
order["ClientDetails"] = JsonConvert.SerializeObject(ClientDetails);
order["CurrencyAbbreviation"] = "JOD";
var content = new FormUrlEncodedContent(order);
var url = "https://bmswebservices.itmam.com/OrderingManagement/Orders.asmx/PlaceOrder";
var response = await client.PostAsync(url, content);
if (!response.IsSuccessStatusCode) {
Console.WriteLine("ERROR: Products Not Posted." + response.ReasonPhrase);
return null;
}
var cs = await response.Content.ReadAsAsync<Order>();
return Json(cs);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8818 次 |
| 最近记录: |