httpapihelper 1.0.4

基于 System.Net.HttpWebRequest 封装,方便,易用;引用函数式编程思想,让代码更加的优雅,高可读性。

Install-Package httpapihelper -Version 1.0.4
dotnet add package httpapihelper --version 1.0.4
<PackageReference Include="httpapihelper" Version="1.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add httpapihelper --version 1.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

logo
dotnet-version
csharp-version
IDE-version
nuget-version
qq-group

httpApiHelper

基于 System.Net.HttpWebRequest 封装,方便,易用;引用函数式编程思想,让代码更加的优雅,高可读性。

说明

  • 只支持返回类型为 json 的请求
  • .SetHeaders() 该方法不必须,为满足需要认证和请求头信息的请求而设
  • .OnStart() 、.OnSuccess、.OnFailure()、.OnError() 这些方法不必须,与顺序无关。
  • .OnSuccess、.OnFailure()、.OnError() 互斥,只会调用其中一个。
  • 默认带Cookies 请求。

使用

Step 1:在nuget上添加对 httpapihelper 的引用或搜索 httpapihelper;;

Install-Package httpapihelper

import

Step 2:添加引用

using HttpApiHelper;

Step 3:使用

  • GET 用法

    //简单用法
     public void TestGet()
          {
              string url = "http://127.0.0.1:8080/nbm/login/test";
              HttpWebRequestHelper<List<User>>.Instance              
                  .OnSuccess((result) =>
                  {
                      Console.WriteLine("Success ...");
                      List<User> Users = result.Data;
                      for (int i = 0; i < Users.Count; i++)
                      {
                          User m = Users[i];
                          Console.WriteLine("name:" + m.name + " age :" + m.age);
                      }
                  })
                  .Get(url); 
                
          }
    
    //基本用法
         public void TestGet()
          {
              string url = "http://127.0.0.1:8080/nbm/login/test";
    
              HttpWebRequestHelper<List<User>>.Instance
                  .OnStart(() =>
                  {
                      Console.WriteLine("STart ...");
                  })
                  .OnSuccess((result) =>
                  {
                      Console.WriteLine("Success ...");
                      List<User> Users = result.Data;
                      for (int i = 0; i < Users.Count; i++)
                      {
                          User m = Users[i];
                          Console.WriteLine("name:" + m.name + " age :" + m.age);
                      }
                  })
                  .OnFailure((res) =>
                  {
                      Console.WriteLine("Failure ..code:+" + res.Code + "...msc:" + res.Message);
                  })
                  .OnError((res) =>
                  {
                      Console.WriteLine("Error ..code:+" + res.Code + "...msc:" + res.Message);
                  })
                  .Get(url); //不需要传参
                  //.Get(url, "name=code&age=56"); // 带参数
          }
    
    public void TestGet()
          {
              string url = "http://127.0.0.1:8080/nbm/login/test";
    
              HttpWebRequestHelper<User>.Instance
                  .OnStart(() =>
                  {
                      Console.WriteLine("STart ...");
                  })
                  .OnSuccess((result) =>
                  {
                      Console.WriteLine("Success ...");
                     User m = result.Data;                   
                     Console.WriteLine("name:" + m.name + " age :" + m.age);                    
                  })
                  .OnFailure((res) =>
                  {
                      Console.WriteLine("Failure ..code:+" + res.Code + "...msc:" + res.Message);
                  })
                  .OnError((res) =>
                  {
                      Console.WriteLine("Error ..code:+" + res.Code + "...msc:" + res.Message);
                  })
                  //.Get(url); //不需要传参
                  .Get(url, "id=56"); // 带参数
          }
    
    • Post 用法
    public void TestPost()
          {
              string url = "http://127.0.0.1:8080/nbm/login/test";
              NameValueCollection collection = new NameValueCollection
              {
                  {"name","code"},
                  {"age","80" },
                  {"salary","12.90" }
              };
              System.Net.WebHeaderCollection headerCollection = new System.Net.WebHeaderCollection
             {
                 {"token","qmfamdfasdfm" }
             };
              User user = new User() { name = "code", age = 90, salary = 89.09 };
    
              HttpWebRequestHelper<List<User>>.Instance
                    .OnStart(() =>
                    {
                        Console.WriteLine("-------------STart ...");
                    })
                    .OnSuccess((result) =>
                    {
                        Console.WriteLine("Success ...");
                        List<User> Users = result.Data;
                        for (int i = 0; i < Users.Count; i++)
                        {
                            User m = Users[i];
                            Console.WriteLine("name:" + m.name + " age :" + m.age + " salary:" + m.salary);
                        }
                    })
                    .OnFailure((res) =>
                    {
                        Console.WriteLine("Failure ..code:+" + res.Code + "...msc:" + res.Message);
                    })
                    .OnError((res) =>
                    {
                        Console.WriteLine("Error ..code:+" + res.Code + "...msc:" + res.Message);
                    })
                    .SetHeaders(headerCollection) // 不必须
                   // .Post(url, collection); //参数类型为 NameValueCollection
                   //.Post<User>(url, user);  //参数为对像 
                   //.Post(url, "name=code&age=56"); //参数为字符串
                   .Post(url); //不需要参数
          }
    
              string url = "http://127.0.0.1:8080/nbm/login/test";
            
              HttpWebRequestHelper<User>.Instance                
                    .OnSuccess((result) =>
                    {
                        Console.WriteLine("Success ...");
                        User Users = result.Data;
                        for (int i = 0; i < Users.Count; i++)
                        {
                            User m = Users[i];
                            Console.WriteLine("name:" + m.name + " age :" + m.age + " salary:" + m.salary);
                        }
                    })                 
                   .Post(url);
    
    

