【问题标题】:Setting Callback URL and retrieve data using RestSharp使用 RestSharp 设置回调 URL 并检索数据
【发布时间】:2014-10-20 23:47:03
【问题描述】:

我正在调用一个 Restful API,它希望我在执行 POST 时传递一个参数回调(一个 URL)。 我用来拨打电话的工具是 RESTSharp。我写了下面的代码

var client = new RestClient("https://services.mywebsite.com/api/v3/");
client.Authenticator = new HttpBasicAuthenticator("Usernamae", "P2$$w0rd");

var request = new RestRequest("myAction", Method.POST);
request.AddHeader("Accept", "application/json");

request.AddParameter("Id", "sid");
request.AddParameter("callback", "http://localhost"); // ????????
request.RequestFormat = DataFormat.Json;
request.AddFile("file", @"e:\MyDocuemnt.pdf");

client.ExecuteAsync(request, response => {
Console.WriteLine(response.Content);

有参数回调的那一行需要我传一个url 我应该在这里传递什么? 结果被传递回提供的回调 URL,我如何才能看到呢?

我的应用程序是一个控制台应用程序,我想在这里捕获结果。

【问题讨论】:

  • 您正试图查看发送到回调 URL 的响应,而不是您的 RestRequest 的响应?您是否有一个在该 URL 上侦听的端点?

标签: c# callback restsharp


【解决方案1】:

您的 API 需要一个地方来转储处理后的结果。

根据我的理解,这是正在发生的事情。

  1. 您调用 API 并希望您提供回调 url
  2. API 向您返回响应。

现在的问题是我的结果在哪里,如果它被发送到回调 url,我如何得到它。

这是你需要做的。

您可以使用 POST 方法创建一个 Web API,为您解决问题。 确保您正在托管您的 WebApi,您正在调用的服务应该可以访问该 WebApi,并希望将结果发送给您。

            public void Post([FromBody]JToken value)
            {
                var path = HttpContext.Current.ApplicationInstance.Server.MapPath("~/App_Data");

                File.WriteAllText(path + @"/WriteJSON" + DateTime.Now.Ticks.ToString() + ".txt", value.ToString());

                // write any code that you want to here

            }

这应该对你有用。我在这里所做的就是写回你可能想做的任何你想做的响应。

【讨论】:

    猜你喜欢
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2014-11-22
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多