【发布时间】:2016-06-03 06:42:54
【问题描述】:
我确实有一个硬编码 json,我必须使用 post 在 http 请求中发送它, 这是json
{
"RequestHeader": {
"UserName": " ",
"Password": " "
},
"RequestBody": {
"ChannelType": 1,
"BillAccountNumber": "1075-001",
"BillAccountType": null,
"PaymentAmount": 15.05,
"FeeAmount": 3.50,
"ABA": "111993776",
"BankAccount": "1234567899",
"EmailAddress": "jonah@doe.org",
"AccountHolder": "JonahDoe",
"WaiveFee": false,
"Recurring": false,
"CustomData": null
}
}
当我通过这个时,我得到了正确的回应
但是当我使用另一个时,它并没有给出我需要的响应,唯一的区别是我硬编码的第一个有 \r\n 而下面的第二个没有。
{
"RequestHeader": {
"UserName": " ",
"Password": " "
},
"RequestBody": {
"ChannelType": 1,
"BillAccountNumber": "1075-001",
"BillAccountType": null,
"PaymentAmount": 15.05,
"FeeAmount": 3.5,
"ABA": "111993776",
"BankAccount": "1234567899",
"EmailAddress": "jonah@doe.org",
"AccountHolder": "Jonah Doe",
"WaiveFee": false,
"Recurring": false,
"CustomData": null
}
}
谁能告诉我是什么问题。
代码是:
class Program
{
static void Main(string[] args)
{
var RequestBody = new RequestBody
{
ChannelType = 1,
BillAccountNumber = "1075-001",
BillAccountType = null,
PaymentAmount = 15.05,
FeeAmount = 3.50,
ABA = "111993776",
BankAccount = "1234567899",
EmailAddress = "jonah@doe.org",
AccountHolder = "Jonah Doe",
WaiveFee = false,
Recurring = false,
CustomData = null
};
var RequestHeader = new RequestHeader
{
UserName = "myUname",
Password = "MyPass"
};
var Request = new Request
{
RequestBody = RequestBody,
RequestHeader = RequestHeader,
};
var ApiCredentials = new ApiCredentials
{
Request = Request
};
var httpWReq = (HttpWebRequest)WebRequest.Create("https://gw1.cwplab.com/api/Gateway/AuthorizeCheck");
httpWReq.ContentType = "application/json";
//httpWReq.Credentials = new NetworkCredential(" ", " ");
string data = "{\r\n\"RequestHeader\":{\r\n\"UserName\":\" \",\r\n\"Password\":\" \"\r\n},\r\n\"RequestBody\":{\r\n \"ChannelType\":1,\r\n \"BillAccountNumber\":\"1075-001\",\r\n \"BillAccountType\":null,\r\n \"PaymentAmount\":15.05,\r\n \"FeeAmount\":3.50,\r\n \"ABA\":\"111993776\",\r\n \"BankAccount\":\"1234567899\",\r\n \"EmailAddress\":\"jonah@doe.org\",\r\n \"AccountHolder\":\"Jonah Doe\",\r\n \"WaiveFee\":false,\r\n \"Recurring\":false,\r\n \"CustomData\":null\r\n}\r\n}\r\n";//Request.ToJSON();
string data1 = Newtonsoft.Json.JsonConvert.SerializeObject(Request);
httpWReq.ContentLength = data.Length;
httpWReq.Method = "POST";
using (StreamWriter stream = new StreamWriter(httpWReq.GetRequestStream()))
{
stream.Write(data);
//stream.Flush();
//stream.Close();
};
dynamic httpResponse = (HttpWebResponse)httpWReq.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
Console.WriteLine(result);
Console.ReadKey();
}
}
}
【问题讨论】:
-
两个json都是一样的。
-
@AmitKumarGhosh 不,它们不是,不同的值