public class ValuesController : ApiController
{
// GET api/<controller>
publicIEnumerable<string> Get()
{
returnnewstring[] { "value1", "value2" };
}
// GET api/<controller>/5
publicstring Get(int id)
{
return"value";
}
// POST api/<controller>
publicvoid Post([FromBody]string value)
{
}
// PUT api/<controller>/5
publicvoid Put(int id, [FromBody]string value)
{
}
// DELETE api/<controller>/5
publicvoid Delete(int id)
{
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
namespace OilDigital.A2_A27.Web
{
public class RestClient
{
public string EndPoint { get; set; } //请求的url地址
public HttpVerb Method { get; set; } //请求的方法
public string ContentType { get; set; } //格式类型:我用的是application/json,text/xml具体使用什么,看需求吧
public string PostData { get; set; } //传送的数据,当然了我使用的是json字符串
public RestClient()
{
EndPoint = "";
Method = HttpVerb.GET;
ContentType = "application/x-www-form-urlencoded";
PostData = "";
}
public RestClient(string endpoint)
{
EndPoint = endpoint;
Method = HttpVerb.GET;
ContentType = "application/json";
PostData = "";
}
public RestClient(string endpoint, HttpVerb method)
{
EndPoint = endpoint;
Method = method;
ContentType = "application/json";
PostData = "";
}
public RestClient(string endpoint, HttpVerb method, string postData)
{
EndPoint = endpoint;
Method = method;
ContentType = "application/json";
PostData = postData;
}
public RestClient(string endpoint, HttpVerb method, string postData, string contentType)
{
EndPoint = endpoint;
Method = method;
ContentType = contentType;
PostData = postData;
}
public string MakeRequest()
{
return MakeRequest("");
}
public string MakeRequest(string parameters)
{
var request = (HttpWebRequest)WebRequest.Create(EndPoint + parameters);
request.Method = Method.ToString();
request.ContentType = ContentType;
if (!string.IsNullOrEmpty(PostData) && Method == HttpVerb.POST)//如果传送的数据不为空,并且方法是post
{
var encoding = new UTF8Encoding();
var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(PostData);//编码方式按自己需求进行更改,我在项目中使用的是UTF-8
request.ContentLength = bytes.Length;
using (var writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, 0, bytes.Length);
}
}
if (!string.IsNullOrEmpty(PostData) && Method == HttpVerb.PUT)//如果传送的数据不为空,并且方法是put
{
var encoding = new UTF8Encoding();
var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(PostData);//编码方式按自己需求进行更改,我在项目中使用的是UTF-8
request.ContentLength = bytes.Length;
using (var writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, 0, bytes.Length);
}
}
using (var response = (HttpWebResponse)request.GetResponse())
{
var responseValue = string.Empty;
if (response.StatusCode != HttpStatusCode.OK)
{
var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
throw new ApplicationException(message);
}
// grab the response
using (var responseStream = response.GetResponseStream())
{
if (responseStream != null)
using (var reader = new StreamReader(responseStream))
{
responseValue = reader.ReadToEnd();
}
}
return responseValue;
}
}
}
public enum HttpVerb
{
GET, //method 常用的就这几样,当然你也可以添加其他的 get:获取 post:修改 put:写入 delete:删除
POST,
PUT,
DELETE
}
}
var client = new RestClient(); string endPoint = @"http:\myRestService.comapi"; var client = new RestClient(endPoint); var json = client.MakeRequest();
var json = client.MakeRequest("?param=0");
var client = new RestClient();
client.EndPoint = @"http:\myRestService.comapi"; ;
client.ContentType = "application/json";
client.Method = HttpVerb.POST;
client.PostData = "{postData: value}";
var json = client.MakeRequest();
/// <summary>
/// 从接口中获取当前用户所有信息
/// </summary>
/// <param name="userId">用户ID</param>
/// <returns>json对象</returns>
public string GetCurrentUserInfo()
{
string userId = GetCurrentUserId();
string endPoint = "http://localhost:100/Api/RestService/"+userId;
var client = new RestClient(endPoint);
var userInfo = client.MakeRequest();
return userInfo;
}
/// <summary>
/// 从接口中获取用户所有信息
/// </summary>
/// <param name="userId">用户ID</param>
/// <returns></returns>
public static JObject CacheUser()
{
try
{
string currentUser = GetCurrentUserId();
if (HttpRuntime.Cache.Get("user$" + GetCurrentUserId()) == null)
{
string endPoint = "http://66.66.66.666:6666/DASBASE/restServices/dataCollectionService/getUserPermissions";
string postData = "jsonData={"userCode": "kfry","systemId": "1E1A7AC94BFC41D4BEBED8942EB69689"}";
var client = new RestClient(endPoint, HttpVerb.POST, postData, "application/x-www-form-urlencoded");
var u = client.MakeRequest();
JObject userInfo = JObject.Parse(u);
//插入缓存
HttpRuntime.Cache.Insert("user$" + currentUser, userInfo, null, System.DateTime.UtcNow.AddMinutes(30), TimeSpan.Zero);
}
return (JObject)HttpRuntime.Cache.Get("user$" + GetCurrentUserId());
}
catch (Exception ex)
{
throw new ApplicationException("获取用户信息出错:"+ex.Message);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有