System.Web.HttpRequest是封装浏览器对服务器的请求的,主要用在ASP.NET中,其中包括浏览器请求的网址,查询字符串数据或表单数据等等,下面是两个自己经常使用的两个请求方法一个是Get一个是Post

首先是GET请求方式


public static string HttpGet(string url, string parms)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + (parms == "" ? "" : "?") + parms);
    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;
}
下面在来一个是POST请求方式


public static string HttpPost(string url, string parms)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = parms.Length;
    StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII);
    writer.Write(parms);
    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;
}