如何将此卷曲转换为 RestSharp

本文关键字:转换 RestSharp | 更新日期: 2023-09-27 18:37:19

我在将此curl请求转换为 RestSharp 时遇到问题,它返回 404 错误,请注意 URL 是正确的。我认为问题来自-d参数。

curl -v -X POST https://sandbox.bluesnap.com/services/2/transactions '
-H 'Content-Type: application/xml' '
-H 'Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=' '
-d '
<card-transaction xmlns="http://ws.plimus.com">
   <card-transaction-type>AUTH_CAPTURE</card-transaction-type>
   <recurring-transaction>ECOMMERCE</recurring-transaction>
   <soft-descriptor>DescTest</soft-descriptor>
   <amount>11.00</amount>
   <currency>USD</currency>
   <card-holder-info>
      <first-name>test first name</first-name>
      <last-name>test last name</last-name>
   </card-holder-info>
   <credit-card>
      <card-number>4263982640269299</card-number>
      <security-code>837</security-code>
      <expiration-month>02</expiration-month>
      <expiration-year>2018</expiration-year>
   </credit-card>
</card-transaction>'

我做了这样的事情:

var client = new    RestClient("https://sandbox.bluesnap.com/services/2/transactions");
            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "Content-Type: application/xml");
            client.Authenticator = new HttpBasicAuthenticator("username", "password");

            request.AddBody(GetXmlToSend(record, CriditCardInfo));
            IRestResponse response = client.Execute(request);
            var content = response.Content; 
            var response2 = client.Execute<dynamic>(request);

如何将此卷曲转换为 RestSharp

当你实例化一个新RestClient时,你应该使用基本网址,例如:

var client = new RestClient("https://sandbox.bluesnap.com");

然后,在创建RestRequest时,应传递资源的路径:

var request = new RestRequest("services/2/transactions", Method.POST);

XML数据写入文件,例如文件.xml

RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest(remainingPartOfURL, Method.POST);
client.Authenticator = new HttpBasicAuthenticator(userId, password);
request.AddHeader("Content-Type", "application/xml");
request.AddHeader("Accept", "application/json");
request.AddFile("myFile", "D:''file.xml");
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
IRestResponse response = client.Execute(request);
if (response != null)
            Console.WriteLine(response.Content); 

如果要在 Office 环境中调用此代码,则需要提供代理服务器信息。您可以从设置->"网络和互联网"->代理中获取代理服务器信息

NetworkCredential aCredentials = new NetworkCredential();
aCredentials.Domain = "";
aCredentials.UserName = "";
aCredentials.Password = "";
WebProxy aProxy = new WebProxy();
aProxy.Address = new Uri("http://proxyserver.com:8080");
aProxy.Credentials = aCredentials;
client.Proxy = aProxy;