捐赠

如果您觉得httpHelper还不错,并且刚好有些闲钱,那么可以选择以下方式来捐赠:

  • 支付宝
    qrcode
  • 微信
    qrcode

logo
dotnet-version
csharp-version
IDE-version
nuget-version
qq-group

httpApiHelper

基于 System.Net.HttpWebRequest 封装,方便,易用;引用函数式编程思想,让代码更加的优雅,高可读性。

说明

  • 只支持返回类型为 json 的请求
  • .SetHeaders() 该方法不必须,为满足需要认证和请求头信息的请求而设
  • .OnStart() 、.OnSuccess、.OnFailure()、.OnError() 这些方法不必须,与顺序无关。
  • .OnSuccess、.OnFailure()、.OnError() 互斥,只会调用其中一个。
  • 默认带Cookies 请求。

使用

Step 1:在nuget上添加对 httpapihelper 的引用或搜索 httpapihelper;;

Install-Package httpapihelper

import

Step 2:添加引用

using HttpApiHelper;

Step 3:使用

  • GET 用法

    //简单用法
     public void TestGet()
          {
              string url = "http://127.0.0.1:8080/nbm/login/test";
              HttpWebRequestHelper<List<User>>.Instance              
                  .OnSuccess((result) =>
                  {
                      Console.WriteLine("Success ...");
                      List<User> Users = result.Data;
                      for (int i = 0; i < Users.Count; i++)
                      {
                          User m = Users[i];
                          Console.WriteLine("name:" + m.name + " age :" + m.age);
                      }
                  })
                  .Get(url); 
                
          }
    
    //基本用法
         public void TestGet()
          {
              string url = "http://127.0.0.1:8080/nbm/login/test";
    
              HttpWebRequestHelper<List<User>>.Instance
                  .OnStart(() =>
                  {
                      Console.WriteLine("STart ...");
                  })
                  .OnSuccess((result) =>
                  {
                      Console.WriteLine("Success ...");
                      List<User> Users = result.Data;
                      for (int i = 0; i < Users.Count; i++)
                      {
                          User m = Users[i];
                          Console.WriteLine("name:" + m.name + " age :" + m.age);
                      }
                  })
                  .OnFailure((res) =>
                  {
                      Console.WriteLine("Failure ..code:+" + res.Code + "...msc:" + res.Message);
                  })
                  .OnError((res) =>
                  {
                      Console.WriteLine("Error ..code:+" + res.Code + "...msc:" + res.Message);
                  })
                  .Get(url); //不需要传参
                  //.Get(url, "name=code&age=56"); // 带参数
          }
    
    public void TestGet()
          {
              string url = "http://127.0.0.1:8080/nbm/login/test";
    
              HttpWebRequestHelper<User>.Instance
                  .OnStart(() =>
                  {
                      Console.WriteLine("STart ...");
                  })
                  .OnSuccess((result) =>
                  {
                      Console.WriteLine("Success ...");
                     User m = result.Data;                   
                     Console.WriteLine("name:" + m.name + " age :" + m.age);                    
                  })
                  .OnFailure((res) =>
                  {
                      Console.WriteLine("Failure ..code:+" + res.Code + "...msc:" + res.Message);
                  })
                  .OnError((res) =>
                  {
                      Console.WriteLine("Error ..code:+" + res.Code + "...msc:" + res.Message);
                  })
                  //.Get(url); //不需要传参
                  .Get(url, "id=56"); // 带参数
          }
    
    • Post 用法
    public void TestPost()
          {
              string url = "http://127.0.0.1:8080/nbm/login/test";
              NameValueCollection collection = new NameValueCollection
              {
                  {"name","code"},
                  {"age","80" },
                  {"salary","12.90" }
              };
              System.Net.WebHeaderCollection headerCollection = new System.Net.WebHeaderCollection
             {
                 {"token","qmfamdfasdfm" }
             };
              User user = new User() { name = "code", age = 90, salary = 89.09 };
    
              HttpWebRequestHelper<List<User>>.Instance
                    .OnStart(() =>
                    {
                        Console.WriteLine("-------------STart ...");
                    })
                    .OnSuccess((result) =>
                    {
                        Console.WriteLine("Success ...");
                        List<User> Users = result.Data;
                        for (int i = 0; i < Users.Count; i++)
                        {
                            User m = Users[i];
                            Console.WriteLine("name:" + m.name + " age :" + m.age + " salary:" + m.salary);
                        }
                    })
                    .OnFailure((res) =>
                    {
                        Console.WriteLine("Failure ..code:+" + res.Code + "...msc:" + res.Message);
                    })
                    .OnError((res) =>
                    {
                        Console.WriteLine("Error ..code:+" + res.Code + "...msc:" + res.Message);
                    })
                    .SetHeaders(headerCollection) // 不必须
                   // .Post(url, collection); //参数类型为 NameValueCollection
                   //.Post<User>(url, user);  //参数为对像 
                   //.Post(url, "name=code&age=56"); //参数为字符串
                   .Post(url); //不需要参数
          }
    
              string url = "http://127.0.0.1:8080/nbm/login/test";
            
              HttpWebRequestHelper<User>.Instance                
                    .OnSuccess((result) =>
                    {
                        Console.WriteLine("Success ...");
                        User Users = result.Data;
                        for (int i = 0; i < Users.Count; i++)
                        {
                            User m = Users[i];
                            Console.WriteLine("name:" + m.name + " age :" + m.age + " salary:" + m.salary);
                        }
                    })                 
                   .Post(url);
    
    

捐赠

如果您觉得httpHelper还不错,并且刚好有些闲钱,那么可以选择以下方式来捐赠:

  • 支付宝
    qrcode
  • 微信
    qrcode

Release Notes

修改程序集的命名空间,优化代码,将一些方法的修饰符设为 internal

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.4 144 9/10/2019
1.0.3 106 9/10/2019
1.0.2 100 9/9/2019
1.0.1 110 9/9/2019