Skip to main content
 首页 » 编程设计

c#之巴克莱加密 asp.net (VB to c#)

2024年02月23日12artech

我正在尝试从 Barclays 支付网关实现加密脚本,但它是在 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" %> 

感谢您的帮助。

请您参考如下方法:

与其在某个地方硬塞这段 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); 
    } 
} 

请注意,大部分代码是相同的,例如通过 namespace 访问框架的类型看起来几乎相同;差异包括区分大小写,因此我们必须为保留字使用小写字母,例如访问描述符(public、private 等)和 new 关键字,等等;此外,在 C# 中,语句以分号结束。

此外,注意一些您可能需要注意的代码“问题”,例如 WebClient是disposable , 此处未正确处理的内容(即从未调用 Dispose。)