标签: restsharp

使用 RestSharp 轻松调用 Rest api

我有一个 RestApi,我想将它与我的 c# mvc 客户端一起使用。

我需要发送一些由 ac# 模型表示的数据包。

我尝试使用 RestSharp 代码:

//model = UserModel
var client = new RestClient(apiUrl);
var request = new RestRequest("/user/register", Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddParameter("user", model);
request.AddHeader("Content-Type", "application/json; charset=UTF-8");        
var response = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)

在response.StatusCode中我有BadRequest System.Net.HttpStatusCode

我如何调试我的请求?我尝试使用wireshark 查看请求,但没有看到任何json 数据包。

编辑:

当我使用 AddJsonBody 而不是 AddParameter 时,它可以工作,但我的 json 数据包的格式不符合预期。

我想

 {
   "user":
       {"Username": "Test", 
        "Password": "password"
       }
 }
Run Code Online (Sandbox Code Playgroud)

使用 AddJsonBody 我有:

 {
       {
        "Username": "Test", 
        "Password": "password"
       }
 }
Run Code Online (Sandbox Code Playgroud)

如何使用 RestSharp 在 json 数据包中给出“名称”?

c# rest restsharp

1
推荐指数
1
解决办法
4827
查看次数

在属性名称中使用减号反序列化 Json

我有一个 JSON 返回如下。

   {
    "id": 100,
    "name": "employer 100",
    "externalId": "100-100",
    "networkId": 1000,
    "address": {
        "street-address": "1230 Main Street",
        "locality": "Vancouver",
        "postal-code": "V6B 5N2",
        "region": "BC",
        "country-name": "CA"
    }
   }
Run Code Online (Sandbox Code Playgroud)

