后台用C#发起HttpWeb请求

 public static string HttpPost(string strUrl, NameValueCollection pars, int timeout, string charset)
        {
            Encoding encoding = Encoding.GetEncoding(charset);
            string strPars = GetParsString(pars);
            byte[] data = encoding.GetBytes(strPars);

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(strUrl);
            request.Method = "POST";
            request.Timeout = timeout;
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;

            Stream newStream = request.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();

            string result = string.Empty;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream s = response.GetResponseStream();
            StreamReader sr = new StreamReader(s, encoding);
            result = sr.ReadToEnd();
            sr.Close();
            response.Close();

            return result;
        }

 

你可能感兴趣的:(http)