从 ASP.NET MVC 项目中的 C# 代码向另一个网站发送 JSON 请求
Send a JSON request to another web site from C# code behind in an ASP.NET MVC project
我正在使用 ASP.NET MVC(后端是 C#),我正在尝试发送一个看起来像这样的 json :
{
"store_id": "store3",
"api_token": "yesguy",
"checkout_id": "UniqueNumber",
"txn_total": "10.00",
"environment": "qa",
"action": "preload"
}
到另一个网站,假设它是这样的:
https://TestGate.paimon.com/chkt/request/request.php
通过一些研究我发现了这个:
看起来不错,但我没有在核心工作,我的项目只是普通的 ASP.NET MVC。我不知道如何使用 json 函数将其发送到网站。
这是我尝试过的方法(在 Liad Dadon 回答的启发下更新):
public ActionResult Index(int idInsc)
{
INSC_Inscription insc = GetMainModelInfos(idinsc);
JsonModel jm = new JsonModel();
jm.store_id = "store2";
jm.api_token = "yesguy";
jm.checkout_id = "uniqueId";
jm.txn_total = "123.00";
jm.environment = "qa";
jm.action = "preload";
var jsonObject = JsonConvert.SerializeObject(jm);
var url = "https://gatewayt.whatever.com/chkt/request/request.php";
HttpClient client = new HttpClient();
var content = new StringContent(jsonObject, System.Text.Encoding.UTF8, "application/json");
System.Threading.Tasks.Task<HttpResponseMessage> res = client.PostAsync(url, content);
insc.response = res.Result; // This cause an exeption
return View(insc);
}
当 Json 正确发布后,其他网站将用自己的 Json 回答我:
{
"response" :
{
"success": "true",
"ticket": "Another_Long_And_Unique_Id_From_The_Other_Web_Site"
}
}
我需要做的就是检索这个Json答案,有了它,剩下的就是小菜一碟了。
信息:
在 PostAsync
函数之后,var res 包含这个:
这就是我如何使用 Newtonsoft.Json 包、HttpClient 和 StringContent post JSON 对象到某个地方 类:
using Newtonsoft.Json;
var object = new Model
{
//your properties
}
var jsonObject = JsonConvert.SerializeObject(object);
var url = "http://yoururl.com/endpoint"; //<- your url here
try
{
using HttpClient client = new();
var content = new StringContent(jsonObject , Encoding.UTF8,
"application/json");
var res = await client.PostAsync(url, content);
}
请确保您的函数是异步的并且您等待 client.PostAsync 函数。
您似乎没有正确处理 asynchronous
任务 — 您看到的 WaitingForActivation
消息实际上不是我们 API 的回复你的任务状态。
任务正在等待 .NET Framework 基础结构在内部激活和安排。
看来您可能需要等待⁽²⁾ task to ensure it completes or access the response with await client.PostAsync(url, content);
. for adding await 您需要将 async
添加到控制器⁽¹⁾ 操作。
public async Task<ActionResult> Index(int idInsc) //Change here [1]
{
INSC_Inscription insc = GetMainModelInfos(idinsc);
JsonModel jm = new JsonModel();
jm.store_id = "store2";
jm.api_token = "yesguy";
jm.checkout_id = "uniqueId";
jm.txn_total = "123.00";
jm.environment = "qa";
jm.action = "preload";
var jsonObject = JsonConvert.SerializeObject(jm);
var url = "https://gatewayt.whatever.com/chkt/request/request.php";
HttpClient client = new HttpClient();
var content = new StringContent(jsonObject, System.Text.Encoding.UTF8, "application/json");
System.Threading.Tasks.Task<HttpResponseMessage> res = await client.PostAsync(url, content); //Change here [2]
insc.response = res.Result; // This cause an exeption
return View(insc);
}
我正在使用 ASP.NET MVC(后端是 C#),我正在尝试发送一个看起来像这样的 json :
{
"store_id": "store3",
"api_token": "yesguy",
"checkout_id": "UniqueNumber",
"txn_total": "10.00",
"environment": "qa",
"action": "preload"
}
到另一个网站,假设它是这样的:
https://TestGate.paimon.com/chkt/request/request.php
通过一些研究我发现了这个:
看起来不错,但我没有在核心工作,我的项目只是普通的 ASP.NET MVC。我不知道如何使用 json 函数将其发送到网站。
这是我尝试过的方法(在 Liad Dadon 回答的启发下更新):
public ActionResult Index(int idInsc)
{
INSC_Inscription insc = GetMainModelInfos(idinsc);
JsonModel jm = new JsonModel();
jm.store_id = "store2";
jm.api_token = "yesguy";
jm.checkout_id = "uniqueId";
jm.txn_total = "123.00";
jm.environment = "qa";
jm.action = "preload";
var jsonObject = JsonConvert.SerializeObject(jm);
var url = "https://gatewayt.whatever.com/chkt/request/request.php";
HttpClient client = new HttpClient();
var content = new StringContent(jsonObject, System.Text.Encoding.UTF8, "application/json");
System.Threading.Tasks.Task<HttpResponseMessage> res = client.PostAsync(url, content);
insc.response = res.Result; // This cause an exeption
return View(insc);
}
当 Json 正确发布后,其他网站将用自己的 Json 回答我:
{
"response" :
{
"success": "true",
"ticket": "Another_Long_And_Unique_Id_From_The_Other_Web_Site"
}
}
我需要做的就是检索这个Json答案,有了它,剩下的就是小菜一碟了。
信息:
在 PostAsync
函数之后,var res 包含这个:
这就是我如何使用 Newtonsoft.Json 包、HttpClient 和 StringContent post JSON 对象到某个地方 类:
using Newtonsoft.Json;
var object = new Model
{
//your properties
}
var jsonObject = JsonConvert.SerializeObject(object);
var url = "http://yoururl.com/endpoint"; //<- your url here
try
{
using HttpClient client = new();
var content = new StringContent(jsonObject , Encoding.UTF8,
"application/json");
var res = await client.PostAsync(url, content);
}
请确保您的函数是异步的并且您等待 client.PostAsync 函数。
您似乎没有正确处理 asynchronous
任务 — 您看到的 WaitingForActivation
消息实际上不是我们 API 的回复你的任务状态。
任务正在等待 .NET Framework 基础结构在内部激活和安排。
看来您可能需要等待⁽²⁾ task to ensure it completes or access the response with await client.PostAsync(url, content);
. for adding await 您需要将 async
添加到控制器⁽¹⁾ 操作。
public async Task<ActionResult> Index(int idInsc) //Change here [1]
{
INSC_Inscription insc = GetMainModelInfos(idinsc);
JsonModel jm = new JsonModel();
jm.store_id = "store2";
jm.api_token = "yesguy";
jm.checkout_id = "uniqueId";
jm.txn_total = "123.00";
jm.environment = "qa";
jm.action = "preload";
var jsonObject = JsonConvert.SerializeObject(jm);
var url = "https://gatewayt.whatever.com/chkt/request/request.php";
HttpClient client = new HttpClient();
var content = new StringContent(jsonObject, System.Text.Encoding.UTF8, "application/json");
System.Threading.Tasks.Task<HttpResponseMessage> res = await client.PostAsync(url, content); //Change here [2]
insc.response = res.Result; // This cause an exeption
return View(insc);
}