HttpWebRequest使用POST提交数据的

private void Common(string url,string Postdata)
        {
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
            Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
            byte[] arrB = encode.GetBytes(Postdata);
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = arrB.Length;

            Stream outStream = myRequest.GetRequestStream();
            outStream.Write(arrB, 0, arrB.Length);
            outStream.Close();

            string sst = "";
            HttpWebResponse HttpWResp;
            try
            {
                HttpWResp = (HttpWebResponse)myRequest.GetResponse();
            }
            catch (WebException ex)
            {
                HttpWResp = (HttpWebResponse)ex.Response;
            }
            //finally
            //{
            //    Response.Close();
            //}

            Stream myStream = HttpWResp.GetResponseStream();
            using (StreamReader sr = new StreamReader(myStream, Encoding.UTF8))
            {
                sst = sr.ReadToEnd();
            }
            str.InnerText = sst;
            //var jObjectnum = JObject.Parse(sst);
        }

你可能感兴趣的:(HttpWebRequest使用POST提交数据的)