zoukankan      html  css  js  c++  java
  • postman 跟restsharp 模拟请求http

    https://github.com/restsharp/RestSharp

    postman 生成的访问代码;

    好用!

    Features

    • Assemblies for .NET 4.5.2 and .NET Standard 2.0
    • Easy installation using NuGet for most .NET flavors (signed)
    • Automatic XML and JSON deserialization
    • Supports custom serialization and deserialization via ISerializer and IDeserializer
    • Fuzzy element name matching ('product_id' in XML/JSON will match C# property named 'ProductId')
    • Automatic detection of type of content returned
    • GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, COPY supported
    • Other non-standard HTTP methods also supported
    • OAuth 1, OAuth 2, Basic, NTLM and Parameter-based Authenticators included
    • Supports custom authentication schemes via IAuthenticator
    • Multi-part form/file uploads
    var client = new RestClient("http://example.com");
    // client.Authenticator = new HttpBasicAuthenticator(username, password);
    
    var request = new RestRequest("resource/{id}");
    request.AddParameter("name", "value"); // adds to POST or URL querystring based on Method
    request.AddUrlSegment("id", "123"); // replaces matching token in request.Resource
    
    // add parameters for all properties on an object 为对象上的所有属性添加参数
    request.AddObject(@object);
    
    // or just whitelisted properties  或者为某个属性添加参数
    request.AddObject(object, "PersonId", "Name", ...);
    
    // easily add HTTP Headers  添加头
    request.AddHeader("header", "value");
    
    // add files to upload (works with compatible verbs)  上传文件
    request.AddFile("file", path);
    
    // execute the request   执行请求
    var response = client.Post(request);
    var content = response.Content; // raw content as string  获取请求之后的内容
    
    // or automatically deserialize result   序列化返回结果
    // return content type is sniffed but can be explicitly set via RestClient.AddHandler();
    var response2 = client.Post<Person>(request);
    var name = response2.Data.Name;
    
    // or download and save file to disk  
    client.DownloadData(request).SaveAs(path);
    
    // easy async support
    await client.ExecuteTaskAsync(request);
    
    // async with deserialization
    var asyncHandle = client.PostAsync<Person>(request, response => {
        Console.WriteLine(response.Data.Name);
    });
    
    // abort the request on demand
    asyncHandle.Abort();
  • 相关阅读:
    Chrome在解析html时的一个bug
    WebGL笔记(二):顶点着色
    跟据一段代码浅谈Javascript闭包
    [标量函数] Html标记过滤 HtmlFilter
    A new weblog from Contribute CS4
    WebGL笔记(一):起步
    MSSQL查询连接数
    DDD中的分层
    非root用户使用docker方法
    七牛跨服务器上传文件带参数
  • 原文地址:https://www.cnblogs.com/zxs-onestar/p/12027747.html
Copyright © 2011-2022 走看看