【发布时间】:2016-05-05 21:29:33
【问题描述】:
请帮我完成最后一步,这样我才能完成这个项目:)
我正在尝试将此示例转换为 Vb.Net:
https://docs.connect.squareup.com/articles/processing-payment-rest/
用户输入信用卡信息后,我可以成功获得 CardOnce ID。我可以成功地从 Square 读取以获取我的位置 ID。但是,当我提供 CardOnce ID、位置 ID 和金额到 POST 时,我卡在最后一步,我不断收到错误消息:“远程服务器返回错误:(422) 状态代码 422。”
下面列出了我的代码和错误发生:
调用时发生错误:
response = DirectCast(request.GetResponse(), HttpWebResponse)
完整代码在这里:
Public Function IsSuccessProcess(ByVal sLocationId As String, ByVal sCardOnce As String, ByVal iAmount As Integer)
Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader
Dim address As Uri
Dim data As StringBuilder
Dim byteData() As Byte
Dim postStream As Stream = Nothing
address = New Uri("https://connect.squareup.com/v2/locations/" & sLocationId & "/transactions")
' Create the web request
request = DirectCast(WebRequest.Create(address), HttpWebRequest)
' Set type to POST
request.Method = "POST"
request.ContentType = "application/json"
request.Accept = "application/json"
request.Headers.Add("Authorization", "Bearer " & AccessToken)
data = New StringBuilder()
Dim sQuote As String = """"
sQuote = "'"
data.Append("{" & sQuote & "card_nonce" & sQuote & ": " & sCardOnce & "," & sQuote & "amount_money" & sQuote & ": {" & sQuote & "amount" & sQuote & ": " & iAmount & "," & sQuote & "currency" & sQuote & ": " & sQuote & "USD" & sQuote & "}")
Dim didempotency_key As Double = Microsoft.VisualBasic.Timer
Dim idempotency_key As Integer = CInt(didempotency_key)
data.Append("'idempotency_key': " & idempotency_key)
' Create a byte array of the data we want to send
byteData = UTF8Encoding.UTF8.GetBytes(data.ToString())
' Set the content length in the request headers
request.ContentLength = byteData.Length
' Write data
Try
postStream = request.GetRequestStream()
postStream.Write(byteData, 0, byteData.Length)
Finally
If Not postStream Is Nothing Then postStream.Close()
End Try
Try
' Get response
response = DirectCast(request.GetResponse(), HttpWebResponse)
' Get the response stream into a reader
reader = New StreamReader(response.GetResponseStream())
' Console application output
Console.WriteLine(reader.ReadToEnd())
Finally
If Not response Is Nothing Then response.Close()
End Try
Return True
End Function
【问题讨论】:
-
HTTP 响应的正文中可能有一个更具体的错误 - 你能打印出来吗?
-
我正在尝试,但我不确定我找对地方了。当我尝试获得响应时,它会触发错误,因此“响应”变量为空。我可以检查“请求”变量的部分内容。我应该尝试查看一些特定的变量或命令吗?
-
我想通了。我的 json 字符串并不完全正确。我将在我的答案中包含正确的版本。那根弦太喜怒无常了:/ ...但一切都很好,结局很好。不过,我仍然想知道如何查看该 HTTP 响应。
标签: asp.net vb.net square-connect