Search code examples
c#httprequesthttpresponsesystem.net.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. But every time I am making the request, I am getting a 404 error. When I do the Postman Request using the same data for POST, I am getting a positive result.

Below is the JSON I'm sending to the API using Postman.

{
  "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:

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.". Below is my C# code

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?


Solution

  • 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.

    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".

    Then I modified your code to add cookie also while sending the request to API, and it worked fine.

    To add cookie to HttpWebRequest, you need to use CookieContainer.

    You need to initialize CookiContainer property of httpWebRequest and add cookie to it.

    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.