当前位置:编程学习 > ASP.NET> 正文

.NET get、post 请求

时间:2015-10-31类别:编程学习

.NET get、post 请求

.NET get、post 请求

一、.NET post 请求

 

  •  
  • C# 代码   复制
  • 
    
    ///url:Post请求的URL地址
    ///param:需要传递的参数,例如:a=2&b=3.可以为""空字符串
    ///timeOut:Post请求超时时间
    ///encoding:Post请求的编码,例如:Encoding.UTF8
    ///contentType:Post请求的类型
    
    public static string HttpPost(string url, string param, int timeOut, Encoding encoding,string contentType="")
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.Method = "POST";
                request.ContentType =string.IsNullOrEmpty(contentType)? "application/x-www-form-urlencoded":contentType;
                request.Accept = "*/*";
                request.Timeout = 1000 * timeOut;
                request.AllowAutoRedirect = false;
    
                StreamWriter requestStream = null;
                WebResponse response = null;
                string responseStr = null;
    
                try
                {
                    requestStream = new StreamWriter(request.GetRequestStream());
                    requestStream.Write(param);
                    requestStream.Close();
    
                    response = request.GetResponse();
                    if (response != null)
                    {
                        StreamReader reader = new StreamReader(response.GetResponseStream(), encoding);
                        responseStr = reader.ReadToEnd();
                        reader.Close();
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    request = null;
                    requestStream = null;
                    response = null;
                }
    
                return responseStr;
            }
    
    		
  •  

    二、.NET get 请求

     

  •  
  • C# 代码   复制
  • 
    public static string HttpGet(string url, int timeOut, Encoding encoding)
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.Method = "GET";
                request.ContentType = "application/x-www-form-urlencoded";
                request.Accept = "*/*";
                request.Timeout = 1000 * timeOut;
                request.AllowAutoRedirect = false;
    
                WebResponse response = null;
                string responseStr = null;
    
                try
                {
                    response = request.GetResponse();
    
                    if (response != null)
                    {
                        StreamReader reader = new StreamReader(response.GetResponseStream(), encoding);
                        responseStr = reader.ReadToEnd();
                        reader.Close();
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    request = null;
                    response = null;
                }
    
                return responseStr;
            }
    
    
    		
  •  

    标签:
    上一篇下一篇

    猜您喜欢

    热门推荐