• 全部
  • C#综合技术
  • C#互联网桌面应用
  • AppLauncher
  • WinForm&WPF
  • C#开发新技术
  • 问答

关于C# 调用curl命令问题,熟悉curl命令的帮下忙

闲游四疯 码农  2020-10-14 10:29:33
paypal官方接口
curl -v -X POST https://api.sandbox.paypal.com/v1/customer/disputes/PP-D-27803/provide-evidence \
-H "Content-Type: multipart/related; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" \
-H "Authorization: Bearer Access-Token" \
-F 'input={
"evidences": [
{
"evidence_type": "PROOF_OF_FULFILLMENT",
"evidence_info": {
"tracking_info": [
{
"carrier_name": "FEDEX",
"tracking_number": "122533485"
}
]
},
"notes": "Test"
}
]
};type=application/json' \
-F 'file1=@NewDoc.pdf'

C#代码及生成命令
cmd = "curl -v -X POST https://api.paypal.com/v1/customer/disputes/" + dirName + "/provide-evidence "
+ "-H \"Content-Type: multipart/related; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW\" "
+ "-H \"Authorization: Bearer " + token + "\" "
+ "-F 'input={\\\"evidences\\\": [{\\\"evidence_type\\\": \\\"PROOF_OF_FULFILLMENT\\\",\\\"evidence_info\\\": "
+ "{\\\"tracking_info\\\": [{\\\"carrier_name\\\": \\\"" + expressType + "\\\",\\\"tracking_number\\\": \\\"" + expressNo + "\\\"}]},"
+ "\\\"notes\\\": \\\"" + expressTxt + "\\\"}]};type=application/json' "
+ "-F 'file1=@\\\"" + filePng.FullName + "\\\"'";

curl -v -X POST https://api.paypal.com/v1/customer/disputes/PP-D-79024859/provide-evidence -H "Content-Type: multipart/related; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -H "Authorization: Bearer A21AAMsI4ZYkaJbKfvCWwaqXc98PSq71uZoVqhJOzVy-RzLIt76922GcGHmdKIK1MhCPWupKXq1C0Teny49LiCNrH7U4jIs3w" -F 'input={\"evidences\": [{\"evidence_type\": \"PROOF_OF_FULFILLMENT\",\"evidence_info\": {\"tracking_info\": [{\"carrier_name\": \"CN_CHINA_POST_EMS\",\"tracking_number\": \"LZ273548120CN\"}]},\"notes\": \"test\"}]};type=application/json' -F 'file1=@\"C:\Users\Administrator\Desktop\PP\PP-D-79024859\LZ273548120CN.png\"'

