更新时间:2023-02-15 21:14:42
试试
使用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(&);
}
//将参数编码为表格数据:
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;
}