C#_模拟webAp_POST-GET-PUT-DELETE
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的更多相关文章
- cURL(wget)—— 测试 RESTful 接口及模拟 GET/POST/PUT/DELETE/OPTIONS 请求
cURL 是一个简单的 http 命令行工具.与最优秀的 Unix 工具一样,在设计之时,cURL 是个小型程序,功能十分专一,而且是故意为之,仅用于访问 http 服务器.(在 Linux 中,可以 ...
- pytho简单爬虫_模拟登陆西电流量查询_实现一键查询自己的校园网流量
闲来无事,由于校园内网络是限流量的,查询流量很是频繁,于是萌生了写一个本地脚本进行一键查询自己的剩余流量. 整个部分可以分为三个过程进行: 对登陆时http协议进行分析 利用python进行相关的模拟 ...
- ZC_C++类函数指针_模拟_Delphi类函数指针_Qt例子
qt-opensource-windows-x86-msvc2010_opengl-5.3.2.exe ZC: “const QString” 作传入参数的时候,不太会弄... 貌似 还是在进行构建等 ...
- BZOJ_1028_[JSOI2007]_麻将_(模拟+贪心)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1028 同一种花色的牌,序数为\(1,2,...,n\).定义"和了"为手上 ...
- python爬虫学习(3)_模拟登陆
1.登陆超星慕课,chrome抓包,模拟header,提取表单隐藏元素构成params. 主要是验证码图片地址,在js中发现由js->new Date().getTime()时间戳动态生成url ...
- C#_socket拆包_封包_模拟乱序包
拆包一直是个硬伤呀,MLGB的,服务端各种乱数据,果断整理下 拆包思路:设计一个网络协议,一般都会分包,一个包就相当于一个逻辑上的命令. .如果我们用udp协议,省事的多,一次会收到一个完整的包,但U ...
- ZC_C++类函数指针_模拟_Delphi类函数指针
ZC: C++的类函数指针 不像 Delphi的类函数指针,前者 需要规定死 是哪个类的函数的指针,后者就不需要 很灵活. 测试环境: Win7x64 cn_visual_studio_2010_ul ...
- 零基础逆向工程40_Win32_14_枚举窗口_模拟鼠标键盘
1 查找窗口 1.1 代码案例 //查找指定窗口 TCHAR szTitle[MAX_PATH] = {0}; HWND hwnd = ::FindWindow(TEXT("#32770&q ...
- phpSpider 单页测试_模拟登陆
<?php require './vendor/autoload.php'; use phpspider\core\phpspider; use phpspider\core\requests; ...
随机推荐
- 宏定义(#define)和常量(const)的区别
最近开始准备一边做实验室的研究,一边记录一些遇到的编程中的小知识点.今天在测试对矩阵进行SVD分解时,需要定义矩阵的行和列的大小,我习惯性的用宏定义来定义了这两个变量,在运行的时候,就开始思考宏定义和 ...
- MongoDB@入门一
安装MongoDB自行搜索, 我这里提供GUI版本类似navicat. 1. 数据库层面 show dbs #查看服务器上的数据库 [local 0.000GB] use test #切换到指定 ...
- 我需要在电脑上安装C编译器
这本书中我们使用了gcc(GNU编译器套装),它不但功能十分强大,而且还是免费的.你需要确保你的电脑上已经安装了gcc.如果你的操作系统是Linux,恭喜你,你已经拥有了gcc.
- JAVA 基础 重新开始
之前做android开发,因为JAVA基础不牢固的原因,自己对写代码很不自信,很多时候要去找源码或者在相近的代码上修修改改以得到想要的结果,从某种意义上来说这根本算不上真正意义上的程序员.后来看到某位 ...
- 解开发者之痛:中国移动MySQL数据库优化最佳实践(转)
开源数据库MySQL比较容易碰到性能瓶颈,为此经常需要对MySQL数据库进行优化,而MySQL数据库优化需要运维DBA与相关开发共同参与,其中MySQL参数及服务器配置优化主要由运维DBA完成,开发则 ...
- ocp 1Z0-043 131-205题解析
131. Which three methods can you use to run an Automatic Database Diagnostic Monitor (ADDM) analysis ...
- HDU 4370 0 or 1 (最短路+最小环)
0 or 1 题目链接: Rhttp://acm.hust.edu.cn/vjudge/contest/122685#problem/R Description Given a n*n matrix ...
- jQuery基础学习7——层次选择器find()方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- C C++实现创建目录
下面代码是C.C++可以使用的创建目录的函数及头文件,这是引用的opencv,haartraining中的一种方式. #include <direct.h> //不同系统可能不一样,这是在 ...
- js面形对象(2)
1.原型与in操作符 有两种方式使用in操作符:单独使用和在for-in循环中使用.在单独使用时,in操作符会在通过对象能够访问给定属性时,返回true,无论该属性是存在实例或者是存在于原型 ...