RestSharp - 带参数的 GET 请求

Des*_*ess 4 c# restsharp

我在发送带参数的请求时遇到问题。我有在 PHP 中发送请求的示例,但我不知道在 RestSharp 中它应该是什么样子:

在此处输入图片说明

如您所见,在示例中,参数被添加到私钥(我也做过),然后还有这个 CURLOPT_POSTFIELDS,其中也添加了参数。我尝试通过 AddParameter、AddBody、AddJsonBody 来做,但没有任何效果。当我将参数连接到私钥时,响应始终为空,如果我删除它,我会得到响应,但我的参数被忽略。

        string data = "{\"Paging\":{\"per_page\":\"" + 10 + "\"}}";

        RestClient client = new RestClient("api");

        string header = "WMS " + publicKey + ":" + GetMd5Hash(privateKey + data);

        IRestRequest request = new RestRequest("products", Method.GET);
        request.AddHeader("Content-Type", "application/json; charset=utf-8");
        request.AddHeader("Authorization", header);
        request.AddHeader("Content-Length", data.Length.ToString());

        //request.RequestFormat = RestSharp.DataFormat.Json;
        request.AddParameter("Paging", new { per_page = 10 });

        IRestResponse response = client.Execute(request);

        Encoding encoding = Encoding.GetEncoding("utf-8");
        var result = encoding.GetString(response.RawBytes);
Run Code Online (Sandbox Code Playgroud)

我能够使用 fiddler 跟踪 php 请求,原始请求如下所示:

获取 API HTTP/1.1
主持人:api
编译指示:无缓存
接受: */*
授权:WMS md5
内容类型:应用程序/json
内容长度:27

{"分页":{"per_page":"8"}}

我的看起来像这样:

获取 API HTTP/1.1
授权:WMS md5
内容类型:应用程序/json
接受: */*
用户代理:RestSharp/105.2.3.0
主持人:api
接受编码:gzip、deflate
连接:保持活动

里面没有显示参数,不知道为什么。我尝试了每种参数类型。除此之外,我的标题“Content-Length”也不可见。

Rom*_*ias 6

向 GET 请求添加参数,只需使用:

request.AddParameter("name", "value");
Run Code Online (Sandbox Code Playgroud)

我认为您的代码中还有其他问题,可能是混合了 json 格式,但由于我们没有您正在使用的实际 API 的详细信息,因此我们无法提供建议。

也许您可以简化示例,对其进行测试,然后在 API 向您显示错误时添加更多配置。在此处查看基本示例:https : //github.com/restsharp/RestSharp