返回结果
c:\Windows\System32>curl -v -X POST https://api.paypal.com/v1/customer/disputes/
PP-D-79024859/provide-evidence -H "Content-Type: multipart/related; boundary=---
-WebKitFormBoundary7MA4YWxkTrZu0gW" -H "Authorization: Bearer A21AAMsI4ZYkaJbKfv
CWwaqXc98PSq71uZoVqhJOzVy-RzLIt76922GcGHmdKIK1MhCPWupKXq1C0Teny49LiCNrH7U4jIs3w"
-F 'input={\"evidences\": [{\"evidence_type\": \"PROOF_OF_FULFILLMENT\",\"evide
nce_info\": {\"tracking_info\": [{\"carrier_name\": \"CN_CHINA_POST_EMS\",\"trac
king_number\": \"LZ273548120CN\"}]},\"notes\": \"test\"}]};type=application/json
' -F 'file1=@\"C:\Users\Administrator\Desktop\PP\PP-D-79024859\LZ273548120CN.png
\"'
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying 64.4.249.23...
* TCP_NODELAY set
* Connected to api.paypal.com (64.4.249.23) port 443 (#0)
> POST /v1/customer/disputes/PP-D-79024859/provide-evidence HTTP/1.1
> Host: api.paypal.com
> User-Agent: curl/7.64.1
> Accept: */*
> Authorization: Bearer A21AAMsI4ZYkaJbKfvCWwaqXc98PSq71uZoVqhJOzVy-RzLIt76922Gc
GHmdKIK1MhCPWupKXq1C0Teny49LiCNrH7U4jIs3w
> Content-Length: 223904
> Content-Type: multipart/related; boundary=----WebKitFormBoundary7MA4YWxkTrZu0g
W; boundary=------------------------42e7b79977d3421a
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 429 Too Many Requests
< Content-Type: application/json
< Content-Length: 128
< Connection: keep-alive
< Date: Wed, 14 Oct 2020 02:05:41 GMT
< Cache-Control: max-age=0, no-cache, no-store, must-revalidate
< Paypal-Debug-Id: e8b878c715d9b
< Strict-Transport-Security: max-age=31536000; includeSubDomains
<
{"name":"RATE_LIMIT_REACHED","debug_id":"e8b878c715d9b","message":"Too many requ
ests. Blocked due to rate limiting.","links":[]}* Connection #0 to host api.payp
al.com left intact
curl: (3) bad range specification in URL position 2:
[{"evidence_type":
^
Note: Unnecessary use of -X or --request, POST is already inferred.
* Could not resolve host: "PROOF_OF_FULFILLMENT","evidence_info"
* Closing connection 1
curl: (6) Could not resolve host: "PROOF_OF_FULFILLMENT","evidence_info"
curl: (3) unmatched brace in URL position 1:
{"tracking_info":
^
curl: (3) bad range specification in URL position 2:
[{"carrier_name":
^
Note: Unnecessary use of -X or --request, POST is already inferred.
* Could not resolve host: "CN_CHINA_POST_EMS","tracking_number"
* Closing connection 2
curl: (6) Could not resolve host: "CN_CHINA_POST_EMS","tracking_number"
curl: (3) unmatched close brace/bracket in URL position 16:
"LZ273548120CN"}]},"notes":
^
curl: (3) unmatched close brace/bracket in URL position 7:
"test"}]};type=application/json'
^
* Closing connection 0
* schannel: shutting down SSL/TLS connection with api.paypal.com port 443

c:\Windows\System32>


重点帮我看下 C# 生成调用命令 有没有问题?是不是字符串没转义好或者格式不对。

...全文
6367 点赞 收藏 6
写回复
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
一看你代码语法好还是处于很低的水平啊 是不是你编辑器 IDE版本太低了还用这样的拼接方式? 高版本的C#中 都是 $语法了.
回复
用Http请求可行?


using (var httpClient = new HttpClient())
{
    using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://api.sandbox.paypal.com/v1/customer/disputes/PP-D-27803/provide-evidence"))
    {
        request.Headers.TryAddWithoutValidation("Authorization", "Bearer Access-Token"); 

        var multipartContent = new MultipartFormDataContent();
        multipartContent.Add(new StringContent("{\n  \"evidences\": [\n{\n  \"evidence_type\": \"PROOF_OF_FULFILLMENT\",\n  \"evidence_info\": {\n  \"tracking_info\": [\n    {\n    \"carrier_name\": \"FEDEX\",\n    \"tracking_number\": \"122533485\"\n    }\n  ]\n  },\n  \"notes\": \"Test\"\n}\n  ]\n}"), "input");
        multipartContent.Add(new ByteArrayContent(File.ReadAllBytes("NewDoc.pdf")), "file1", Path.GetFileName("NewDoc.pdf"));
        request.Content = multipartContent; 

        var response = await httpClient.SendAsync(request);
    }
}

回复
提醒一下,多看一下开发文档。我经常写接口,文档和测试都好重要的。
回复
两个问题,,第一个在内容参数外,尽量用单引号。减少转义 第二个,双引号放到C#代码里直接转义,并在转义前将代码测试一遍,然后以参数的形式修改内容
回复
闲游四疯 2020-10-14
引用 4 楼 亚特兰蒂斯DE 的回复:
一看你代码语法好还是处于很低的水平啊 是不是你编辑器 IDE版本太低了还用这样的拼接方式? 高版本的C#中 都是 $语法了.
我用的是2.0 只是临时小项目 没讲究 直接写,http请求 我晚点试下 谢谢
引用 5 楼 ziqi0716 的回复:
学习下Postman吧,可以导入Curl请求,并生成C#的请求代码(以及其他语言的版本.)
postman有用 但生成的curl命令不对,json类型指定type=application/json ,没了
回复
ziqi0716 2020-10-14
学习下Postman吧,可以导入Curl请求,并生成C#的请求代码(以及其他语言的版本.)
回复
发帖
C#
创建于2007-09-28

10.5w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2020-10-14 10:29
社区公告

让您成为最强悍的C#开发者