所以我创建了类来反序列化上面的json。

    public class Employer
        {
            public int id { get; set; }
            public string name { get; set; }
            public string externalId { get; set; }
            public int networkId { get; set; }
            public Address address { get; set; }
        }
    public class Address
        {
            public string street_address { get; …
Run Code Online (Sandbox Code Playgroud)

c# json restsharp deserialization json-deserialization

1
推荐指数
1
解决办法
1931
查看次数

通过 .NET Core Api 从请求获取文件

我不知道如何通过 .NET Core API 获取文件并将其保存为文件。

例如:我使用以下方法上传我的测试文件:

private void Button_Click(object sender, RoutedEventArgs e) {
        var client = new RestSharp.RestClient("https://localhost:44356/api/");

        var request = new RestSharp.RestRequest("Upload", RestSharp.Method.POST);
        request.AddFile(
            "test",
            "c:\\kill\\test.wav");

        client.Execute(request);
    }
Run Code Online (Sandbox Code Playgroud)

我的 API 收到一个请求。这个方法看起来像这样:

[HttpPost]
    public IActionResult UploadFile() {
        using(var reader = new StreamReader(Request.Body)) {
            var body = reader.ReadToEnd();
        }
        return View();
    }
Run Code Online (Sandbox Code Playgroud)

我的 body 变量包含以下内容:

-------------------------------28947758029299 内容处置:表单数据;名称=“测试”;filename="test.wav" 内容类型:application/octet-stream RIFF^b

现在我的问题是:

如何获取该文件并将其保存在我的光盘上?

我在我的客户端 Restsharp 版本 106.3.1 上使用 .NET Core 2.1 Webapi 。

.net c# rest restsharp asp.net-core

1
推荐指数
1
解决办法
8083
查看次数

以字符串形式返回响应

我正在尝试使用 RestSharp 从 POST 请求返回字符串响应。

这就是我正在尝试的

public IRestResponse PostNewLocation(string Name, string Type, Nullable<Guid> ParentId, string Location)
{
    object tmp = new
    {
        name = Name,
        type = Type,
        parentId = ParentId,
        Location = Location
    };
    string json = JsonConvert.SerializeObject(tmp);

    var Client = new RestClient();
    Client.BaseUrl = new Uri(BaseURL);
    var request = new RestRequest(Method.POST);
    request.Resource = string.Format("/Sample/URL?");
    request.AddParameter("application/json", json, ParameterType.RequestBody);
    IRestResponse response = Client.Execute(request);

    Console.Write(response.Content);

    if (!IsReturnedStatusCodeOK(response))
    {
        throw new HttpRequestException("Request issue -> HTTP code:" + response.StatusCode);
    }

    return response.Content;
} …
Run Code Online (Sandbox Code Playgroud)

c# api restsharp

1
推荐指数
1
解决办法
6971
查看次数

如何使用 RESTSharp 获取响应标头?

我正在开发一个调用 API 的程序,一切正常,但我必须恢复响应头上的一些信息,我该如何恢复这些信息?

我尝试过类似的东西:string h = response.Headers;但它不起作用。

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

        client.Authenticator = new HttpBasicAuthenticator("user", "password");


        var request = new RestRequest("xx/xx/xx", Method.GET);
        IRestResponse response = client.Execute(request);
        var xml_text = response.Content;
Run Code Online (Sandbox Code Playgroud)

c# api restsharp

1
推荐指数
1
解决办法
3079
查看次数

RestRequest.AddFileBytes 发布损坏的文件

我正在尝试使用 RestRequest.AddFileBytes 将图像文件发布到服务,但是当我这样做时,文件似乎已损坏。当我下载回来时,文件大小稍有偏差,并且根据文件类型,它可能看起来有错误的扩展名或无法解释(大概是因为标头混乱了)

当我尝试用 Postman 做同样的事情时,它的文件没问题,所以这似乎与我发布文件的方式有关。请指教!

private static bool AddPageBytes(string user, string password, string documentId, string fileName, string contentType, byte[] bytes)
        {
            System.Console.WriteLine($"AddPageBytes documentId:{documentId}  fileName:{fileName} contentType:{contentType} bytes:{bytes.Length}");
            var client = new RestClient(BASE_URL);
            var request = new RestRequest("v1/document/{id}/page", Method.POST);

            request.AddUrlSegment("id", documentId);

            request.AddHeader("X-IntegrationServer-Username", user);
            request.AddHeader("X-IntegrationServer-Password", password);
            request.AddHeader("Content-Type", "application/octet-stream");

            request.AddHeader("X-IntegrationServer-FileSize", bytes.Length.ToString());

            request.AddHeader("X-IntegrationServer-Resource-Name", fileName);
            request.AddFileBytes(fileName, bytes, fileName, contentType);


            var response = client.Execute(request);

            return response.IsSuccessful;

        }
Run Code Online (Sandbox Code Playgroud)

restsharp

1
推荐指数
1
解决办法
1958
查看次数

RestClient ExecuteAsync 方法已弃用

由于我已经升级了 RestSharp 包,看来这段代码现在已被弃用:

var taskCS = new TaskCompletionSource<IRestResponse>();
var client = new RestClient(url + endPoint);
var request = new RestRequest(Method.POST);

client.ExecuteAsync(request, response =>
{
    taskCS.SetResult(response);
});

...

return await taskCS.Task;
Run Code Online (Sandbox Code Playgroud)

这是我收到的警告:

RestClientExtensions.ExecuteAsync(IRestClient, IRestRequest, Action)' 已过时:'使用返回任务的 ExecuteAsync'

您是否有使用返回任务的 ExecuteAsync 方法的示例?

谢谢,

克里斯

c# restsharp

1
推荐指数
1
解决办法
4068
查看次数

使用 openssl 将 .pem 文件转换为 .crt

我希望能够在 API 请求中发送证书。

请参阅 -根据 RestSharp 请求添加证书

正如那篇文章所示。我需要将 .crt 和 .key 转换为 .pfx ,但是我当前的证书是 .pem ,所以我想我需要先将它们转换为 .crt 和 .key ,然后使用该帖子中使用的 openssl 命令来转换它们到 .pfx 中,然后继续解决方案的其余部分。

我的证书是——

CRT 文件 - C:\Users\JohnSmith\Downloads\certsh\client-crt.pem

密钥文件 - C:\Users\JohnSmith\Downloads\certsh\client-key.pem

我能够将 Key 文件转换为 .key ,但是当尝试转换 CRT 文件时,我收到此错误。

unable to load certificate 13668:error:0906D06C:PEM routines:PEM_read_bio:no start line:.\crypto\pem\pem_lib.c:697:Expecting: TRUSTED CERTIFICATE error in x509
Run Code Online (Sandbox Code Playgroud)

我正在使用此命令尝试将 .pem 转换为 .crt

x509 -outform der -in client-csr.pem -out client.crt
Run Code Online (Sandbox Code Playgroud)

ssl crt pem restsharp postman

1
推荐指数
1
解决办法
3万
查看次数

休息夏普 - 406错误 - 接受标头不匹配

尝试使用RestSharp将请求发布到第三方应用程序时,我收到406错误.我是REST的新手,所以我不得不承认我甚至不知道你可以添加标题.我尝试添加这些,但我仍然遇到同样的问题:

    var client = new RestClient(myURL);            

    RestRequest request = new RestRequest("restAction", Method.POST);     

    request.AddHeader("Accept", "text/plain"); 
    request.AddHeader("Content-Type", "text/plain");

    request.AddParameter("parameter1", param1);
    request.AddParameter("parameter2", param2);

    var response = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)

根据我的阅读,这可能是处理名为"接受"的标题.是对的吗?

知道会发生什么吗?

c# rest restsharp http-status-code-406

0
推荐指数
1
解决办法
5963
查看次数

使用 RestSharp 提交 POST 请求,将值添加到 header

我正在尝试使用 C# 中的 RestSharp 向 API 提交一些数据,似乎我的所有参数都添加到 Headers 集合中 - 或者这就是它们在 VS 中的编目方式。

这是我的代码

var client = new RestClient("https://api.com");
var request = new RestRequest("/recognize", Method.POST);

request.AddHeader("app_id", "");
request.AddHeader("app_key", "");
request.AddHeader("Content-Type", "multipart/form-data");

request.AddParameter("gallery_name", model.gallery_name);
request.AddParameter("image", model.image);

var response = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)

从 API 返回的错误表示请求缺少 gallery_name 和 image 参数,但查看它们在 Headers 集合中的请求对象。

我可以在 Postman 中进行调用,其中方法设置为 Post,Body 设置为 form-data,并列出 2 个键/值对。

我究竟做错了什么?

c# restsharp

0
推荐指数
1
解决办法
2558
查看次数