帮助curl和c# -基础(GoPay项目)
本文关键字:GoPay 项目 基础 curl 帮助 | 更新日期: 2023-09-27 18:08:51
谁能帮我curl和。net ?我有curl文档的主要部分和这个服务器上的一些主题,但仍然不知道(如何格式化数据参数,头等)。例如,我需要发送这个curl请求:(gopay项目)
curl -v https://testgw.gopay.cz/api/oauth2/token '
-X "POST" '
-H "Accept: application/json" '
-H "Content-Type: application/x-www-form-urlencoded" '
-u "<Client ID>:<Client Secret>" '
-d "grant_type=client_credentials&scope=payment-create"
然后像这样(更大的数据):
curl -v https://testgw.gopay.cz/api/payments/payment '
-X "POST" '
-H "Accept: application/json" '
-H "Content-Type: application/json" '
-H "Authorization: Bearer xzZnu3YnAHRk298EwdettFQMcbCcvmwTKKfhrJx2aGG8ZnFyBJhAvFW547WVSD7p" '
-d '{
"payer": {
"default_payment_instrument":"BANK_ACCOUNT",
"default_swift":"FIAFZPP",
"contact":{"first_name":"Petr1",
"last_name":"Pan"
}
},
"amount":"100",
"items":[{"name":"item01","amount":"500"},
{"name":"item02","amount":"500"}
],
"callback":{
"return_url":"http://www.eshop.cz/return",
"notification_url":"http://www.eshop.cz/notify"
}
}'
好的,工作示例(第一部分):
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://testgw.gopay.cz/api/oauth2/token");
request.Method = "POST";
request.Accept = "application/json";
string credentials = String.Format("{0}:{1}", "testid", "testecret");
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(credentials);
string base64 = Convert.ToBase64String(bytes);
string authorization = String.Concat("basic ", base64);
request.Headers.Add("Authorization", authorization);
request.ContentType = "application/x-www-form-urlencoded";
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
string data = "grant_type=client_credentials&scope=payment-create";
streamWriter.Write(data);
}
WebResponse wr = request.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, System.Text.Encoding.UTF8);
string content = reader.ReadToEnd();
var json = "[" + content + "]"; // change this to array
var objects = JArray.Parse(json); // parse as array
foreach (JObject o in objects.Children<JObject>())
{
foreach (JProperty p in o.Properties())
{
string name = p.Name;
string value = p.Value.ToString();
}
}