using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web;
namespace WebAPIClientDemo
{
public class RestClient
{
private string BaseUri;
public RestClient(string baseUri)
{
this.BaseUri = baseUri;
} #region Delete方式
public string Delete(string data, string uri)
{
return CommonHttpRequest(data, uri, "DELETE");
} public string Delete(string uri)
{
//Web访问对象64
string serviceUrl = string.Format("{0}/{1}", this.BaseUri, uri);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
myRequest.Method = "DELETE";
// 获得接口返回值68
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
//string ReturnXml = HttpUtility.UrlDecode(reader.ReadToEnd());
string ReturnXml = reader.ReadToEnd();
reader.Close();
myResponse.Close();
return ReturnXml;
}
#endregion #region Put方式
public string Put(string data, string uri)
{
return CommonHttpRequest(data, uri, "PUT");
}
#endregion #region POST方式实现 public string Post(string data, string uri)
{
return CommonHttpRequest(data,uri,"POST");
} public string CommonHttpRequest(string data, string uri,string type)
{
//Web访问对象,构造请求的url地址
string serviceUrl = string.Format("{0}/{1}", this.BaseUri, uri); //构造http请求的对象
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
//转成网络流
byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
//设置
myRequest.Method = type;
myRequest.ContentLength = buf.Length;
myRequest.ContentType = "application/json";
myRequest.MaximumAutomaticRedirections = ;
myRequest.AllowAutoRedirect = true;
// 发送请求
Stream newStream = myRequest.GetRequestStream();
newStream.Write(buf, , buf.Length);
newStream.Close();
// 获得接口返回值
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string ReturnXml = reader.ReadToEnd();
reader.Close();
myResponse.Close();
return ReturnXml;
}
#endregion #region GET方式实现
public string Get(string uri)
{
//Web访问对象64
string serviceUrl = string.Format("{0}/{1}", this.BaseUri, uri); //构造一个Web请求的对象
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
// 获得接口返回值68
//获取web请求的响应的内容
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); //通过响应流构造一个StreamReader
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
//string ReturnXml = HttpUtility.UrlDecode(reader.ReadToEnd());
string ReturnXml = reader.ReadToEnd();
reader.Close();
myResponse.Close();
return ReturnXml;
}
#endregion
}
}

调用方式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace WebAPIClientDemo
{
class Program
{
static void Main(string[] args)
{
RestClient client = new RestClient("http://localhost:50168"); #region Get 方式请求列表
string str = client.Get("api/values"); Console.WriteLine(str);
#endregion #region Get 方式请求id对应的数据
string strGetById = client.Get("api/values/2"); Console.WriteLine(strGetById);
#endregion #region Post 方式 添加数据 string postUri = "api/values/"; string userJson = @"{""Id"":123,""Age"":12,""UserInfo"":""111""}"; string postResponse = client.Post(userJson, postUri); Console.WriteLine(postResponse);
#endregion #region Delete string deleteUri = "api/values/3";
string deleteResponse = client.Delete(deleteUri); Console.WriteLine( deleteResponse);
#endregion #region Put
string putUri = "api/values/123"; string userJson3 = @"{""Id"":123,""Age"":12,""UserInfo"":""111""}"; string putResponse = client.Post(userJson3, putUri); Console.WriteLine(putResponse);
#endregion Console.ReadKey();
}
}
}

