2011-10-12 35 views
1

我試圖從巴克萊支付網關實現加密腳本,但它是在VB中,我們的網站的其餘部分是在C#中。巴克萊加密asp.net(VB到C#)

劇本是

Public Class Example 
    Inherits System.Web.UI.Page 

#Region " Web Form Designer Generated Code " 
    'This call is required by the Web Form Designer. 
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() 
    End Sub 

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 
     'CODEGEN: This method call is required by the Web Form Designer 
     'Do not modify it using the code editor. 
     InitializeComponent() 
    End Sub 
#End Region 

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     'Put user code to initialize the page here 

     'The Following Creates the WebClient Object 
     Dim web As New System.Net.WebClient() 

     'The Header Content Type is then set 
     web.Headers.Add("Content-Type", "application/x-www-form-urlencoded") 

     'PostData is then declared as data type Byte and populated with the post data 
     Dim PostData As Byte() = System.Text.Encoding.ASCII.GetBytes("clientid=[clientid]&password=[password]&oid=[orderid]&chargetype=PreAuth&currencycode=826&total=[total]") 

     'The Web object is then used to upload the postdata to the Encryption URL and the response is stored in the Response variable 
     Dim Response As Byte() = web.UploadData("https://secure2.epdq.co.uk/cgi-bin/CcxBarclaysEpdqEncTool.e", "POST", PostData) 

     'The response from the post is then converted from Type Byte to String and stored in the session variable 
     Session("Response") = (System.Text.Encoding.ASCII.GetString(Response)) 

    End Sub 
End Class 

我怎麼能無論是在我的C#aspx頁面運行這個VB?或者我需要將其轉換爲使用c#?在我的aspx頁面中,我有第一行

<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="utf-8" CodeBehind="encryption.cs" %> 

感謝您的任何幫助。

+0

基本上是建立交流#asp.net頁面並在page_load方法中將'Dim X as Y'更改爲'YX'或'var X'幾乎可以讓你在那裏。 –

回答

2

而不是鞋在這個VB.NET代碼在某處,它會更簡單的轉換它;並且,相信你會採取消化它的時候,我已經採取了自由,爲你這樣做:

public class Example : System.Web.UI.Page 
{ 
    private void Page_Load(object sender, System.EventArgs e) 
    { 
     //put user code to initialise page here 

     var client = new System.Net.WebClient(); 
     client.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); 

     var data = System.Text.Encoding.ASCII.GetBytes(
      "clientid=[clientid]&password=[password]&oid=[orderid]&" + 
      "chargetype=PreAuth&currencycode=826&total=[total]"); 

     var response = client.UploadData(
      "https://secure2.epdq.co.uk/cgi-bin/CcxBarclaysEpdqEncTool.e", 
      "POST", data); 

     Session["Response"] = System.Text.Encoding.ASCII.GetString(response); 
    } 
} 

注意,大部分的代碼是一樣的,如通過訪問類型框架命名空間看起來幾乎相同;差異包括區分大小寫,所以我們必須爲保留字的用戶小寫字母,如訪問描述符(public,private等)和new等關鍵字等;此外,語句在C#中以分號結尾。

另外,注意一些「問題」與您可能想要看出來的代碼,例如WebClientdisposable,東西在這裏處理不當(即Dispose不會被調用。)

+0

非常感謝!我已經彈出了代碼,並且我明白你做了什麼:)但是,它似乎並沒有被調用。我刪除了除設置session =「hello」之外的所有代碼。它沒有按照我的預期寫出來。這是我第一次使用代碼隱藏! –

+0

在你的頁面指令中,嘗試設置'AutoEventWireup = true';這個問題聽起來像你的頁面沒有訂閱該事件,因此從未被警告。 –

+0

改變了一點... src =「encryption.cs」inherits =「Encryption:Example」。它現在顯示會話變量....非常感謝您的幫助! :) –

2

您的整個應用程序代碼是用C#編寫的。因此,最好將該巴克萊代碼轉換爲C#。您可以使用下面的鏈接將vb代碼轉換爲C#。 http://www.developerfusion.com/tools/convert/vb-to-csharp/

+0

感謝您的鏈接 - 這將毫無疑問將在未來有用! –

+0

享受developerfusion.com ...- :) –