网络挂接在Asp.net MVC
问题描述:
我想接收来自外部API数据,我的asp.net mvc的项目,我想知道如何编写代码来接收来自外部API JSON数据到我的contrller(网络挂接):网络挂接在Asp.net MVC
这是数据:
{
"event": "tracking_update",
"msg": {
"id": "53aa94fc55ece21582000004",
"tracking_number": "906587618687",
"title": "906587618687",
"origin_country_iso3": null,
"destination_country_iso3": null,
"shipment_package_count": 0,
"active": false,
"order_id": null,
"order_id_path": null,
"customer_name": null,
"source": "web",
"emails": [],
"custom_fields": {},
"tag": "Delivered",
"tracked_count": 1,
"expected_delivery": null,
"signed_by": "D Johnson",
"shipment_type": null,
"tracking_account_number": null,
"tracking_postal_code": "DA15BU",
"tracking_ship_date": null,
"created_at": "2014-06-25T09:23:08+00:00",
"updated_at": "2014-06-25T09:23:08+00:00",
"slug": "dx",
"unique_token": "xk7LesjIgg",
"checkpoints": [{
"country_name": null,
"country_iso3": null,
"state": null,
"city": null,
"zip": null,
"message": "Signed For by: D Johnson",
"coordinates": [],
"tag": "Delivered",
"created_at": "2014-06-25T09:23:11+00:00",
"checkpoint_time": "2014-05-02T16:24:38",
"slug": "dx"
}]
},
"ts": 1403688191
}
- 如何在asp.net项目办作为网络挂接从另一个网页API接收JSON数据
答
这是总代码:
[HttpPost]
public ActionResult Index()
{
string FileContent = "";
using (StreamReader sr = new StreamReader(Request.InputStream))
{
FileContent = sr.ReadToEnd();
}
AfterShip model = JsonConvert.DeserializeObject<AfterShip>(FileContent);
}
答
试试这个 - 你需要添加授权头ERS如果需要的话:
using (var httpClient = new HttpClient())
{
try
{
var response = await httpClient.GetAsync(url);
if (response.StatusCode == HttpStatusCode.OK)
{
var responseContent = await response.Content.ReadAsStringAsync();
ObjectThatRepresentsYourJson objectThatRepresentsYourJson = JsonConvert.DeserializeObject<ObjectThatRepresentsYourJson>(responseContent);
}
}
}
答
您可以访问它作为一个输入流
string FileContent = "";
using (StreamReader sr = new StreamReader(Request.InputStream))
{
FileContent = sr.ReadToEnd();
}
和序列化的内容接收。
+0
Bro隐藏的api,比如需要在asp或php中使用webhook页面发送json数据到它时,发货更新或删除或交付 –
我没有URL –
API将JSON数据发送到我的MVC网站当一些事件happend(api是Aftership) –
Aftership API只需要网络挂接页面来获得其json数据 –