【发布时间】:2020-09-12 03:33:35
【问题描述】:
我是 C# 新手,我想以 JSON 格式向 API 发送请求并获得 JSON 响应作为回报,以便我可以在我的应用程序中使用它。但是每次我提出请求时,都会收到 404 错误。当我使用相同的 POST 数据执行 Postman 请求时,我得到了肯定的结果。
下面是我使用 Postman 发送到 API 的 JSON。
{
"ActionId": null,
"AgentCode": "MerchantCode:Password",
"Mpin": null,
"Amount": 0.0,
"CustomerMSISDN": null,
"ServiceId": 0,
"MTI": "0300",
"TerminalId": null,
"TransactionRef": null,
"CustomerAccount": null,
"CustomerData": null,
"Product": null,
"ServiceProvider": "Yomoney",
"Source": null,
"PaymentMethod": null,
"ProcessingCode": "420000",
"Quantity": null,
"OrderLines": null,
"Narrative": null,
"Note": null,
"MaxSale": 0.0,
"MinSale": 0.0,
"TransactionType": 2
}
这是我要发送到的 URL:
https://www.yomoneyservice.com/yoclient/transaction
这是我得到的回应
{"ResponseCode":"00000","Description":null,"Balance":null,"TransactionCode":null,"vouchers":null,"AgentCode":null,"Mpin":null,"Amount":0,"MaxSale":0,"MinSale":0,"CustomerMSISDN":null,"ServiceId":0,"MTI":null,"TerminalId":null,"TransactionRef":null,"TransactionType":0,"CustomerAccount":null,"CustomerData":null,"Product":null,"Quantity":0,"Action":null,"ProcessingCode":null,"Note":"Purchase","Narrative":"[{\"Id\":\"1\",\"Name\":\"THE
REDEEMED
SOLDIER\",\"Value\":\"12\",\"Note\":\"Supplier\",\"Image\":null,\"Description\":null,\"Section\":null,\"ServiceId\":12,\"Currency\":null,\"ActionId\":1,\"TransactionType\":0,\"SupplierId\":null,\"Amount\":null,\"Count\":0,\"date\":\"0001-01-01T00:00:00\",\"WebLink\":null,\"HasProducts\":false,\"IsAdvert\":false,\"IsNotAdvert\":false,\"IsShare\":false,\"RequireVerification\":false,\"RequiresRegistration\":false,\"RequiresAmount\":false},{\"Id\":\"1\",\"Name\":\"BUY
VOUCHER\",\"Value\":\"10\",\"Note\":\"Supplier\",\"Image\":\"~/Content/Logos/YOMONEY/yomoney_v_logo.png\",\"Description\":null,\"Section\":null,\"ServiceId\":10,\"Currency\":null,\"ActionId\":1,\"TransactionType\":0,\"SupplierId\":null,\"Amount\":null,\"Count\":0,\"date\":\"0001-01-01T00:00:00\",\"WebLink\":null,\"HasProducts\":false,\"IsAdvert\":false,\"IsNotAdvert\":false,\"IsShare\":false,\"RequireVerification\":false,\"RequiresRegistration\":false,\"RequiresAmount\":false},{\"Id\":\"1\",\"Name\":\"ECONET
PINLLESS\",\"Value\":\"8\",\"Note\":\"Supplier\",\"Image\":\"~/Content/Logos/YOMONEY/buddie.png\",\"Description\":null,\"Section\":null,\"ServiceId\":8,\"Currency\":null,\"ActionId\":1,\"TransactionType\":0,\"SupplierId\":null,\"Amount\":null,\"Count\":0,\"date\":\"0001-01-01T00:00:00\",\"WebLink\":null,\"HasProducts\":false,\"IsAdvert\":false,\"IsNotAdvert\":false,\"IsShare\":false,\"RequireVerification\":false,\"RequiresRegistration\":false,\"RequiresAmount\":false},{\"Id\":\"1\",\"Name\":\"TELECEL
PINLESS\",\"Value\":\"3\",\"Note\":\"Supplier\",\"Image\":null,\"Description\":null,\"Section\":null,\"ServiceId\":3,\"Currency\":null,\"ActionId\":1,\"TransactionType\":0,\"SupplierId\":null,\"Amount\":null,\"Count\":0,\"date\":\"0001-01-01T00:00:00\",\"WebLink\":null,\"HasProducts\":false,\"IsAdvert\":false,\"IsNotAdvert\":false,\"IsShare\":false,\"RequireVerification\":false,\"RequiresRegistration\":false,\"RequiresAmount\":false},{\"Id\":\"1\",\"Name\":\"Econet\",\"Value\":\"2\",\"Note\":\"Supplier\",\"Image\":null,\"Description\":null,\"Section\":null,\"ServiceId\":2,\"Currency\":null,\"ActionId\":1,\"TransactionType\":0,\"SupplierId\":null,\"Amount\":null,\"Count\":0,\"date\":\"0001-01-01T00:00:00\",\"WebLink\":null,\"HasProducts\":true,\"IsAdvert\":false,\"IsNotAdvert\":false,\"IsShare\":false,\"RequireVerification\":false,\"RequiresRegistration\":false,\"RequiresAmount\":false},{\"Id\":\"1\",\"Name\":\"NETONE
PINLESS\",\"Value\":\"1\",\"Note\":\"Supplier\",\"Image\":null,\"Description\":null,\"Section\":null,\"ServiceId\":1,\"Currency\":null,\"ActionId\":1,\"TransactionType\":0,\"SupplierId\":null,\"Amount\":null,\"Count\":0,\"date\":\"0001-01-01T00:00:00\",\"WebLink\":null,\"HasProducts\":false,\"IsAdvert\":false,\"IsNotAdvert\":false,\"IsShare\":false,\"RequireVerification\":false,\"RequiresRegistration\":false,\"RequiresAmount\":false},{\"Id\":\"1\",\"Name\":\"THE
REDEEMED
SOLDIER\",\"Value\":\"12\",\"Note\":\"Supplier\",\"Image\":null,\"Description\":null,\"Section\":null,\"ServiceId\":12,\"Currency\":null,\"ActionId\":1,\"TransactionType\":0,\"SupplierId\":null,\"Amount\":null,\"Count\":0,\"date\":\"0001-01-01T00:00:00\",\"WebLink\":null,\"HasProducts\":false,\"IsAdvert\":false,\"IsNotAdvert\":false,\"IsShare\":false,\"RequireVerification\":false,\"RequiresRegistration\":false,\"RequiresAmount\":false},{\"Id\":\"1\",\"Name\":\"BUY
VOUCHER\",\"Value\":\"10\",\"Note\":\"Supplier\",\"Image\":\"~/Content/Logos/YOMONEY/yomoney_v_logo.png\",\"Description\":null,\"Section\":null,\"ServiceId\":10,\"Currency\":null,\"ActionId\":1,\"TransactionType\":0,\"SupplierId\":null,\"Amount\":null,\"Count\":0,\"date\":\"0001-01-01T00:00:00\",\"WebLink\":null,\"HasProducts\":false,\"IsAdvert\":false,\"IsNotAdvert\":false,\"IsShare\":false,\"RequireVerification\":false,\"RequiresRegistration\":false,\"RequiresAmount\":false},{\"Id\":\"1\",\"Name\":\"ECONET
PINLLESS\",\"Value\":\"8\",\"Note\":\"Supplier\",\"Image\":\"~/Content/Logos/YOMONEY/buddie.png\",\"Description\":null,\"Section\":null,\"ServiceId\":8,\"Currency\":null,\"ActionId\":1,\"TransactionType\":0,\"SupplierId\":null,\"Amount\":null,\"Count\":0,\"date\":\"0001-01-01T00:00:00\",\"WebLink\":null,\"HasProducts\":false,\"IsAdvert\":false,\"IsNotAdvert\":false,\"IsShare\":false,\"RequireVerification\":false,\"RequiresRegistration\":false,\"RequiresAmount\":false},{\"Id\":\"1\",\"Name\":\"TELECEL
PINLESS\",\"Value\":\"3\",\"Note\":\"Supplier\",\"Image\":null,\"Description\":null,\"Section\":null,\"ServiceId\":3,\"Currency\":null,\"ActionId\":1,\"TransactionType\":0,\"SupplierId\":null,\"Amount\":null,\"Count\":0,\"date\":\"0001-01-01T00:00:00\",\"WebLink\":null,\"HasProducts\":false,\"IsAdvert\":false,\"IsNotAdvert\":false,\"IsShare\":false,\"RequireVerification\":false,\"RequiresRegistration\":false,\"RequiresAmount\":false},{\"Id\":\"1\",\"Name\":\"Econet\",\"Value\":\"2\",\"Note\":\"Supplier\",\"Image\":null,\"Description\":null,\"Section\":null,\"ServiceId\":2,\"Currency\":null,\"ActionId\":1,\"TransactionType\":0,\"SupplierId\":null,\"Amount\":null,\"Count\":0,\"date\":\"0001-01-01T00:00:00\",\"WebLink\":null,\"HasProducts\":true,\"IsAdvert\":false,\"IsNotAdvert\":false,\"IsShare\":false,\"RequireVerification\":false,\"RequiresRegistration\":false,\"RequiresAmount\":false},{\"Id\":\"1\",\"Name\":\"NETONE
PINLESS\",\"Value\":\"1\",\"Note\":\"Supplier\",\"Image\":null,\"Description\":null,\"Section\":null,\"ServiceId\":1,\"Currency\":null,\"ActionId\":1,\"TransactionType\":0,\"SupplierId\":null,\"Amount\":null,\"Count\":0,\"date\":\"0001-01-01T00:00:00\",\"WebLink\":null,\"HasProducts\":false,\"IsAdvert\":false,\"IsNotAdvert\":false,\"IsShare\":false,\"RequireVerification\":false,\"RequiresRegistration\":false,\"RequiresAmount\":false}]"}
现在,在 C# 中,我正在使用 WebRequest&WebResponse,但我收到 404 错误,提示“远程服务器返回错误:(404) 未找到。”。下面是我的 C# 代码
try
{
string url = String.Format("https://www.yomoneyservice.com/yoclient/transaction");
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = JsonConvert.SerializeObject(yomoneyRequest, Formatting.Indented);
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
yomoneyResponse = JsonConvert.DeserializeObject<YomoneyResponse>(result);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
我不再确定我的 C# 代码哪里出错了,谁能帮帮我?
【问题讨论】:
标签: c# httprequest httpresponse system.net.httpwebrequest