using System.IO;
using System.Web;
public string HttpGet(string Url)//发送GET请求
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url)
request.Method = "GET"
request.ContentType = "text/html;charset=UTF-8"
HttpWebResponse response = (HttpWebResponse)request.GetResponse()
Stream myResponseStream = response.GetResponseStream()
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8)
string retString = myStreamReader.ReadToEnd()
myStreamReader.Close()
myResponseStream.Close()
return retString
}
public static string HttpPost(string Url, string postDataStr)//发送post请求
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postDataStr.Length;
StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII);
writer.Write(postDataStr);
writer.Flush();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string encoding = response.ContentEncoding;
if (encoding == null || encoding.Length < 1)
{
encoding = "UTF-8";
}
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
string retString = reader.ReadToEnd();
return retString;
}
GET请求使用案例:
string resstr = HttpGet("http://localhost:8080/api/plates/processong_data?database=<database>&access_token=<access_token>");
POST请求使用说明:
string resstr = HttpPost("http://127.0.0.1:8080/api/plates/{upi}/processing_data?database=<database>&access_token=<access_token>", json);