新建class 命名 HttpRequest.cs, 内容:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.IO; using System.Net; namespace yourNameSpace { class HttpRequest { public static string Http(string url, string method, string data) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = string.IsNullOrEmpty(method) ? "GET" : method; request.ContentType = "application/json;charset=utf-8"; if (!string.IsNullOrEmpty(data)) { Stream RequestStream = request.GetRequestStream(); byte[] bytes = Encoding.UTF8.GetBytes(data); RequestStream.Write(bytes, 0, bytes.Length); RequestStream.Close(); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream ResponseStream = response.GetResponseStream(); StreamReader StreamReader = new StreamReader(ResponseStream, Encoding.GetEncoding("utf-8")); string re = StreamReader.ReadToEnd(); StreamReader.Close(); ResponseStream.Close(); return re; } } }
在同命名空间下调用:
string res = HttpRequest.Http("接口地址", "GET", "");
1