巴克莱加密asp.net (VB到c#)
本文关键字:VB net 加密 asp 巴克 | 更新日期: 2023-09-27 18:11:14
我正试图实现从巴克莱支付网关的加密脚本,但它是在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¤cycode=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
我如何能够运行这个VB在我的c# aspx页面?或者我需要将其转换为使用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¤cycode=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#中的语句以分号结束。
进一步,注意代码中的一些"问题",您可能需要注意,例如WebClient
是一次性的,这里没有正确处理(即从未调用Dispose
)
您的整个应用程序代码是用c#编写的。因此,最好将巴克莱代码转换为c#。您可以使用下面的链接将vb代码转换为c#。http://www.developerfusion.com/tools/convert/vb-to-csharp/