C# Postman和HttpWebRequest给出了不同的结果

C# Postman和HttpWebRequest给出了不同的结果,c#,httprequest,httpresponse,system.net.httpwebrequest,C#,Httprequest,Httpresponse,System.net.httpwebrequest,我是C#新手,我想用JSON向API发送一个请求,并得到JSON响应,以便在我的应用程序中使用它。但每次我提出请求时,都会出现404错误。当我使用POST的相同数据执行邮递员请求时,我得到了一个积极的结果 下面是我使用Postman发送给API的JSON { "ActionId": null, "AgentCode": "MerchantCode:Password", "Mpin": null, &q

我是C#新手,我想用JSON向API发送一个请求,并得到JSON响应,以便在我的应用程序中使用它。但每次我提出请求时,都会出现404错误。当我使用POST的相同数据执行邮递员请求时,我得到了一个积极的结果

下面是我使用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代码

试试看
{
字符串url=string.Format(“https://www.yomoneyservice.com/yoclient/transaction");
var httpWebRequest=(httpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType=“应用程序/json”;
httpWebRequest.Method=“POST”;
使用(var streamWriter=newstreamwriter(httpWebRequest.GetRequestStream()))
{
字符串json=JsonConvert.SerializeObject(yomoneyRequest,Formatting.Indented);
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
var httpResponse=(HttpWebResponse)httpWebRequest.GetResponse();
使用(var streamReader=newstreamreader(httpResponse.GetResponseStream())
{
var result=streamReader.ReadToEnd();
yomoneyResponse=JsonConvert.DeserializeObject(结果);
}
}                
}
捕获(例外e)
{
控制台写入线(e.Message);
}

我不再确定我的C代码哪里出错了,有人能帮我吗?

我试图在代码和postman中重现您的用例。你的观察是正确的,它适用于postman,但不适用于C#code

然后我观察到code和postman发送的请求之间的差异。区别在于饼干

邮递员发送一个名为“AspXAutoDetectCookiesSupport”的cookie,其值为“1”

然后,我修改了您的代码,在将请求发送到API时也添加了cookie,效果很好

要将cookie添加到HttpWebRequest,您需要使用

您需要初始化httpWebRequest的
CookicContainer
属性,并向其中添加cookie

您需要如下更改代码

try
{

    string url = String.Format("https://www.yomoneyservice.com/yoclient/transaction");
    var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = "POST"; 
    httpWebRequest.CookieContainer = new CookieContainer();
    Cookie c = new Cookie("AspxAutoDetectCookieSupport", "1");
    c.Domain = "www.yomoneyservice.com";
    httpWebRequest.CookieContainer.Add(c);  
    string json = JsonConvert.SerializeObject(yomoneyRequest, Formatting.Indented);
    
    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
    {
        streamWriter.Write(json);
    }

    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var result = streamReader.ReadToEnd();
        var yomoneyResponse = JsonConvert.DeserializeObject<YomoneyResponse>(result);
    } 
}
catch(Exception e)
{
    Console.WriteLine(e.Message);
}
试试看
{
字符串url=string.Format(“https://www.yomoneyservice.com/yoclient/transaction");
var httpWebRequest=(httpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType=“应用程序/json”;
httpWebRequest.Method=“POST”;
httpWebRequest.CookieContainer=新CookieContainer();
Cookie c=新Cookie(“AspxAutoDetectCookieSupport”,“1”);
c、 Domain=“www.yomoneyservice.com”;
httpWebRequest.CookieContainer.Add(c);
字符串json=JsonConvert.SerializeObject(yomoneyRequest,Formatting.Indented);
使用(var streamWriter=newstreamwriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(json);
}
var httpResponse=(HttpWebResponse)httpWebRequest.GetResponse();
使用(var streamReader=newstreamreader(httpResponse.GetResponseStream())
{
var result=streamReader.ReadToEnd();
var yomoneyResponse=JsonConvert.DeserializeObject(结果);
} 
}
捕获(例外e)
{
控制台写入线(e.Message);
}

我希望这将帮助您解决您的问题。

我尝试用代码和postman复制您的用例。你的观察是正确的,它适用于postman,但不适用于C#code

然后我观察到code和postman发送的请求之间的差异。区别在于饼干

邮递员发送一个名为“AspXAutoDetectCookiesSupport”的cookie,其值为“1”

然后,我修改了您的代码,在将请求发送到API时也添加了cookie,效果很好

要将cookie添加到HttpWebRequest,您需要使用

您需要初始化httpWebRequest的
CookicContainer
属性,并向其中添加cookie

您需要如下更改代码

try
{

    string url = String.Format("https://www.yomoneyservice.com/yoclient/transaction");
    var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = "POST"; 
    httpWebRequest.CookieContainer = new CookieContainer();
    Cookie c = new Cookie("AspxAutoDetectCookieSupport", "1");
    c.Domain = "www.yomoneyservice.com";
    httpWebRequest.CookieContainer.Add(c);  
    string json = JsonConvert.SerializeObject(yomoneyRequest, Formatting.Indented);
    
    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
    {
        streamWriter.Write(json);
    }

    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var result = streamReader.ReadToEnd();
        var yomoneyResponse = JsonConvert.DeserializeObject<YomoneyResponse>(result);
    } 
}
catch(Exception e)
{
    Console.WriteLine(e.Message);
}
试试看
{
字符串url=string.Format(“https://www.yomoneyservice.com/yoclient/transaction");
var httpWebRequest=(httpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType=“应用程序/json”;
httpWebRequest.Method=“POST”;
httpWebRequest.CookieContainer=新CookieContainer();
Cookie c=新Cookie(“AspxAutoDetectCookieSupport”,“1”);
c、 Domain=“www.yomoneyservice.com”;
httpWebRequest.CookieContainer.Add(c);
字符串json=JsonConvert.SerializeObject(yomoneyRequest,Formatting.Indented);
使用(var streamWriter=newstreamwriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(json);
}
var httpResponse=(HttpWebResponse)httpWebRequest.GetResponse();
使用(var streamReader=newstreamreader(httpResponse.GetResponseStream())
{
var result=streamReader.ReadToEnd();
var yomoneyResponse=JsonConvert.DeserializeObject(结果);
} 
}
捕获(例外e)
{
控制台写入线(e.Message);
}

我希望这将帮助您解决您的问题。

非常感谢。你的解决方案很有效!我从来没有想过饼干。太谢谢你了。你的解决方案很有效!我从来没有想过饼干。