HTTP请求与接收get/post方式

Posted zl181015

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HTTP请求与接收get/post方式相关的知识,希望对你有一定的参考价值。

        
//get方式
public string HttpGet(string Url, string postDataStr) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr); 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.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; }
//post方式
private string HttpPost(string Url, string postDataStr) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr); Stream myRequestStream = request.GetRequestStream(); StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312")); myStreamWriter.Write(postDataStr); myStreamWriter.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; }

 接收方式

get接收    

            string str = Request.Url.ToString();
            string user = Request.QueryString["user"].ToString();
            WriteLog(str);

post接收  

            Stream s = System.Web.HttpContext.Current.Request.InputStream;
            byte[] b = new byte[s.Length];
            s.Read(b, 0, (int)s.Length);
            WriteLog(Encoding.UTF8.GetString(b));
            return Encoding.UTF8.GetString(b);

 

以上是关于HTTP请求与接收get/post方式的主要内容,如果未能解决你的问题,请参考以下文章

前端HTTP发POST请求携带参数与后端接口接收参数

HTTP请求中POST与GET的区别

http请求中get和post请求的区别

HTTP GET方式提交与POST方式提交

Struts2 Action接收POST请求JSON数据及其实现解析

Servlet获得Http请求,GET/POST