Calling REST API in Asp.Net

I want to call REST Api from Asp.net application in order to integrate with NCB bank, they provide me with some test parameters like:

  • AccessCode
  • Merchant Id
  • Secure Hash code
  • I've tried the below code but it doesn't work properly it throw 404 http error code.

     private const string URL="https://migs.mastercard.com.au/vpcpay";
     private const string urlParameters = @"{""object"":{""vpc_AccessCode"":""000000"",""vpc_Version"":""0""};}";
    
     HttpClient client = new HttpClient();
     client.BaseAddress = new Uri(URL);
    
     //Add an Accept header for JSON format.
     client.DefaultRequestHeaders.Accept.Add(
     new MediaTypeWithQualityHeaderValue("application/json"));
    
     HttpResponseMessage response = client.GetAsync(urlParameters).Result;
    
    
       if (response.IsSuccessStatusCode)
        {
          // Parse the response body. Blocking!
          var dataObjects = response.Content.ReadAsAsync<IEnumerable<DataObject>>().Result;
             foreach (var d in dataObjects)
              {
                Console.WriteLine("{0}", d.Name);
              }
        }
       else
        {
          Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
        }
    

    I think it should be using a POST, not a GET. Use PostAsync instead of GetAsync.


    Googling around I found this gist which seems to be what you are after with a whole lot of the plumbing already done for you.

    https://gist.github.com/samnaseri/2211309

    链接地址: http://www.djcxy.com/p/45886.html

    上一篇: 如何在j#中动态地将json反序列化为多种类型

    下一篇: 在Asp.Net中调用REST API