如何在asp.net中实现RestFul Web API [英] how to implement RestFul web API in asp .net
问题描述
我正在探索RestFul Web API。我不知道如何实现它。所以需要一些帮助。如果你知道,请分享一些文章。
I am exploring RestFul Web API. I dont know how to implement it. so need some help..share some article if you know.
推荐答案
试试
使用ASP.NET Web API构建RESTful API [ ^ ]
REST和.net Web API简介 [ ^ ]
使用ASP.NET Web API创建REST服务 [ ^ ]
Try
Build RESTful API's with ASP.NET Web API[^]
Introduction to REST and .net Web API[^]
Creating REST Service using ASP.NET Web API [^]
试试这个REST w eb API示例。
HTTP GET请求
Hi,
Try this REST web API example.
HTTP GET Requests
static string HttpGet(string url) {
HttpWebRequest req = WebRequest.Create(url)
as HttpWebRequest;
string result = null;
using (HttpWebResponse resp = req.GetResponse()
as HttpWebResponse)
{
StreamReader reader =
new StreamReader(resp.GetResponseStream());
result = reader.ReadToEnd();
}
return result;
}
HTTP POST请求
HTTP POST Requests
<pre lang="c#">
静态字符串HttpPost(字符串url,
string [] paramName,string [] paramVal)
{
HttpWebRequest req = WebRequest.Create(new Uri(url))
as HttpWebRequest;
req.Method =POST;
req.ContentType =application / x-www-form-urlencoded;
StringBuilder paramz = new StringBuilder();
for(int i = 0; i< paramName.Length; i ++){
paramz.Append(paramName [i]);
paramz .Append(=);
paramz.Append(HttpUtility.UrlEncode(paramVal [i]));
paramz.Append(&); < br $>
}
//将参数编码为表格数据:
byte [] formData =
UTF8Encoding.UTF8.GetBytes(paramz.ToString());
req.ContentLength = formData.Length;
/ /发送请求:
使用(Stream post = req.GetRequestStream())
{
post.Write(formData,0,formData 。长度);
}
string result = null;
using(HttpWebResponse resp = req.GetResponse()
as HttpWebResponse)
{
StreamReader reader =
new StreamReader(resp。 GetResponseStream());
result = reader.ReadToEnd();
}
返回结果;
}
static string HttpPost(string url,
string[] paramName, string[] paramVal)
{
HttpWebRequest req = WebRequest.Create(new Uri(url))
as HttpWebRequest;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
StringBuilder paramz = new StringBuilder();
for (int i = 0; i < paramName.Length; i++) {
paramz.Append(paramName[i]);
paramz.Append("=");
paramz.Append(HttpUtility.UrlEncode(paramVal[i]));
paramz.Append("&");
}
// Encode the parameters as form data:
byte[] formData =
UTF8Encoding.UTF8.GetBytes(paramz.ToString());
req.ContentLength = formData.Length;
// Send the request:
using (Stream post = req.GetRequestStream())
{
post.Write(formData, 0, formData.Length);
}
string result = null;
using (HttpWebResponse resp = req.GetResponse()
as HttpWebResponse)
{
StreamReader reader =
new StreamReader(resp.GetResponseStream());
result = reader.ReadToEnd();
}
return result;
}
这篇关于如何在asp.net中实现RestFul Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!