标签:png service pos reader get code turn coding response
原文:https://www.cnblogs.com/i3yuan/p/10703500.html
insert 简单测试:
public void test8() { string url = "http://localhost:21021/api/services/app/Role/Create"; var str2 = HttpPost3(url, System.IO.File.ReadAllText("2.txt")); Console.WriteLine(str2); Console.Read(); }
public string HttpPost4(string url, string body) { Encoding encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/json-patch+json"; byte[] buffer = encoding.GetBytes(body); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { return reader.ReadToEnd(); } }
update简单测试:
public void test10() { string url = "http://localhost:21021/api/services/app/Menu/Update"; var str2 = HttpPost5(url, "{\"name\": \"1\",\"url\": \"1\",\"id\": \"1\"}"); Console.WriteLine(str2); Console.Read(); }
public string HttpPost5(string url, string body) { Encoding encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "PUT"; request.ContentType = "application/json-patch+json"; byte[] buffer = encoding.GetBytes(body); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { return reader.ReadToEnd(); } }
delete简单测试:
public void test11() { string url = "http://localhost:21021/api/services/app/Menu/Delete?Id=12"; var str2 = Delete("", url); Console.WriteLine(str2); Console.Read(); }
public string Delete(string data, string uri) { HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(uri); byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data); Request.Method = "DELETE"; Request.ContentLength = buf.Length; Request.ContentType = "application/json"; Request.MaximumAutomaticRedirections = 1; Request.AllowAutoRedirect = true; Stream stream = Request.GetRequestStream(); stream.Write(buf, 0, buf.Length); stream.Close(); HttpWebResponse Response = (HttpWebResponse)Request.GetResponse(); StreamReader reader = new StreamReader(Response.GetResponseStream(), Encoding.UTF8); string returnXml = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾 reader.Close(); Response.Close(); return returnXml; }
标签:png service pos reader get code turn coding response
原文地址:https://www.cnblogs.com/guxingy/p/11972121.html