C#_模拟webAp_POST-GET-PUT-DELETE的更多相关文章

  1. cURL(wget)—— 测试 RESTful 接口及模拟 GET/POST/PUT/DELETE/OPTIONS 请求

    cURL 是一个简单的 http 命令行工具.与最优秀的 Unix 工具一样,在设计之时,cURL 是个小型程序,功能十分专一,而且是故意为之,仅用于访问 http 服务器.(在 Linux 中,可以 ...

  2. pytho简单爬虫_模拟登陆西电流量查询_实现一键查询自己的校园网流量

    闲来无事,由于校园内网络是限流量的,查询流量很是频繁,于是萌生了写一个本地脚本进行一键查询自己的剩余流量. 整个部分可以分为三个过程进行: 对登陆时http协议进行分析 利用python进行相关的模拟 ...

  3. ZC_C++类函数指针_模拟_Delphi类函数指针_Qt例子

    qt-opensource-windows-x86-msvc2010_opengl-5.3.2.exe ZC: “const QString” 作传入参数的时候,不太会弄... 貌似 还是在进行构建等 ...

  4. BZOJ_1028_[JSOI2007]_麻将_(模拟+贪心)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1028 同一种花色的牌,序数为\(1,2,...,n\).定义"和了"为手上 ...

  5. python爬虫学习(3)_模拟登陆

    1.登陆超星慕课,chrome抓包,模拟header,提取表单隐藏元素构成params. 主要是验证码图片地址,在js中发现由js->new Date().getTime()时间戳动态生成url ...

  6. C#_socket拆包_封包_模拟乱序包

    拆包一直是个硬伤呀,MLGB的,服务端各种乱数据,果断整理下 拆包思路:设计一个网络协议,一般都会分包,一个包就相当于一个逻辑上的命令. .如果我们用udp协议,省事的多,一次会收到一个完整的包,但U ...

  7. ZC_C++类函数指针_模拟_Delphi类函数指针

    ZC: C++的类函数指针 不像 Delphi的类函数指针,前者 需要规定死 是哪个类的函数的指针,后者就不需要 很灵活. 测试环境: Win7x64 cn_visual_studio_2010_ul ...

  8. 零基础逆向工程40_Win32_14_枚举窗口_模拟鼠标键盘

    1 查找窗口 1.1 代码案例 //查找指定窗口 TCHAR szTitle[MAX_PATH] = {0}; HWND hwnd = ::FindWindow(TEXT("#32770&q ...

  9. phpSpider 单页测试_模拟登陆

    <?php require './vendor/autoload.php'; use phpspider\core\phpspider; use phpspider\core\requests; ...

随机推荐

  1. 数据结构(三)实现AVL树

    AVL树的定义 一种自平衡二叉查找树,中面向内存的数据结构. 二叉搜索树T为AVL树的满足条件为: T是空树 T若不是空树,则TL.TR都是AVL树,且|HL-HR| <= 1 (节点的左子树高 ...

  2. 轻松突击ThreadLocal

    本文出自 代码大湿 代码大湿 ThreadLocal是用来保存线程的本地变量,可以保证每个线程都有一个自己的变量(包括static变量). 本文所有代码请点击我 1 看个实际场景. 我们要设计一个序列 ...

  3. JAVA NIO 类库的异步通信框架netty和mina

    Netty 和 Mina 我究竟该选择哪个? 根据我的经验,无论选择哪个,都是个正确的选择.两者各有千秋,Netty 在内存管理方面更胜一筹,综合性能也更优.但是,API 变更的管理和兼容性做的不是太 ...

  4. WebService学习之四:关于JAX-WS 注释

    基于 XML 的 Web Service 的 Java API"(JAX-WS)通过使用注释来指定与 Web Service 实现相关联的元数据以及简化 Web Service 的开发.注释 ...

  5. ACM OJ Collection

    浙江大学(ZJU):http://acm.zju.edu.cn/ 北京大学(PKU):http://acm.pku.edu.cn/JudgeOnline/ 同济大学(TJU):http://acm.t ...

  6. Linux下的进程管理

    在操作系统系统中,进程是一个非常重要的概念. 一.Linux中进程的相关知识 1.什么是进程呢? 通俗的来说进程是运行起来的程序.唯一标示进程的是进程描述符(PID),在linux内核中是通过task ...

  7. HDU 2045 不容易系列之(3)—— LELE的RPG难题 (递推)

    题意:略. 析:首先是假设前n-2个已经放好了,那么放第 n 个时,先考虑一下第 n-1 放的是什么,那么有两种情况. 如果n-1放的是和第1个一样的,那么第 n 个就可以在n-2的基础上放2个,也就 ...

  8. SQL大数据查询分页存储过程

    最后一页分页一卡死,整个网站的性能都会非常明显的下降,不知道为啥,微软有这个BUG一直没处理好.希望SQL2012里不要有这个问题就好了. 参考代码如下: -- =================== ...

  9. Objective-C :Category

    Category 引入 在日常的开发中,可能会碰到这样的需求:给某个类增加方法.比如说,需要给NSString类增加一个打印的方法.当然,我们可以新建一个类比如TestString,并继承NSStri ...

  10. c# 实现IComparable、IComparer接口、Comparer类的详解

    在默认情况下,对象的Equals(object o)方法(基类Object提供),是比较两个对象变量是否引用同一对象.我们要必须我自己的对象,必须自己定义对象比较方式.IComparable和ICom ...