C#调用HTTP接口两种方式

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

C#调⽤HTTP接⼝两种⽅式using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ;
using System.Text;
using System.Web;
namespace WebApplication1
{
public class RestClient
{
private string BaseUri;
public RestClient(string baseUri)
{
this.BaseUri = baseUri;
}
#region Get请求
public string Get(string uri)
{
//先根据⽤户请求的uri构造请求地址
string serviceUrl = string.Format("{0}/{1}", this.BaseUri, uri);
//创建Web访问对象
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
//通过Web访问对象获取响应内容
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
//通过响应内容流创建StreamReader对象,因为StreamReader更⾼级更快
StreamReader reader = new StreamReader(myResponse.GetResponseStream(),Encoding.UTF8);
//string returnXml = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就⽤这个⽅法
string returnXml = reader.ReadToEnd();//利⽤StreamReader就可以从响应内容从头读到尾
reader.Close();
myResponse.Close();
return returnXml;
}
#endregion
#region Post请求
public string Post(string data, string uri)
{
//先根据⽤户请求的uri构造请求地址
string serviceUrl = string.Format("{0}/{1}", this.BaseUri, uri);
//创建Web访问对象
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
//把⽤户传过来的数据转成“UTF-8”的字节流
byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
myRequest.Method = "POST";
myRequest.ContentLength = buf.Length;
myRequest.ContentType = "application/json";
myRequest.MaximumAutomaticRedirections = 1;
myRequest.AllowAutoRedirect = true;
//发送请求
Stream stream = myRequest.GetRequestStream();
stream.Write(buf,0,buf.Length);
stream.Close();
//获取接⼝返回值
//通过Web访问对象获取响应内容
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
//通过响应内容流创建StreamReader对象,因为StreamReader更⾼级更快
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
//string returnXml = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就⽤这个⽅法
string returnXml = reader.ReadToEnd();//利⽤StreamReader就可以从响应内容从头读到尾
reader.Close();
myResponse.Close();
return returnXml;
}
#endregion
#region Put请求
public string Put(string data, string uri)
{
//先根据⽤户请求的uri构造请求地址
string serviceUrl = string.Format("{0}/{1}", this.BaseUri, uri);
//创建Web访问对象
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
//把⽤户传过来的数据转成“UTF-8”的字节流
byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
myRequest.Method = "PUT";
myRequest.ContentLength = buf.Length;
myRequest.ContentType = "application/json";
myRequest.MaximumAutomaticRedirections = 1;
myRequest.AllowAutoRedirect = true;
//发送请求
Stream stream = myRequest.GetRequestStream();
stream.Write(buf, 0, buf.Length);
stream.Close();
//获取接⼝返回值
//通过Web访问对象获取响应内容
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
//通过响应内容流创建StreamReader对象,因为StreamReader更⾼级更快
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
//string returnXml = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就⽤这个⽅法
string returnXml = reader.ReadToEnd();//利⽤StreamReader就可以从响应内容从头读到尾
reader.Close();
myResponse.Close();
return returnXml;
}
#endregion
#region Delete请求
public string Delete(string data, string uri)
{
//先根据⽤户请求的uri构造请求地址
string serviceUrl = string.Format("{0}/{1}", this.BaseUri, uri);
//创建Web访问对象
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
//把⽤户传过来的数据转成“UTF-8”的字节流
byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);
myRequest.Method = "DELETE";
myRequest.ContentLength = buf.Length;
myRequest.ContentType = "application/json";
myRequest.MaximumAutomaticRedirections = 1;
myRequest.AllowAutoRedirect = true;
//发送请求
Stream stream = myRequest.GetRequestStream();
stream.Write(buf, 0, buf.Length);
stream.Close();
//获取接⼝返回值
//通过Web访问对象获取响应内容
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
//通过响应内容流创建StreamReader对象,因为StreamReader更⾼级更快
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
//string returnXml = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就⽤这个⽅法
string returnXml = reader.ReadToEnd();//利⽤StreamReader就可以从响应内容从头读到尾
reader.Close();
myResponse.Close();
return returnXml;
}
#endregion
}
}。

相关文档
最新文档