简体   繁体   English

Postman 和 HttpWebRequest 给出不同的结果

[英]Postman and HttpWebRequest are giving different results

I'm new to C# and I want to send a request in JSON to an API and get the JSON response in return so that I can use it in my application.我是 C# 新手,我想以 JSON 格式向 API 发送请求并获得 JSON 响应作为回报,以便我可以在我的应用程序中使用它。 But every time I am making the request, I am getting a 404 error.但是每次我提出请求时,我都会收到 404 错误。 When I do the Postman Request using the same data for POST, I am getting a positive result.当我使用相同的 POST 数据执行 Postman Request 时,我得到了肯定的结果。

Below is the JSON I'm sending to the API using 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
}

and this is the URL I am sending to:这是我要发送到的 URL:

https://www.yomoneyservice.com/yoclient/transaction 

and this is the response I get这是我得到的回应

{"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}]"}

Now, In C# I'm using WebRequest&WebResponse but I'm getting a 404 error saying "The remote server returned an error: (404) Not Found.".现在,在 C# 中,我正在使用 WebRequest&WebResponse,但出现 404 错误,提示“远程服务器返回错误:(404) 未找到。”。 Below is my C# code下面是我的 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);
            }

I'm no longer sure where I'm getting it wrong in my C# code, could anyone help me?我不再确定我的 C# 代码哪里出错了,有人可以帮助我吗?

I tried to reproduce your use case both in code and postman.我试图在代码和邮递员中重现您的用例。 Your observation is correct, it works in postman but not in C# code.您的观察是正确的,它适用于邮递员,但不适用于 C# 代码。

Then I observe the different between the requests being sent from code and postman.然后我观察从代码和邮递员发送的请求之间的不同。 The difference is cookie.区别在于饼干。

Postman sends one cookie with name "AspxAutoDetectCookieSupport" which has value "1". Postman 发送一个名为“AspxAutoDetectCookieSupport”的 cookie,其值为“1”。

Then I modified your code to add cookie also while sending the request to API, and it worked fine.然后我修改了您的代码以在向 API 发送请求的同时添加 cookie,并且它运行良好。

To add cookie to HttpWebRequest, you need to use CookieContainer .要将 cookie 添加到 HttpWebRequest,您需要使用CookieContainer

You need to initialize CookiContainer property of httpWebRequest and add cookie to it.您需要初始化CookiContainer属性并向其添加 cookie。

You need to change your code as following.您需要按如下方式更改代码。

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);
}

I hope this will help you resolve your issue.我希望这将帮助您解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM