php - 如何将 PHP CURL 转换为 ASP.NET
问题描述
我想将下面的 php curl 代码转换为 asp.net。
<?php require_once("config.php");
if($_POST['token']!='' and isset($_POST['token']) ){
$token = $_POST['token'];
$url = 'https://dev-kpaymentgateway-services.kasikornbank.com/card/v2/charge';
$datasend = array(
"amount"=> "2000.00",
"currency"=> "THB",
"description" => "Awesome Product",
"source_type" => "card",
"mode"=> "token",
"token"=> $token,
"reference_order"=> "11251513"
);
$ch = curl_init();
$post_string = json_encode($datasend);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Cache-Control:no-cache',
'x-api-key: '.$secretkey
)
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_SSLVERSION, 0 );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$data = curl_exec($ch);
$response = json_decode($data);
curl_close ($ch);
$response = json_decode(json_encode($response), True);
ob_clean();
header('Location: '.$response['redirect_url']);
}
?>
我尝试使用下面的代码转换为 asp.net。
Dim AppKey As String = "skey_test_xxxxxxxxxx"
Dim URL As String = "https://dev-kpaymentgateway-services.kasikornbank.com/card/v2/charge/"
Try
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
Dim CjSONString As String
Dim li As jsonGetToken = New jsonGetToken()
li.amount = 2000.0
li.currency = "THB"
li.description = "Awesome Product"
li.source_type = "card"
li.mode = "token"
li.reference_order = "11251513"
CjSONString = "{""amount"": " & li.amount & ",""currency"": " & """" & li.currency & """" & ",""description"": " & """" & li.description & """" & ",""source_type"": " & """" & li.source_type & """" & ",""mode"": " & """" & li.mode & """" & ",""reference_order"": " & """" & li.reference_order & """" & "}"
Dim URLHTTP As String = URL
Dim StringJSON As jsonGetToken = JsonConvert.DeserializeObject(Of jsonGetToken)(CjSONString)
Dim jSONString As String = JsonConvert.SerializeObject(li)
Me.txtja.Text = jSONString
Dim request = CType(WebRequest.Create(URLHTTP), HttpWebRequest)
request.ContentType = "application/json; charset=UTF-8"
request.Method = "POST"
request.ContentLength = jSONString.Length
request.Headers.Add("Authorization", "Basic " + AppKey)
Dim output As String = ""
Using streamWriter = New StreamWriter(request.GetRequestStream())
streamWriter.Write(jSONString)
streamWriter.Flush()
streamWriter.Close()
Dim httpResponse = CType(request.GetResponse(), HttpWebResponse)
Using streamReader = New StreamReader(httpResponse.GetResponseStream())
output = streamReader.ReadToEnd()
End Using
End Using
Me.txtAert.Text = output
Catch ex As Exception
Me.txtAert.Text = ex.Message
End Try
但是那个返回错误。
远程服务器返回错误:(401) Unauthorized。
在部分数据中,我发送了如下的 json 格式。
{ “金额”:2000.0, "货币":"泰铢", "description":"很棒的产品", “source_type”:“卡片”, “模式”:“令牌”, “reference_order”:“11251513” }
请建议我。谢谢你。
解决方案
你稍微改变了你的标题
在原始 PHP 中,授权的标题前缀是“x-api-key:”,但在 VB 中,您将其设为“授权:基本”
像这样试试
Dim AppKey As String = "skey_test_xxxxxxxxxx"
Dim URL As String = "https://dev-kpaymentgateway-services.kasikornbank.com/card/v2/charge/"
Try
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
Dim CjSONString As String
Dim li As jsonGetToken = New jsonGetToken()
li.amount = 2000.0
li.currency = "THB"
li.description = "Awesome Product"
li.source_type = "card"
li.mode = "token"
li.reference_order = "11251513"
CjSONString = "{""amount"": " & li.amount & ",""currency"": " & """" & li.currency & """" & ",""description"": " & """" & li.description & """" & ",""source_type"": " & """" & li.source_type & """" & ",""mode"": " & """" & li.mode & """" & ",""reference_order"": " & """" & li.reference_order & """" & "}"
Dim URLHTTP As String = URL
Dim StringJSON As jsonGetToken = JsonConvert.DeserializeObject(Of jsonGetToken)(CjSONString)
Dim jSONString As String = JsonConvert.SerializeObject(li)
Me.txtja.Text = jSONString
Dim request = CType(WebRequest.Create(URLHTTP), HttpWebRequest)
request.ContentType = "application/json; charset=UTF-8"
request.Method = "POST"
request.ContentLength = jSONString.Length
request.Headers.Add("x-api-key", AppKey)
Dim output As String = ""
Using streamWriter = New StreamWriter(request.GetRequestStream())
streamWriter.Write(jSONString)
streamWriter.Flush()
streamWriter.Close()
Dim httpResponse = CType(request.GetResponse(), HttpWebResponse)
Using streamReader = New StreamReader(httpResponse.GetResponseStream())
output = streamReader.ReadToEnd()
End Using
End Using
Me.txtAert.Text = output
Catch ex As Exception
Me.txtAert.Text = ex.Message
End Try
推荐阅读
- java - 将应用设置为主启动另一个实例
- paypal - PayPal Payflow 重定向到 Express Checkout,而不是显示带有卡字段的 Hosted Checkout 页面
- android - 从 YUV_420_888 图像中获取 RGB 值
- javascript - 在 Javascript 中对不同的 ID 多次使用相同的函数
- c# - 等待一系列任务按顺序运行
- linux - cron 作业 - 在周一、周三和周五的不同时间运行
- c# - 使用对象分配索引值时出错
- python - 获取使用传递引用的 COM 对象的返回值
- javascript - 从文件读取时未定义
- azure - Azure Active Directory 双重同意、非确定性流