客户端的HTTP的请求方式一般分为四种:GET、POST、PUT、DELETE,这四种请求方式有什么不同呢。
简单的说,GET就是获取资源,POST就是创建资源,PUT就是更新资源,DELETE 就是删除资源。具体来说:
PUT:PUT请求是向服务器端发送数据的,从而改变信息,该请求就像数据库的update操作一样,用来修改数据的内容,但是不会增加数据的种类等,也就是说无论进行多少次PUT操作,资源不会增加。
DELETE:DELETE请求顾名思义,就是用来删除某一个资源的,该请求就像数据库的delete操作。
GET:GET操作是安全的。所谓安全是指不管进行多少次操作,资源的状态都不会改变,GET只是访问和查看资源。
POST:操作不是安全的,每次请求都会创建资源,当我们多次发出POST请求后,其结果是创建出了多个资源。还有一点需要注意的就是,创建操作可以使用POST,也可以使用PUT,区别在于POST 是作用在一个集合资源之上的(/uri),而PUT操作是作用在一个具体资源之上的(/uri/xxx),再通俗点说,如果URL可以在客户端确定,那么就使用PUT,如果是在服务端确定,那么就使用POST,比如说很多资源使用数据库自增主键作为标识信息,而创建的资源的标识信息到底是什么只能由服务端提供,这个时候就必须使用POST。
C# POST 请求
//创建 Post 的HTTP请求 带Json参数的 public string PostRequest(string url, string headerToken, string strParam) { try { //string strParam = "{\"destNumber\":\"" + strExtTel + "\", \"app\":\"transfer\",\"args\":\"" + strCalledNumber + "\"}"; //String strUrl = "http://192.168.1.31:80/api/channels"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; //request.ContentType = "text/html, application/xhtml+xml, */*"; request.ContentType = "application/json"; request.Headers["X-XTRA-AUTH-ID"] = headerToken; //添加头 //参数 ASCIIEncoding encoding = new ASCIIEncoding(); byte[] data = encoding.GetBytes(strParam); request.ContentLength = data.Length; Stream sm = request.GetRequestStream(); sm.Write(data, 0, data.Length); sm.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream rs = response.GetResponseStream(); //StreamReader sr = new StreamReader(rs, encode); StreamReader sr = new StreamReader(rs, Encoding.UTF8); var result = sr.ReadToEnd(); sr.Close(); rs.Close(); return result; } catch (Exception ex) { myClass.clsLogHelper.m_CreateErrorLogTxt("clsWebSubmit", "PostRequest()", ex.Message.ToString()); return "-1"; } }
C# GET 请求
//创建GET的HTTP请求 public string GetRequest(string url, string headerToken) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; //request.ContentType = "text/html, application/xhtml+xml, */*"; request.ContentType = "application/json"; request.Headers["X-XTRA-AUTH-ID"] = headerToken; //添加头 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream rs = response.GetResponseStream(); //StreamReader sr = new StreamReader(rs, encode); StreamReader sr = new StreamReader(rs, Encoding.UTF8); var result = sr.ReadToEnd(); sr.Close(); rs.Close(); return result; }
C# DELETE 请求
public string DeleteRequest(string url, string headerToken) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "DELETE"; //request.ContentType = "text/html, application/xhtml+xml, */*"; request.ContentType = "application/json"; request.Headers["X-XTRA-AUTH-ID"] = headerToken; //添加头 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream rs = response.GetResponseStream(); //StreamReader sr = new StreamReader(rs, encode); StreamReader sr = new StreamReader(rs, Encoding.UTF8); var result = sr.ReadToEnd(); sr.Close(); rs.Close(); return result; }
注:其中 headerToken 可以没有,具体在接口的需求
完整类文件可参考
https://www.cnblogs.com/hailexuexi/p/16895404.html
感谢 潘达小新 的分享
https://blog.csdn.net/fangyu723/article/details/106668905
标签:Http,string,GET,C#,sr,rs,request,POST,StreamReader From: https://www.cnblogs.com/hailexuexi/p/16902190.html