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

时间:2011-10-12 11:22:36

标签: c# asp.net .net vb.net

我正在尝试从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" %>

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

您的整个应用程序代码都在C#中。因此,最好将巴克莱代码转换为C#。您可以使用以下链接将vb代码转换为C#。 http://www.developerfusion.com/tools/convert/vb-to-csharp/

答案 1 :(得分: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);
    }
}

请注意,大部分代码都是相同的,例如通过名称空间看起来几乎相同来访问框架的类型;差异包括区分大小写,因此我们必须用户保留字的小写字母,例如访问描述符(publicprivate等)和{{1} } keyword,et cetera;另外,语句以C#中的分号结束。

此外,请注意您可能需要注意的代码的一些“问题”,例如WebClientdisposable,此处未正确处理(即new永远不会被称为。)