2013-02-21 39 views
0

我是一位新的vb.net開發人員,我正在嘗試與API服務進行集成,該服務允許我存儲信用卡數據而不遵從PCI。我遇到的問題是,該示例僅適用於JavaScript,並且我無法確定如何在vb.NET中創建相同的簡單過程。我希望有人能夠幫助我這個樣本。使用VB.NET理解JavaScript API示例時遇到困難

這個想法是將POST數據從我的表單傳遞給API,然後處理成功或失敗的響應數據。

 //<![CDATA[ 
     $(document).ready(function() { 
      $('input:button').click(function() { 
       $.ajax({ 
        type: 'post', 
        url: 'https://certify.i4go.com/index.cfm', 
        dataType: 'jsonp', 
        data: { 
         fuseaction: 'account.jsonpPostCardEntry', 
         i4Go_AccountID: '123123', 
         i4Go_SiteID: '456456', 
         i4Go_CardNumber: $('#i4Go_CardNumber').val(), 
         i4Go_ExpirationMonth: $('#i4Go_ExpirationMonth').val(), 
         i4Go_ExpirationYear: $('#i4Go_ExpirationYear').val() 
        }, 
        cache: false 
       }); 
      });     
     }); 
     function parseResponse(data) { 
      if (data.i4go_responsecode != null) { 
       if (data.i4go_responsecode == 1) { 
        $('#response').html('i4Go_UniqueId: ' + data.i4go_uniqueid); 
       } else { 
        $('#response').html('i4Go_ResponseCode: ' + data.i4go_responsecode); 
       } 
      } else { 
       $('#response').html('i4Go_ResponseCode is null'); 
      } 
     } 
     //]]> 
    </script> 
</head> 
<body> 
    <table cellpadding="0" cellspacing="0"> 
     <tbody><tr> 
      <td>Card number:</td><td><input type="text" id="i4Go_CardNumber" value="4111111111111111"></td> 
     </tr> 
     <tr> 
      <td>Exp month:</td><td><input type="text" id="i4Go_ExpirationMonth" value="12"></td> 
     </tr> 
     <tr> 
      <td>Exp year:</td><td><input type="text" id="i4Go_ExpirationYear" value="2020"></td> 
     </tr> 
     <tr> 
      <td><input type="button" value="Get Token"></td> 
     </tr> 
    </tbody></table> 
    <div id="response"></div> 

我感謝任何和所有的幫助。

乾杯,

AJ

回答

1

如果你在.NET 4.5,看看新的WebAPI調用,使這很容易。一旦你創建表示數據的類,序列化是所有爲你做,你的代碼看起來是這樣的:

​​

分析返回的對象,以得到他們正在傳遞迴到你身邊,並決定如何從那裏出發。

有大量的重載可供選擇,所有這些都基本上做同樣的事情。

編輯: 對於.NET 4.0中,你可以使用WebClient,而不是HttpClient - 這是一個多一點的工作,但仍然不是太糟糕了 - 你只需要手動處理序列。對此也有可能很多這樣做的方法,但這裏有一個:

Dim message As String 
Dim s As New SomeData() ' SomeData still represents the data you're passing 
message = New JavaScriptSerializer().Serialize(s) 
Dim webClient As New WebClient() 
Dim response() As Byte = webClient.UploadData(url, Encoding.UTF8.GetBytes(message)) 
Dim responseString as String = Encoding.UTF8.GetString(response) 

現在,您已經將響應作爲一個字符串,你可以計算出如何處理它做的 - 它可能是一個JSON對象或東西像那樣,你需要閱讀或反序列化。

+0

喬,謝謝你的幫助。我目前在.NET 4.0上,我如何實現這個沒有新的HttpClient? – UserAyeJay 2013-02-22 17:43:54

+0

用4.0示例編輯答案。 – 2013-02-22 19:08:25